Skip to content

Commit edb3706

Browse files
ampagenttrly
andcommitted
ci: isolate metadata push credentials
Co-authored-by: Travis Lyons <travis.lyons@gmail.com>
1 parent 904a5c3 commit edb3706

2 files changed

Lines changed: 95 additions & 27 deletions

File tree

‎.github/workflows/publish.yml‎

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ jobs:
3030
runs-on: ubuntu-latest
3131
outputs:
3232
packages: ${{ steps.plan.outputs.packages }}
33+
current: ${{ steps.current.outputs.current }}
3334
steps:
3435
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
3536
with:
@@ -47,6 +48,7 @@ jobs:
4748
require-base: 'true'
4849

4950
- name: Confirm main is current
51+
id: current
5052
env:
5153
GITHUB_TOKEN: ${{ github.token }}
5254
shell: bash
@@ -55,14 +57,16 @@ jobs:
5557
5658
head_sha=$(gh api "repos/${GITHUB_REPOSITORY}/git/ref/heads/main" --jq .object.sha)
5759
if [[ "$head_sha" != "$GITHUB_SHA" ]]; then
58-
echo "Refusing to publish superseded commit $GITHUB_SHA; main is at $head_sha" >&2
59-
exit 1
60+
echo "::notice::Skipping superseded commit $GITHUB_SHA; main is at $head_sha"
61+
echo 'current=false' >> "$GITHUB_OUTPUT"
62+
exit 0
6063
fi
64+
echo 'current=true' >> "$GITHUB_OUTPUT"
6165
6266
build:
6367
name: Build (${{ matrix.package }})
6468
needs: plan
65-
if: needs.plan.outputs.packages != '[]'
69+
if: needs.plan.outputs.current == 'true' && needs.plan.outputs.packages != '[]'
6670
strategy:
6771
fail-fast: false
6872
max-parallel: 4
@@ -133,7 +137,7 @@ jobs:
133137
publish:
134138
name: Publish (${{ matrix.package }})
135139
needs: [plan, build]
136-
if: needs.plan.outputs.packages != '[]'
140+
if: needs.plan.outputs.current == 'true' && needs.plan.outputs.packages != '[]'
137141
strategy:
138142
fail-fast: false
139143
max-parallel: 4
@@ -145,13 +149,33 @@ jobs:
145149
contents: read
146150
environment: packages.trly.dev
147151
steps:
152+
# Re-checked here, not just in plan: the build runs between the two jobs,
153+
# so the plan-time check no longer covers the moment of the write.
154+
- name: Confirm main is current
155+
id: current
156+
env:
157+
GITHUB_TOKEN: ${{ github.token }}
158+
shell: bash
159+
run: |
160+
set -euo pipefail
161+
162+
head_sha=$(gh api "repos/${GITHUB_REPOSITORY}/git/ref/heads/main" --jq .object.sha)
163+
if [[ "$head_sha" != "$GITHUB_SHA" ]]; then
164+
echo "::notice::Skipping superseded commit $GITHUB_SHA; main is at $head_sha"
165+
echo 'current=false' >> "$GITHUB_OUTPUT"
166+
exit 0
167+
fi
168+
echo 'current=true' >> "$GITHUB_OUTPUT"
169+
148170
- name: Download package archive
171+
if: steps.current.outputs.current == 'true'
149172
uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2
150173
with:
151174
name: package-${{ matrix.package }}
152175
path: ${{ runner.temp }}/packages/${{ matrix.package }}
153176

154177
- name: Install pkgdepot
178+
if: steps.current.outputs.current == 'true'
155179
env:
156180
PKGDEPOT_VERSION: 0.2.1
157181
PKGDEPOT_SHA256: 441ff17351e6ab06c1e59b07353976080d570bb5359cdc3a76ea1f81dd13d7eb
@@ -168,22 +192,8 @@ jobs:
168192
tar --extract --gzip --file "$archive" --directory "$install_dir" pkgdepot
169193
printf '%s\n' "$install_dir" >> "$GITHUB_PATH"
170194
171-
# Re-checked here, not just in plan: the build runs between the two jobs,
172-
# so the plan-time check no longer covers the moment of the write.
173-
- name: Confirm main is current
174-
env:
175-
GITHUB_TOKEN: ${{ github.token }}
176-
shell: bash
177-
run: |
178-
set -euo pipefail
179-
180-
head_sha=$(gh api "repos/${GITHUB_REPOSITORY}/git/ref/heads/main" --jq .object.sha)
181-
if [[ "$head_sha" != "$GITHUB_SHA" ]]; then
182-
echo "Refusing to publish superseded commit $GITHUB_SHA; main is at $head_sha" >&2
183-
exit 1
184-
fi
185-
186195
- name: Publish package archives
196+
if: steps.current.outputs.current == 'true'
187197
env:
188198
PKGDEPOT_URL: https://packages.trly.dev
189199
PKGDEPOT_OAUTH_ISSUER: https://id.trly.dev

