Repository navigation
136 lines (127 loc) · 6.72 KB
/
Copy pathphp-upstream.yaml
File metadata and controls
136 lines (127 loc) · 6.72 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
# Starts the build as soon as the official PHP images have a new PHP release, instead of waiting for the nightly build.
# Every hour it compares PHP_VERSION of each official base image (the FROM line of docker/php/<tag>/Dockerfile)
# with PHP_VERSION of webdevops/php:<tag> and webdevops/php-nginx:<tag>. If one of them is older or does not
# exist yet, it starts build.yaml on master, unless a build of master is already running, or a build that this
# workflow started in the last hours failed, or it already started several. An image that cannot be read is skipped.
# A rebuild of the official images without a new PHP release (same PHP_VERSION) does not start a build; the nightly
# build picks that up.
name: php-upstream
'on':
schedule:
- cron: '17 * * * *'
workflow_dispatch:
inputs:
dry-run:
description: 'Only report outdated tags, do not start a build'
type: boolean
default: false
permissions:
contents: read
actions: write
concurrency:
group: php-upstream
cancel-in-progress: false
jobs:
check:
name: 'Compare PHP versions with the official images'
runs-on: ubuntu-latest
if: "${{ github.repository == 'webdevops/Dockerfile' }}"
steps:
- uses: actions/checkout@v7
with:
persist-credentials: false
- name: 'Login to hub.docker.com'
env:
DOCKERHUB_USERNAME: '${{ secrets.DOCKERHUB_USERNAME }}'
DOCKERHUB_TOKEN: '${{ secrets.DOCKERHUB_TOKEN }}'
run: |-
set -euo pipefail
command -v skopeo >/dev/null || { sudo apt-get update -q && sudo apt-get install -y -q skopeo; }
printf '%s' "$DOCKERHUB_TOKEN" \
| skopeo login --authfile "$RUNNER_TEMP/docker-hub-auth.json" --username "$DOCKERHUB_USERNAME" --password-stdin docker.io
- name: 'Find tags whose PHP is older than the official image'
id: compare
run: |-
set -euo pipefail
skopeo_error="$RUNNER_TEMP/skopeo-error.txt"
# Prints PHP_VERSION of the image for linux/amd64. Fails if the image cannot be read; skopeo's error is
# then in $skopeo_error. --no-tags: skopeo would otherwise list every tag of the repository.
php_version() {
local image=$1
local variables
variables=$(skopeo inspect --authfile "$RUNNER_TEMP/docker-hub-auth.json" --no-tags --retry-times 3 \
--override-os linux --override-arch amd64 \
--format '{{range .Env}}{{println .}}{{end}}' "docker://docker.io/$image" 2>"$skopeo_error") || return 1
sed -n '/^PHP_VERSION=/{s///p;q;}' <<<"$variables"
}
outdated=""
for dockerfile in docker/php/*/Dockerfile; do
tag=$(basename "$(dirname "$dockerfile")")
upstream_image=$(sed -nE 's/^FROM[[:space:]]+([^[:space:]]+).*/\1/p' "$dockerfile" | head -n1)
if ! upstream=$(php_version "$upstream_image"); then
echo "::warning::Could not read $upstream_image, skipping $tag: $(tail -n1 "$skopeo_error")"
continue
elif [ -z "$upstream" ]; then
echo "::warning::$upstream_image has no PHP_VERSION, skipping $tag"
continue
fi
for image in "webdevops/php:$tag" "webdevops/php-nginx:$tag"; do
if ! ours=$(php_version "$image"); then
if grep -qi 'manifest unknown' "$skopeo_error"; then
# The tag does not exist yet, for example a new PHP version: build it
echo "::notice::$image does not exist yet, $upstream_image has PHP $upstream"
outdated="$outdated $image"
else
echo "::warning::Could not read $image, skipping it: $(tail -n1 "$skopeo_error")"
fi
elif [ "$ours" = "$upstream" ]; then
echo "$image: PHP $ours, up to date with $upstream_image"
else
echo "::notice::$image has PHP ${ours:-?}, $upstream_image has PHP $upstream"
outdated="$outdated $image"
fi
done
done
echo "outdated=${outdated# }" >> "$GITHUB_OUTPUT"
- name: 'Start build.yaml on master'
if: "${{ steps.compare.outputs.outdated != '' }}"
env:
GH_TOKEN: '${{ github.token }}'
GH_REPO: '${{ github.repository }}'
DRY_RUN: "${{ inputs.dry-run && 'true' || 'false' }}"
# Within this many hours, a build started by this workflow that did not succeed blocks the next one, so that
# a failing build is not started again every hour; so do MAX_BUILDS builds started by it, in case a build
# succeeds and still leaves an image outdated. The nightly build still runs.
COOLDOWN_HOURS: 8
MAX_BUILDS: 3
OUTDATED: '${{ steps.compare.outputs.outdated }}'
run: |-
set -euo pipefail
# --branch matches the head branch, so a pull request from the master branch of a fork would count as well
running=$(gh run list --workflow build.yaml --branch master --limit 20 --json status,event \
--jq '[.[] | select(.status != "completed" and .event != "pull_request")] | length')
if [ "$running" != "0" ]; then
echo "A build of master is already running or queued; the next check looks again."
exit 0
fi
# Conclusions of the builds that this workflow started in the last COOLDOWN_HOURS: runs started with the
# token of a run have the actor github-actions[bot]; builds started by hand do not count.
since=$(date -u -d "-${COOLDOWN_HOURS} hours" +%Y-%m-%dT%H:%M:%SZ)
conclusions=$(gh api "repos/$GH_REPO/actions/workflows/build.yaml/runs?branch=master&event=workflow_dispatch&per_page=20" \
--jq "[.workflow_runs[] | select(.actor.login == \"github-actions[bot]\" and .created_at > \"$since\") | .conclusion]")
failed=$(jq '[.[] | select(. != "success")] | length' <<<"$conclusions")
started=$(jq 'length' <<<"$conclusions")
if [ "$failed" != "0" ]; then
echo "::warning::A build that this workflow started in the last ${COOLDOWN_HOURS} hours did not succeed; not starting another one."
exit 0
fi
if [ "$started" -ge "$MAX_BUILDS" ]; then
echo "::warning::This workflow started ${started} builds in the last ${COOLDOWN_HOURS} hours and images are still outdated; not starting another one."
exit 0
fi
if [ "$DRY_RUN" = "true" ]; then
echo "Dry run: would start build.yaml on master for: $OUTDATED"
exit 0
fi
gh workflow run build.yaml --ref master
echo "::notice::Started build.yaml on master for: $OUTDATED"