Skip to content

Commit d9c5fa7

Browse files
committed
New workflow to trigger automatic image builds
This is executed daily and performs various checks: - Is there a newer timezonedb extension available. - (more coming soon). And, if there is any of the checks happening, it automatically dispatches a reposity_dispatch (auto-build-triggered) event, causing the test and build workflow to be triggered. Now the test & build workflow supports workflow_call events That way we can make other workflows in the repository to, conditionally, launch a rebuild when some conditions are met: - When there is a new version of an extension. - When there is a new PHP release. - After X days - ... Note this is related to https://tracker.moodle.org/browse/MDL-76675 Also, update some actions to use new nodejs versions and avoid some deprecation warnings.
1 parent 59fe7eb commit d9c5fa7

File tree

2 files changed

+100
-10
lines changed

2 files changed

+100
-10
lines changed

.github/workflows/test_buildx_and_publish.yml

+14-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
name: Test and publish
22

3-
on: [push, pull_request, workflow_dispatch]
3+
on:
4+
push:
5+
pull_request:
6+
workflow_dispatch:
7+
workflow_call:
48

59
env:
610
REPOSITORY: moodle-php-apache
@@ -12,7 +16,7 @@ jobs:
1216
runs-on: ubuntu-latest
1317
steps:
1418
- name: Checkout
15-
uses: actions/checkout@v2
19+
uses: actions/checkout@v3
1620

1721
- name: Build image
1822
run: |
@@ -35,18 +39,18 @@ jobs:
3539
3640
Publish:
3741
# Completely avoid forks and pull requests to try this job.
38-
if: github.repository_owner == 'moodlehq' && contains(fromJson('["push", "workflow_dispatch"]'), github.event_name)
42+
if: github.repository_owner == 'moodlehq' && contains(fromJson('["push", "workflow_dispatch", "workflow_call"]'), github.event_name)
3943
# Requires Test to pass
4044
needs: Test
4145
runs-on: ubuntu-latest
4246
steps:
4347
- name: Checkout
44-
uses: actions/checkout@v2
48+
uses: actions/checkout@v3
4549

4650
# Calculate the tags to be pussed to the registries.
4751
- name: Calculate image tag names
4852
id: calculatetags
49-
uses: docker/metadata-action@v3
53+
uses: docker/metadata-action@v4
5054
with:
5155
images: |
5256
${{ env.DOCKERHUB_OWNER }}/${{ env.REPOSITORY }}
@@ -58,30 +62,30 @@ jobs:
5862
5963
# https://github.com/docker/setup-qemu-action#usage
6064
- name: Set up QEMU
61-
uses: docker/setup-qemu-action@v1
65+
uses: docker/setup-qemu-action@v2
6266

6367
# https://github.com/marketplace/actions/docker-setup-buildx
6468
- name: Set up Docker Buildx
65-
uses: docker/setup-buildx-action@v1
69+
uses: docker/setup-buildx-action@v2
6670

6771
# https://github.com/docker/login-action#docker-hub
6872
- name: Login to Docker Hub
69-
uses: docker/login-action@v1
73+
uses: docker/login-action@v2
7074
with:
7175
username: ${{ secrets.DOCKERHUB_USERNAME }}
7276
password: ${{ secrets.DOCKERHUB_TOKEN }}
7377

7478
# https://github.com/docker/login-action#github-container-registry
7579
- name: Login to GitHub Container Registry
76-
uses: docker/login-action@v1
80+
uses: docker/login-action@v2
7781
with:
7882
registry: ghcr.io
7983
username: ${{ secrets.GH_USERNAME }}
8084
password: ${{ secrets.GITHUB_TOKEN }}
8185