‎.github/workflows/update-package-metadata.yml‎

Lines changed: 66 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ jobs:
2929
runs-on: ubuntu-latest
3030
container:
3131
image: archlinux:base-devel@sha256:767de6eaa40dcfeda5263a48737ea03a782caabdf3a403b6e6a203c7fd333681
32+
outputs:
33+
created: ${{ steps.stage_updated_metadata.outputs.created }}
3234
permissions:
3335
contents: read
3436
steps:
@@ -83,27 +85,80 @@ jobs:
8385
)
8486
done
8587
86-
- name: Commit updated metadata
87-
id: commit_updated_metadata
88+
- name: Stage updated metadata
89+
id: stage_updated_metadata
8890
shell: bash
8991
run: |
9092
set -euo pipefail
9193
9294
echo 'created=false' >> "$GITHUB_OUTPUT"
93-
git config user.name 'renovate[bot]'
94-
git config user.email '29139614+renovate[bot]@users.noreply.github.com'
9595
git add -- '*/PKGBUILD' '*/.SRCINFO'
9696
if git diff --cached --quiet; then
9797
echo 'Package metadata is already current.'
9898
exit 0
9999
fi
100-
git commit -m 'chore: update package metadata'
100+
101+
while IFS= read -r path; do
102+
if [[ ! $path =~ ^[a-z0-9][a-z0-9@._+-]*/(PKGBUILD|\.SRCINFO)$ ]]; then
103+
echo "Refusing to export unexpected changed path: $path" >&2
104+
exit 1
105+
fi
106+
done < <(git diff --cached --name-only)
107+
108+
git diff --cached --binary > "$RUNNER_TEMP/metadata.patch"
101109
echo 'created=true' >> "$GITHUB_OUTPUT"
102110
111+
- name: Upload updated metadata
112+
if: steps.stage_updated_metadata.outputs.created == 'true'
113+
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
114+
with:
115+
name: package-metadata-${{ github.run_id }}
116+
path: ${{ runner.temp }}/metadata.patch
117+
if-no-files-found: error
118+
retention-days: 1
119+
120+
push:
121+
name: Commit updated metadata
122+
needs: update
123+
if: needs.update.outputs.created == 'true'
124+
runs-on: ubuntu-latest
125+
container:
126+
image: archlinux:base-devel@sha256:767de6eaa40dcfeda5263a48737ea03a782caabdf3a403b6e6a203c7fd333681
127+
permissions:
128+
contents: read
129+
steps:
130+
- name: Install tools
131+
run: pacman -Syu --noconfirm --needed git github-cli
132+
133+
- name: Check out Renovate branch
134+
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
135+
with:
136+
fetch-depth: 0
137+
ref: ${{ github.event.pull_request.head.sha }}
138+
persist-credentials: false
139+
140+
- name: Download updated metadata
141+
uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2
142+
with:
143+
name: package-metadata-${{ github.run_id }}
144+
path: ${{ runner.temp }}/metadata
145+
146+
- name: Apply updated metadata
147+
shell: bash
148+
run: |
149+
set -euo pipefail
150+
151+
git apply --index "$RUNNER_TEMP/metadata/metadata.patch"
152+
while IFS= read -r path; do
153+
if [[ ! $path =~ ^[a-z0-9][a-z0-9@._+-]*/(PKGBUILD|\.SRCINFO)$ ]]; then
154+
echo "Refusing to commit unexpected changed path: $path" >&2
155+
exit 1
156+
fi
157+
done < <(git diff --cached --name-only)
158+
103159
# GITHUB_TOKEN commits do not trigger another workflow run, so the
104-
# narrowly scoped PAT is used only for this final push.
105-
- name: Push updated metadata
106-
if: steps.commit_updated_metadata.outputs.created == 'true'
160+
# narrowly scoped PAT is used only in this isolated push job.
161+
- name: Commit and push updated metadata
107162
env:
108163
GITHUB_TOKEN: ${{ github.token }}
109164
METADATA_PUSH_TOKEN: ${{ secrets.METADATA_PUSH_TOKEN }}
@@ -113,6 +168,9 @@ jobs:
113168
run: |
114169
set -euo pipefail
115170
171+
git config user.name 'renovate[bot]'
172+
git config user.email '29139614+renovate[bot]@users.noreply.github.com'
173+
116174
current_head_sha=$(gh api \
117175
"repos/${GITHUB_REPOSITORY}/git/ref/heads/${GITHUB_HEAD_REF}" \
118176
--jq .object.sha)

0 commit comments

Comments
 (0)