-
Notifications
You must be signed in to change notification settings - Fork 62
151 lines (129 loc) · 5.19 KB
/
docker-build.yml
File metadata and controls
151 lines (129 loc) · 5.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
name: Docker build and push
# limit concurrency
# https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#examples-using-concurrency-and-the-default-behavior
concurrency: docker_mmgis_main
on:
push:
# Only activate for `master` branch
branches:
- master
- development
# Plus for all tags
tags:
- "*"
# Plus for any pull-requests
pull_request:
branches:
- master
- development
# And for any final releases
release:
types: [published]
env:
# Will be "NASA-AMMOS/MMGIS" for the main repo, for forks "user-name-of-fork/MMGIS"
# For generating the tag, all will be converted to lowercase
IMAGE_SLUG: ${{ github.repository }}
jobs:
# Generate shared tags for both architectures
# The image tag pattern is:
# for pull-requests: <PATCH_VERSION>-<DATE>-<PR_NUMBER>, eg: 1.35.2-20210125-25
# for tags: <TAG>
# for `master` branch: latest,<PATCH_VERSION>-latest,<MINOR_VERSION>-latest,<MAJOR_VERSION>-latest,<PATCH_VERSION>-<DATE>-<SHA>
# for `development` branch: development,<MAJOR_VERSION>-development,<PATCH_VERSION>-<DATE>-<SHA>
# for releases: release,<PATCH_VERSION>-release,<MINOR_VERSION>-release,<MAJOR_VERSION>-release,<PATCH_VERSION>-<DATE>-<SHA>
# Version is parsed from package.json
generate-tags:
runs-on: ubuntu-latest
if: github.event_name == 'push' || github.event_name == 'pull_request' || github.event_name == 'release'
outputs:
tags: ${{ steps.generate.outputs.TAGS }}
image-id: ${{ steps.generate.outputs.IMAGE_ID }}
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Generate tags
id: generate
run: |
IMAGE_ID=$(echo "ghcr.io/$IMAGE_SLUG" | tr '[A-Z]' '[a-z]')
PATCH_VERSION=$(jq .version -r ./package.json)
BRANCH_OR_TAG=$(echo "${{ github.ref }}" | sed -e 's,.*/\(.*\),\1,')
TAGS=""
if [ "${{ github.event_name }}" == "pull_request" ]; then
PR_NUMBER=$(echo "${{ github.ref }}" | sed -e 's,.*/\(.*\)/merge,\1,')
TAGS="$PATCH_VERSION-$(date +%Y%m%d)-$PR_NUMBER"
elif [[ "$BRANCH_OR_TAG" == "master" ]]; then
TAGS="latest $PATCH_VERSION-latest"
elif [[ "$BRANCH_OR_TAG" == "development" ]]; then
TAGS="development"
elif [[ "${{ github.ref }}" == "refs/tags/"* ]]; then
TAGS="$BRANCH_OR_TAG"
fi
echo "Generated tags: $TAGS"
echo "TAGS=$TAGS" >> $GITHUB_OUTPUT
echo "IMAGE_ID=$IMAGE_ID" >> $GITHUB_OUTPUT
# Build and push AMD64 image on x64 runner
build-amd64:
needs: generate-tags
runs-on: ubuntu-latest # x64 runner for native AMD64 builds
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Login to GHCR
run: echo ${{ secrets.GITHUB_TOKEN }} | docker login -u ${{ github.actor }} --password-stdin ghcr.io
- name: Docker buildx build and push (AMD64)
run: |
IMAGE_ID=${{ needs.generate-tags.outputs.image-id }}
# Create a set of --tag arguments with the -amd64 suffix
ARCH_TAGS=$(for tag in ${{ needs.generate-tags.outputs.tags }}; do echo "--tag $IMAGE_ID:$tag-amd64"; done)
docker buildx build \
--platform linux/amd64 \
$ARCH_TAGS \
--push \
--no-cache \
.
# Build and push ARM64 image on ARM64 runner
build-arm64:
needs: generate-tags
runs-on: ubuntu-24.04-arm # ARM64 runner for native ARM64 builds
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Login to GHCR
run: echo ${{ secrets.GITHUB_TOKEN }} | docker login -u ${{ github.actor }} --password-stdin ghcr.io
- name: Docker buildx build and push (ARM64)
run: |
IMAGE_ID=${{ needs.generate-tags.outputs.image-id }}
# Create a set of --tag arguments with the -arm64 suffix
ARCH_TAGS=$(for tag in ${{ needs.generate-tags.outputs.tags }}; do echo "--tag $IMAGE_ID:$tag-arm64"; done)
docker buildx build \
--platform linux/arm64 \
$ARCH_TAGS \
--push \
--no-cache \
.
# Create and push multi-arch manifest
publish-manifest:
needs: [build-amd64, build-arm64] # Runs after both builds are successful
runs-on: ubuntu-latest
steps:
- name: Login to GHCR
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Create and push manifest list
run: |
IMAGE_ID=${{ needs.generate-tags.outputs.image-id }}
for tag in ${{ needs.generate-tags.outputs.tags }}; do
echo "Creating manifest for $IMAGE_ID:$tag"
docker manifest create $IMAGE_ID:$tag \
--amend $IMAGE_ID:$tag-amd64 \
--amend $IMAGE_ID:$tag-arm64
echo "Pushing manifest for $IMAGE_ID:$tag"
docker manifest push $IMAGE_ID:$tag
done