forked from CanastaWiki/Canasta
-
Notifications
You must be signed in to change notification settings - Fork 1
288 lines (254 loc) · 9.76 KB
/
docker-image.yml
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
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_taqasta_main
on:
push:
# Only activate for `master` branch
branches:
- master
# Plus for all tags
tags:
- '*'
# Plus for any pull-requests
pull_request:
env:
IMAGE_NAME: taqasta
jobs:
# Lint the image Dockerfile syntax using https://github.com/replicatedhq/dockerfilelint
test:
runs-on: ubuntu-latest
if: github.event_name == 'push' || github.event_name == 'pull_request'
steps:
- uses: actions/checkout@v3
-
name: Run linter (hadolint)
uses: vedmaka/hadolint-action@master
with:
dockerfile: "Dockerfile"
config: "hadolint.yaml"
# Generate image tags
# The image tag pattern is:
# for pull-requests: <MW_CORE_VERSION>-<DATE>-<PR_NUMBER>, eg: 1.35.2-20210125-25
# for tags: <TAG>
# for `master` branch: latest + <MW_VERSION>-latest + <MW_CORE_VERSION>-<DATE>-<SHA>
# <MW_CORE_VERSION> being parsed from the Dockerfile
tags:
needs: [ test ]
runs-on: ubuntu-latest
if: github.event_name == 'push' || github.event_name == 'pull_request'
outputs:
REGISTRY_TAGS: ${{ steps.generate.outputs.REGISTRY_TAGS }}
REGISTRY_TAGS_VERSION: ${{ steps.generate.outputs.REGISTRY_TAGS_VERSION }}
REGISTRY_TAGS_PR_NUMBER: ${{ steps.generate.outputs.REGISTRY_TAGS_PR_NUMBER }}
SHA_SHORT: ${{ steps.generate.outputs.SHA_SHORT }}
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Generate tags
id: generate
run: |
# Image ID
IMAGE_ID=ghcr.io/wikiteq/$IMAGE_NAME
# Date
BDATE=$(date +%Y%m%d)
# Extract MW version from Dockerfile
MEDIAWIKI_VERSION=$(sed -nr 's/MW_CORE_VERSION\=([0-9\.]+)/\1/p' Dockerfile | sed "s/ \\\//" | sed "s/\t//")
# Extract MW major version (like 1.35)
MEDIAWIKI_MAJOR_VERSION=${MEDIAWIKI_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,')
# Get the Taqasta version from the "VERSION" file
TAQASTA_VERSION=$(cat VERSION)
# 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=$MEDIAWIKI_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
# Compose REGISTRY_TAGS variable
REGISTRY_TAGS=$IMAGE_ID:$VERSION
# For master branch also supply an extra tag: <MW_VERSION>-latest,<MW_VERSION>-<DATE>-<SHA>
[ "$VERSION" == "latest" ] && REGISTRY_TAGS=$REGISTRY_TAGS,$IMAGE_ID:$TAQASTA_VERSION,$IMAGE_ID:$MEDIAWIKI_MAJOR_VERSION-latest,$IMAGE_ID:$MEDIAWIKI_VERSION-latest,$IMAGE_ID:$MEDIAWIKI_VERSION-$BDATE-$(git rev-parse --short HEAD)
SHA_SHORT=${{ github.sha }}
[ "${{ github.event_name }}" == "pull_request" ] && SHA_SHORT=$(echo ${{ github.event.pull_request.head.sha }} | cut -c1-8)
echo IMAGE_ID=$IMAGE_ID
echo VERSION=$VERSION
echo REGISTRY_TAGS=$REGISTRY_TAGS
echo SHA_SHORT=$SHA_SHORT
echo EventName=${{ github.event_name }}
echo headref=${{ github.head_ref }}
echo "Final image tag to be pushed:"
echo $REGISTRY_TAGS
echo "REGISTRY_TAGS=$REGISTRY_TAGS" >> $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
# Pre-build the image, no push
build:
needs: [tags]
runs-on: ubuntu-latest
# services:
# registry:
# image: registry:2
# ports:
# - 5000:5000
if: github.event_name == 'push' || github.event_name == 'pull_request'
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Set up QEMU
uses: docker/setup-qemu-action@v2
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
with:
install: true
- name: Cache Docker layers
uses: actions/cache@v3
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-buildx-${{ github.sha }}
restore-keys: |
${{ runner.os }}-buildx-
- name: Build and push
id: docker_build
uses: docker/build-push-action@v3
with:
context: .
platforms: linux/amd64, linux/arm64
secrets: |
COMPOSER_TOKEN=${{ secrets.GITHUB_TOKEN }}
push: false
tags: ${{ needs.tags.outputs.REGISTRY_TAGS }}
cache-from: type=gha
cache-to: type=gha,mode=max
# Deploy via Compose
deploy-e2e:
needs: [build]
timeout-minutes: 30
runs-on: ubuntu-latest
if: github.event_name == 'push' || github.event_name == 'pull_request'
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
with:
install: true
- name: Cache Docker layers
uses: actions/cache@v3
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-buildx-${{ github.sha }}
restore-keys: |
${{ runner.os }}-buildx-
- name: Setup Docker Compose
uses: yu-ichiro/spin-up-docker-compose-action@main
env:
COMPOSER_TOKEN: ${{ secrets.GITHUB_TOKEN }}
DOCKER_BUILDKIT: 1
COMPOSE_DOCKER_CLI_BUILD: 1
with:
file: docker-compose.yml
cache-key: ${{ runner.os }}-buildx-${{ github.sha }}
shared: true
# registry: true
- name: Give containers a moment to init
shell: bash
run: sleep 30s
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 18
- name: Install dependencies
working-directory: ./e2e
run: npm ci
- name: Install Playwright Browsers
working-directory: ./e2e
run: npx playwright install chromium --with-deps
- name: Run Playwright tests
working-directory: ./e2e
run: npx playwright test
- uses: actions/upload-artifact@v3
if: always()
with:
name: playwright-report
path: e2e/playwright-report/
retention-days: 10
# Builds from cache & push the image to GitHub registry this time
push:
needs: [tags,deploy-e2e]
runs-on: ubuntu-latest
outputs:
digest: ${{ steps.docker_build.outputs.digest }}
imageid: ${{ steps.docker_build.outputs.imageid }}
if: github.event_name == 'push' || github.event_name == 'pull_request'
steps:
- name: Checkout
uses: actions/checkout@v3
-
name: Set up QEMU
uses: docker/setup-qemu-action@v2
-
name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
with:
install: true
-
name: Cache Docker layers
uses: actions/cache@v3
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-buildx-${{ github.sha }}
restore-keys: |
${{ runner.os }}-buildx-
-
name: Login to GitHub Container Registry
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
-
name: Build and push
id: docker_build
uses: docker/build-push-action@v3
with:
context: .
platforms: linux/amd64, linux/arm64
secrets: |
COMPOSER_TOKEN=${{ secrets.GITHUB_TOKEN }}
push: true
tags: ${{ needs.tags.outputs.REGISTRY_TAGS }}
cache-from: type=gha
cache-to: type=gha,mode=max
-
name: Image digest
run: echo ${{ steps.docker_build.outputs.digest }}
-
name: Image tags debug
run: echo ${{ needs.tags.outputs.REGISTRY_TAGS }}
# Send a message to the PR thread
notify:
needs: [tags,push]
runs-on: ubuntu-latest
if: github.event_name == 'push' || github.event_name == 'pull_request'
steps:
-
name: Notify about image tag
if: github.event_name == 'pull_request' && needs.push.outputs.digest != ''
uses: hasura/[email protected]
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
repository: ${{ github.repository }}
number: ${{ github.event.number }}
id: comment
message: ":whale: The image based on [${{ needs.tags.outputs.SHA_SHORT }}](https://github.com/WikiTeq/Taqasta/pull/${{ needs.tags.outputs.REGISTRY_TAGS_PR_NUMBER }}/commits/${{ github.event.pull_request.head.sha }}) commit has been built with `${{ needs.tags.outputs.REGISTRY_TAGS_VERSION }}` tag as [${{ needs.tags.outputs.REGISTRY_TAGS }}](https://github.com/${{ github.repository }}/pkgs/container/${{ env.IMAGE_NAME }}/${{ needs.push.outputs.imageid }}?tag=${{ needs.tags.outputs.REGISTRY_TAGS_VERSION }})"
recreate: true
fail: false