Skip to content

Commit 2e85c12

Browse files
grandizzyAmp
andauthored
Improve CI workflow (#451)
Co-authored-by: Amp <amp@ampcode.com>
1 parent 03998da commit 2e85c12

4 files changed

Lines changed: 49 additions & 25 deletions

File tree

.github/workflows/build.yml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,6 @@ jobs:
9393
with:
9494
key: ${{ matrix.build.target }}
9595
save-if: false
96-
- name: Configure git auth
97-
run: git config --global url."https://x-access-token:${{ secrets.GH_PAT }}@github.com/".insteadOf "https://github.com/"
9896
- name: Build release binary
9997
run: cargo build --locked --release --target ${{ matrix.build.target }} -p ${{ env.PACKAGE }}
10098
- run: strip target/${{ matrix.build.target }}/release/${{ env.PACKAGE }}
@@ -201,8 +199,6 @@ jobs:
201199
TAG="${RELEASE_TAG}"
202200
gh release upload "$TAG" artifacts/* --clobber
203201
204-
- name: Configure git auth
205-
run: git config --global url."https://x-access-token:${{ secrets.GH_PAT }}@github.com/".insteadOf "https://github.com/"
206202
- name: Build release signer
207203
run: cargo build --release -p tempo-sign
208204

.github/workflows/changelog-generate.yml

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ on:
1010

1111
concurrency: ${{ github.workflow }}-${{ github.event.number }}
1212

13-
permissions:
14-
contents: write
15-
pull-requests: write
13+
permissions: {}
1614

1715
jobs:
1816
generate:
1917
if: startsWith(github.event.label.name, 'changelog:')
2018
runs-on: ubuntu-latest
19+
environment: release
20+
permissions: {}
2121
steps:
2222
- name: Determine PR source
2323
id: source
@@ -45,13 +45,26 @@ jobs:
4545
fi
4646
echo "ref=$REF" >> "$GITHUB_OUTPUT"
4747
48+
- name: Mint scoped app token
49+
if: steps.source.outputs.same_repo == 'true'
50+
id: app-token
51+
uses: actions/create-github-app-token@1b10c78c7865c340bc4f6099eb2f838309f1e8c3 # v3.1.1
52+
with:
53+
app-id: ${{ secrets.RELEASE_BOT_APP_ID }}
54+
private-key: ${{ secrets.RELEASE_BOT_PRIVATE_KEY }}
55+
owner: tempoxyz
56+
repositories: wallet
57+
permission-contents: write
58+
permission-pull-requests: write
59+
permission-metadata: read
60+
4861
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
4962
if: steps.source.outputs.same_repo == 'true'
5063
with:
5164
repository: ${{ github.event.pull_request.head.repo.full_name }}
5265
ref: ${{ github.event.pull_request.head.sha }}
5366
fetch-depth: 0
54-
token: ${{ secrets.GH_PAT }}
67+
token: ${{ steps.app-token.outputs.token }}
5568
persist-credentials: false
5669

5770
- name: Fetch base branch for diff comparison
@@ -61,10 +74,6 @@ jobs:
6174
run: |
6275
git fetch origin "$BASE_REF"
6376
64-
- name: Configure git credentials for private dependencies
65-
if: steps.source.outputs.same_repo == 'true'
66-
run: git config --global url."https://x-access-token:${{ secrets.GH_PAT }}@github.com/".insteadOf "https://github.com/"
67-
6877
- name: Check for existing changelog
6978
if: steps.source.outputs.same_repo == 'true'
7079
id: existing
@@ -140,16 +149,25 @@ jobs:
140149
if: steps.source.outputs.same_repo == 'true' && steps.existing.outputs.found == 'false'
141150
env:
142151
VALIDATED_REF: ${{ steps.ref.outputs.ref }}
152+
APP_TOKEN: ${{ steps.app-token.outputs.token }}
143153
run: |
144154
set -euo pipefail
145155
git config user.name "github-actions[bot]"
146156
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
147157
git add .changelog/
148158
git commit -m "chore: add changelog"
149-
git push origin "HEAD:${VALIDATED_REF}"
159+
git push "https://x-access-token:${APP_TOKEN}@github.com/${GITHUB_REPOSITORY}.git" "HEAD:${VALIDATED_REF}"
150160
161+
pr-feedback:
162+
name: PR feedback
163+
needs: generate
164+
if: always() && startsWith(github.event.label.name, 'changelog:')
165+
runs-on: ubuntu-latest
166+
permissions:
167+
pull-requests: write
168+
steps:
151169
- name: Comment for fork PRs
152-
if: steps.source.outputs.same_repo != 'true'
170+
if: github.event.pull_request.head.repo.full_name != github.repository
153171
env:
154172
GH_TOKEN: ${{ github.token }}
155173
PR_NUMBER: ${{ github.event.number }}

.github/workflows/release.yml

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,35 +16,43 @@ env:
1616
jobs:
1717
release:
1818
runs-on: ubuntu-latest
19-
permissions:
20-
contents: write
21-
pull-requests: write
19+
environment: release
20+
permissions: {}
2221
steps:
22+
- name: Mint scoped app token
23+
id: app-token
24+
uses: actions/create-github-app-token@1b10c78c7865c340bc4f6099eb2f838309f1e8c3 # v3.1.1
25+
with:
26+
app-id: ${{ secrets.RELEASE_BOT_APP_ID }}
27+
private-key: ${{ secrets.RELEASE_BOT_PRIVATE_KEY }}
28+
owner: tempoxyz
29+
repositories: wallet
30+
permission-contents: write
31+
permission-pull-requests: write
32+
permission-metadata: read
33+
2334
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
2435
with:
2536
fetch-depth: 0
26-
token: ${{ secrets.GH_PAT }}
37+
token: ${{ steps.app-token.outputs.token }}
2738
persist-credentials: false
2839

29-
- name: Configure git auth
30-
run: git config --global url."https://x-access-token:${{ secrets.GH_PAT }}@github.com/".insteadOf "https://github.com/"
31-
3240
- uses: tempoxyz/changelogs@54f693643e1bd8469293bdfcbfb647bceb559490 # changelogs@0.6.3 + unified PR title + install from source
3341
id: changelogs
3442
with:
3543
conventional-commit: true
36-
github-token: ${{ github.token }}
44+
github-token: ${{ steps.app-token.outputs.token }}
3745

3846
- name: Update Cargo.lock on release PR
3947
if: steps.changelogs.outputs.pullRequestNumber != ''
4048
env:
41-
GH_TOKEN: ${{ secrets.GH_PAT }}
49+
APP_TOKEN: ${{ steps.app-token.outputs.token }}
4250
run: |
4351
git fetch origin changelog-release/main
4452
git checkout changelog-release/main
4553
cargo update --workspace
4654
if ! git diff --quiet Cargo.lock; then
4755
git add Cargo.lock
4856
git commit -m "chore: update Cargo.lock"
49-
git push origin changelog-release/main
57+
git push "https://x-access-token:${APP_TOKEN}@github.com/${GITHUB_REPOSITORY}.git" HEAD:changelog-release/main
5058
fi

.github/workflows/workflow-validation.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,9 @@ jobs:
8080
echo "Checking pull_request_target hardening..."
8181
grep -n "pull_request_target:" .github/workflows/changelog-generate.yml >/dev/null
8282
grep -n "if: steps.source.outputs.same_repo == 'true'" .github/workflows/changelog-generate.yml >/dev/null
83-
grep -n "git push origin \"HEAD:" .github/workflows/changelog-generate.yml >/dev/null
83+
# The push must go to the validated branch ref via an authenticated URL
84+
# (App-token), since persist-credentials=false on the checkout step.
85+
grep -n "git push \"https://x-access-token:\${APP_TOKEN}@github.com/\${GITHUB_REPOSITORY}.git\" \"HEAD:" .github/workflows/changelog-generate.yml >/dev/null
8486
if grep -n "git checkout -b \$\{\{ github.event.pull_request.head.ref \}\}" .github/workflows/changelog-generate.yml; then
8587
echo "::error::Unsafe branch checkout pattern detected in changelog generation workflow."
8688
exit 1

0 commit comments

Comments
 (0)