-
Notifications
You must be signed in to change notification settings - Fork 60
199 lines (166 loc) · 7.8 KB
/
docker-build.yml
File metadata and controls
199 lines (166 loc) · 7.8 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
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:
registry-tags: ${{ steps.generate.outputs.REGISTRY_TAGS }}
image-id: ${{ steps.generate.outputs.IMAGE_ID }}
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Generate tags
id: generate
run: |
# Image ID
IMAGE_ID=ghcr.io/$IMAGE_SLUG
# Date
BDATE=$(date +%Y%m%d)
# PATCH_VERSION from package.json is like "1.2.3"
# MINOR_VERSION like "1.2", MAJOR_VERSION like "1"
PATCH_VERSION=$(jq .version -r ./package.json)
MINOR_VERSION=${PATCH_VERSION%.*}
MAJOR_VERSION=${MINOR_VERSION%.*}
# Change all uppercase to lowercase, just in case
IMAGE_ID=$(echo $IMAGE_ID | tr '[A-Z]' '[a-z]')
# Strip git ref prefix from version and use it as suffix for version
VERSION=$(echo "${{ github.ref }}" | sed -e 's,.*/\(.*\),\1,')
# For pull requests just extract the PR number
PR_NUMBER=""
[ "${{ github.event_name }}" == "pull_request" ] && VERSION=$(echo "${{ github.ref }}" | sed -e 's,.*/\(.*\)/merge,\1,')
[ "${{ github.event_name }}" == "pull_request" ] && PR_NUMBER=$VERSION
# Append version
[ "${{ github.event_name }}" == "pull_request" ] && VERSION=$PATCH_VERSION-$BDATE-$VERSION
# Strip "v" prefix from tag name if it's a tag
# [[ "${{ github.ref }}" == "refs/tags/"* ]] && VERSION=$(echo $VERSION | sed -e 's/^v//')
# Use Docker `latest` tag convention if it's a master branch build
[ "$VERSION" == "master" ] && VERSION=latest
[ "$VERSION" == "development" ] && VERSION=development
[ "${{ github.event_name }}" == "release" ] && VERSION=release
# Compose REGISTRY_TAGS variable for buildx (space-separated with --tag flags)
REGISTRY_TAGS="--tag $IMAGE_ID:$VERSION"
# For master branch also supply an extra tag: <PATCH_VERSION>-latest,<MINOR_VERSION>-latest,<MAJOR_VERSION>-latest,<PATCH_VERSION>-<DATE>-<SHA>
[ "$VERSION" == "latest" ] && REGISTRY_TAGS="$REGISTRY_TAGS --tag $IMAGE_ID:$PATCH_VERSION-latest --tag $IMAGE_ID:$MINOR_VERSION-latest --tag $IMAGE_ID:$MAJOR_VERSION-latest --tag $IMAGE_ID:$PATCH_VERSION-$BDATE-$(git rev-parse --short HEAD)"
[ "$VERSION" == "development" ] && REGISTRY_TAGS="$REGISTRY_TAGS --tag $IMAGE_ID:$MAJOR_VERSION-development --tag $IMAGE_ID:$PATCH_VERSION-$BDATE-$(git rev-parse --short HEAD)"
[ "$VERSION" == "release" ] && REGISTRY_TAGS="$REGISTRY_TAGS --tag $IMAGE_ID:$PATCH_VERSION-release --tag $IMAGE_ID:$MINOR_VERSION-release --tag $IMAGE_ID:$MAJOR_VERSION-release --tag $IMAGE_ID:$PATCH_VERSION-$BDATE-$(git rev-parse --short HEAD)"
echo IMAGE_ID=$IMAGE_ID
echo VERSION=$VERSION
echo REGISTRY_TAGS=$REGISTRY_TAGS
echo headref=${{ github.head_ref }}
SHA_SHORT=${{ github.sha }}
[ "${{ github.event_name }}" == "pull_request" ] && SHA_SHORT=$(echo ${{ github.event.pull_request.head.sha }} | cut -c1-8)
echo "Final image tags to be pushed:"
echo $REGISTRY_TAGS
echo "REGISTRY_TAGS=$REGISTRY_TAGS" >> $GITHUB_OUTPUT
echo "IMAGE_ID=$IMAGE_ID" >> $GITHUB_OUTPUT
echo "REGISTRY_TAGS_VERSION=$VERSION" >> $GITHUB_OUTPUT
echo "REGISTRY_TAGS_PR_NUMBER=$PR_NUMBER" >> $GITHUB_OUTPUT
echo "SHA_SHORT=$SHA_SHORT" >> $GITHUB_OUTPUT
# 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: Login to GHCR
uses: docker/login-action@184bdaa0721073962dff0199f1fb9940f07167d1
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
with:
driver-opts: |
image=moby/buildkit:latest
- name: Docker buildx build and push (ARM64)
env:
DOCKER_BUILDKIT: 1
run: |
# Generate ARM64-specific tags by adding -arm64 suffix
ARM64_TAGS="${{ needs.generate-tags.outputs.registry-tags }}-arm64"
# Only disable cache for releases
CACHE_FLAG=""
if [ "${{ github.event_name }}" = "release" ]; then
CACHE_FLAG="--no-cache"
fi
docker buildx build \
${ARM64_TAGS} \
--push \
--platform linux/arm64 \
--cache-from type=registry,ref=${{ needs.generate-tags.outputs.image-id }}:buildcache-arm64 \
--cache-to type=registry,ref=${{ needs.generate-tags.outputs.image-id }}:buildcache-arm64,mode=max \
--build-arg PUBLIC_URL_ARG=${{ secrets.PUBLIC_URL }} \
${CACHE_FLAG} \
.
# Build and push AMD64 image on x64 runner
build-amd64:
needs: [generate-tags, build-arm64] # Build amd64 last so that it shows as the latest
runs-on: ubuntu-latest # x64 runner for native AMD64 builds
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Login to GHCR
uses: docker/login-action@184bdaa0721073962dff0199f1fb9940f07167d1
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
with:
driver-opts: |
image=moby/buildkit:latest
- name: Docker buildx build and push (AMD64)
env:
DOCKER_BUILDKIT: 1
run: |
# Generate AMD64-specific tags by adding -amd64 suffix
AMD64_TAGS="${{ needs.generate-tags.outputs.registry-tags }}-amd64"
# Only disable cache for releases
CACHE_FLAG=""
if [ "${{ github.event_name }}" = "release" ]; then
CACHE_FLAG="--no-cache"
fi
docker buildx build \
${AMD64_TAGS} \
--push \
--platform linux/amd64 \
--cache-from type=registry,ref=${{ needs.generate-tags.outputs.image-id }}:buildcache-amd64 \
--cache-to type=registry,ref=${{ needs.generate-tags.outputs.image-id }}:buildcache-amd64,mode=max \
--build-arg PUBLIC_URL_ARG=${{ secrets.PUBLIC_URL }} \
${CACHE_FLAG} \
.