Repository navigation
103 lines (97 loc) · 4.81 KB
/
Copy pathon-push.yml
File metadata and controls
103 lines (97 loc) · 4.81 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
name: Publish on push
# A commit on dev rebuilds the images whose build context changed (plus everything built on top of
# them) for every channel. Channels run one at a time to stay within the registry's pull budget.
on:
push:
branches: [dev]
permissions:
contents: write
packages: write
id-token: write
concurrency:
group: publish-push
cancel-in-progress: false
jobs:
select:
name: Affected targets
runs-on: ubuntu-26.04
# This job reads this workflow's own run history, which the workflow-level block does not
# grant - and a job-level block replaces it, so the checkout's contents: read is restated
# while the write access this job never needed is dropped. The lookup happens to succeed
# today because the repository is public and its Actions API is readable anonymously; that
# is a property of the repository, not of the permissions, and it would stop being true the
# day the repository turned private.
permissions:
actions: read
contents: read
outputs:
targets: ${{ steps.sel.outputs.targets }}
count: ${{ steps.sel.outputs.count }}
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
with:
fetch-depth: 0
- uses: docker/setup-buildx-action@f87e5991a6d7451dcb8d9637bfbc97413f497069 # v4
- id: sel
env:
BEFORE: ${{ github.event.before }}
GH_TOKEN: ${{ github.token }}
HEAD: ${{ github.sha }}
run: |
# Diff from the last commit this workflow actually published, not from this push's own
# parent. Only one run may sit pending in a concurrency group, so when several merges land
# minutes apart the middle ones are cancelled - and the next push's github.event.before is
# the cancelled push's head, so whatever that push would have rebuilt is skipped for good
# rather than retried. That is not hypothetical: the sibling repository shipped a
# base/Dockerfile change that stayed unpublished for two days for exactly this reason.
# "There is no previous successful publish" and "we could not find out" are different
# answers and must not share a branch. The first is normal on a new branch. The second
# used to be swallowed by `|| true`, which fell back to this push's parent - the exact
# base that drops a cancelled run's work - while the log said nothing and the job
# stayed green. Failing the lookup now builds everything: wasteful, visible, and it
# never drops a publish.
if last=$(gh run list --workflow on-push.yml --branch "$GITHUB_REF_NAME" \
--status success --limit 1 --json headSha -q '.[0].headSha' 2>/dev/null); then
if [ -z "$last" ]; then
echo "no previous successful publish on this branch; diffing from this push's parent"
elif git cat-file -e "$last" 2> /dev/null; then
echo "diffing from the last successful publish: $last"
BEFORE="$last"
else
# A third answer, and not the same as having none: a publish happened and this
# clone cannot see the commit, which a rewritten branch produces. Diffing from the
# push parent here would skip whatever that publish did not cover, which is the
# defect this whole step exists to avoid.
echo "::warning::the last successful publish ($last) is not in this clone, so what it covered cannot be determined; building every target rather than diffing from this push's parent"
BEFORE=""
fi
else
echo "::warning::could not read this workflow's run history; building every target rather than diffing from this push's parent, which drops work when a run is cancelled"
BEFORE=""
fi
if [ "$BEFORE" = "0000000000000000000000000000000000000000" ] || ! git cat-file -e "$BEFORE" 2> /dev/null; then
echo "no usable base commit -> building everything"
files="docker-bake.hcl"
else
files=$(git diff --name-only "$BEFORE" "$HEAD")
fi
printf '%s\n' "$files"
targets=$(printf '%s\n' "$files" | python3 scripts/affected.py paths)
echo "targets: $targets"
echo "targets=$(echo "$targets" | jq -r 'join(" ")')" >> "$GITHUB_OUTPUT"
echo "count=$(echo "$targets" | jq length)" >> "$GITHUB_OUTPUT"
publish:
name: ${{ matrix.channel }}
needs: select
if: needs.select.outputs.count != '0'
strategy:
fail-fast: false
max-parallel: 1
matrix:
channel: [alpha, testing, stable]
uses: ./.github/workflows/build-images.yml
with:
channel: ${{ matrix.channel }}
targets: ${{ needs.select.outputs.targets }}
push: true
secrets: inherit