8286
# https://github.com/docker/build-push-action#multi-platform-image
8387
- name: Build and push to Docker Hub and Github registries
84-
uses: docker/build-push-action@v2
88+
uses: docker/build-push-action@v3
8589
with:
8690
context: .
8791
file: Dockerfile
+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
name: Trigger new builds based on various criteria.
2+
3+
# If any of the criteria happens, they set the trigger_build
4+
# output variable to 'true' and the rebuild will happen.
5+
6+
on:
7+
workflow_dispatch:
8+
schedule:
9+
# Fridays 16:00 UTC
10+
- cron: '10 16 * * 5'
11+
12+
jobs:
13+
14+
# This job compares the currently used timezonedb extension version
15+
# in the docker images with the latest timezonedb release (tag) available
16+
# @ https://github.com/php/pecl-datetime-timezonedb repository.
17+
# If different, a rebuilt will be triggered.
18+
datetimedb-new-release:
19+
# Completely avoid forks and pull requests to try this job.
20+
if: github.repository_owner == 'moodlehq' && contains(fromJson('["workflow_dispatch", "schedule"]'), github.event_name)
21+
runs-on: ubuntu-latest
22+
23+
outputs:
24+
trigger_build: ${{ steps.calculate.outputs.result }}
25+
26+
steps:
27+
28+
- name: Configuring git vars
29+
uses: rlespinasse/github-slug-action@v4
30+
31+
- name: Compare current and latest datetimedb versions
32+
id: calculate
33+
run: |
34+
# Calculate current image version
35+
# If the branch has has X.Y-xxxxx format, use it as docker tag. Else, use "dev" image (master branch).
36+
tag=dev
37+
if [[ ${{ env.GITHUB_REF_SLUG }} =~ \d+\.\d+\-\w+ ]]; then
38+
tag=${{ env.GITHUB_REF_SLUG }}
39+
fi
40+
echo "LOG: docker tag: $tag"
41+
42+
# Extract the timezonedb version from the image.
43+
current=$(docker run -t --rm moodlehq/moodle-php-apache:$tag php -r 'echo timezone_version_get();')
44+
echo "LOG: current: $current"
45+
46+
# Look for the latest tag available @ https://github.com/php/pecl-datetime-timezonedb
47+
latest=$(curl -s "https://api.github.com/repos/php/pecl-datetime-timezonedb/tags" | jq -r '.[0].name')
48+
echo "LOG: latest: $latest"
49+
50+
# Compare the versions (digits only), if current < latest, then we need to rebuild.
51+
if [[ ${current//[!0-9]/} -lt ${latest//[!0-9]/} ]]; then
52+
echo "result=true" >> $GITHUB_OUTPUT
53+
echo "LOG: timezonedb to trigger image build"
54+
fi
55+
56+
# This job gets the results of all the jobs in the workflow and,
57+
# if any of them has ended with the "trigger_build" output set, then
58+
# will set its own (final) trigger_build output to 'true'.
59+
evaluate:
60+
# Completely avoid forks and pull requests to try this job.
61+
if: github.repository_owner == 'moodlehq' && contains(fromJson('["workflow_dispatch", "schedule"]'), github.event_name)
62+
runs-on: ubuntu-latest
63+
needs: [datetimedb-new-release]
64+
65+
outputs:
66+
trigger_build: ${{ steps.evaluate.outputs.trigger }}
67+
68+
steps:
69+
70+
- name: Evaluate if we have to trigger a build
71+
id: evaluate
72+
run: |
73+
# Add here more conditions (ORed) when new criteria are added.
74+
if [[ ${{ needs.datetimedb-new-release.outputs.trigger_build }} ]]; then
75+
echo "trigger=true" >> $GITHUB_OUTPUT
76+
echo "LOG: Final evaluation, trigger the build"
77+
fi
78+
79+
Build:
80+
# Only if the final workflow.outputs.trigger_build from evaluate job has decided to build.
81+
if: ${{ needs.evaluate.outputs.trigger_build }} == 'true'
82+
needs: [evaluate]
83+
84+
# Launch the build job (as reusable workflow).
85+
uses: ./.github/workflows/test_buildx_and_publish.yml
86+
secrets: inherit

0 commit comments

Comments
 (0)