Skip to content

Commit 194c86b

Browse files
caarlos0Copilot
andcommitted
ci: use a GitHub App token to rebuild dist on dependabot PRs
Replaces GH_PAT (a broad org PAT) with a GitHub App token for pushing the rebuilt dist/ back to Dependabot PR branches. An App token is scoped to this repo with minimal permissions and is short-lived, so it is much safer to expose on the (semi-trusted) Dependabot PR build than a wide PAT. The job stays a no-op until the DIST_REBUILD_APP_ID and DIST_REBUILD_APP_PRIVATE_KEY Dependabot secrets are configured. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>
1 parent d2d17a6 commit 194c86b

1 file changed

Lines changed: 25 additions & 15 deletions

File tree

.github/workflows/rebuild-dist.yml

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,45 +17,55 @@ jobs:
1717
# the validate workflow stays green.
1818
#
1919
# Dependabot runs get a read-only GITHUB_TOKEN, and commits pushed with it do
20-
# not re-trigger checks. Pushing the dist commit therefore uses GH_PAT, which
21-
# can re-run workflows. Note: Dependabot runs only expose Dependabot secrets,
22-
# so GH_PAT must exist as a Dependabot secret (org or repo) with contents:write
23-
# on this repo. Until it does this job is a no-op.
20+
# not re-trigger checks. Pushing the dist commit therefore uses a GitHub App
21+
# token, which is repo-scoped and short-lived, and can re-run workflows.
22+
# Configure a GitHub App with contents:write on this repo and set its
23+
# credentials as Dependabot secrets named DIST_REBUILD_APP_ID and
24+
# DIST_REBUILD_APP_PRIVATE_KEY (Dependabot runs only expose Dependabot
25+
# secrets). Until both exist this job is a no-op.
2426
rebuild-dist:
2527
if: github.actor == 'dependabot[bot]'
2628
runs-on: ubuntu-latest
2729
steps:
28-
- name: Check token
29-
id: token
30+
- name: Check app credentials
31+
id: app
3032
env:
31-
GH_PAT: ${{ secrets.GH_PAT }}
33+
DIST_REBUILD_APP_ID: ${{ secrets.DIST_REBUILD_APP_ID }}
34+
DIST_REBUILD_APP_PRIVATE_KEY: ${{ secrets.DIST_REBUILD_APP_PRIVATE_KEY }}
3235
run: |
33-
if [ -n "$GH_PAT" ]; then
36+
if [ -n "$DIST_REBUILD_APP_ID" ] && [ -n "$DIST_REBUILD_APP_PRIVATE_KEY" ]; then
3437
echo "available=true" >> "$GITHUB_OUTPUT"
3538
else
3639
echo "available=false" >> "$GITHUB_OUTPUT"
37-
echo "::notice::GH_PAT Dependabot secret is not set; skipping automatic dist rebuild."
40+
echo "::notice::DIST_REBUILD_APP_ID/DIST_REBUILD_APP_PRIVATE_KEY Dependabot secrets are not set; skipping automatic dist rebuild."
3841
fi
42+
- name: Generate token
43+
if: steps.app.outputs.available == 'true'
44+
id: token
45+
uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3.2.0
46+
with:
47+
app-id: ${{ secrets.DIST_REBUILD_APP_ID }}
48+
private-key: ${{ secrets.DIST_REBUILD_APP_PRIVATE_KEY }}
3949
- name: Checkout
40-
if: steps.token.outputs.available == 'true'
50+
if: steps.app.outputs.available == 'true'
4151
uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6.0.3
4252
with:
4353
ref: ${{ github.head_ref }}
44-
token: ${{ secrets.GH_PAT }}
54+
token: ${{ steps.token.outputs.token }}
4555
- name: Setup Node.js
46-
if: steps.token.outputs.available == 'true'
56+
if: steps.app.outputs.available == 'true'
4757
uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e # v6.4.0
4858
with:
4959
node-version-file: '.node-version'
5060
cache: npm
5161
- name: Install dependencies
52-
if: steps.token.outputs.available == 'true'
62+
if: steps.app.outputs.available == 'true'
5363
run: npm ci --ignore-scripts
5464
- name: Rebuild dist
55-
if: steps.token.outputs.available == 'true'
65+
if: steps.app.outputs.available == 'true'
5666
run: npm run build
5767
- name: Commit and push dist if changed
58-
if: steps.token.outputs.available == 'true'
68+
if: steps.app.outputs.available == 'true'
5969
env:
6070
HEAD_REF: ${{ github.head_ref }}
6171
run: |

0 commit comments

Comments
 (0)