Skip to content

Commit db05328

Browse files
committed
ci: split build and deploy workflows for security
- separate build (no secrets) from deploy (with secrets) - add github pages deployment alongside ipfs/storacha - update actions to latest versions (checkout@v5, setup-node@v5) - follow security pattern from ipfs/specs and ipfs/ipfs-docs
1 parent 8956377 commit db05328

File tree

2 files changed

+116
-19
lines changed

2 files changed

+116
-19
lines changed

.github/workflows/build.yml

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,56 @@
1-
name: Build and Deploy to IPFS
1+
# Build workflow - runs for both PRs and main branch pushes
2+
# This workflow builds the website without access to secrets
3+
# For PRs: Runs on untrusted fork code safely (using pull_request event, not pull_request_target)
4+
# For main: Builds and uploads artifacts for deployment
5+
# Artifacts are passed to the deploy workflow which has access to secrets
6+
7+
name: Build
8+
9+
permissions:
10+
contents: read
11+
212
on:
313
push:
4-
branches: [ main ]
14+
branches:
15+
- main
516
pull_request:
6-
branches: [ main ]
17+
branches:
18+
- main
719

8-
permissions:
9-
contents: read
10-
pull-requests: write
11-
statuses: write
20+
env:
21+
BUILD_PATH: 'dist'
1222

23+
concurrency:
24+
group: ${{ github.workflow }}-${{ github.ref }}
25+
cancel-in-progress: true
1326

1427
jobs:
1528
build:
1629
runs-on: ubuntu-latest
1730
steps:
18-
- uses: actions/checkout@v4
19-
- uses: actions/setup-node@v4
31+
- name: Checkout code
32+
uses: actions/checkout@v5
2033
with:
21-
node-version: 20
22-
- run: npm ci
23-
- run: npm run build
34+
# For PRs: PR head commit
35+
# For pushes: the pushed commit
36+
ref: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.sha || github.sha }}
2437

25-
- uses: ipfs/ipfs-deploy-action@v1
26-
name: Deploy to IPFS
27-
id: deploy
38+
- name: Setup Node.js
39+
uses: actions/setup-node@v5
2840
with:
29-
path-to-deploy: dist
30-
storacha-key: ${{ secrets.STORACHA_KEY }}
31-
storacha-proof: ${{ secrets.STORACHA_PROOF }}
32-
github-token: ${{ github.token }}
41+
node-version: '20'
42+
cache: 'npm'
43+
44+
- name: Install dependencies
45+
run: npm ci --prefer-offline --no-audit --progress=false
3346

47+
- name: Build project
48+
run: npm run build
49+
50+
# Upload artifact for deploy workflow
51+
- name: Upload build artifact
52+
uses: actions/upload-artifact@v4
53+
with:
54+
name: cid-utils-build-${{ github.run_id }}
55+
path: ${{ env.BUILD_PATH }}
56+
retention-days: 1

.github/workflows/deploy.yml

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Deploy workflow - triggered by workflow_run after successful build
2+
# This workflow has access to secrets but never executes untrusted code
3+
# It only downloads and deploys pre-built artifacts from the build workflow
4+
# Security: Fork code cannot access secrets as it only runs in build workflow
5+
# Deploys to IPFS/Storacha for all branches and GitHub Pages for main branch only
6+
7+
name: Deploy
8+
9+
# Explicitly declare permissions
10+
permissions:
11+
contents: read
12+
pull-requests: write
13+
statuses: write
14+
15+
on:
16+
workflow_run:
17+
workflows: ["Build"]
18+
types: [completed]
19+
20+
env:
21+
BUILD_PATH: 'cid-utils-build'
22+
23+
jobs:
24+
deploy-ipfs:
25+
if: github.event.workflow_run.conclusion == 'success'
26+
runs-on: ubuntu-latest
27+
outputs:
28+
cid: ${{ steps.deploy.outputs.cid }}
29+
steps:
30+
- name: Download build artifact
31+
uses: actions/download-artifact@v4
32+
with:
33+
name: cid-utils-build-${{ github.event.workflow_run.id }}
34+
path: ${{ env.BUILD_PATH }}
35+
run-id: ${{ github.event.workflow_run.id }}
36+
github-token: ${{ github.token }}
37+
38+
- name: Deploy to IPFS/Storacha
39+
uses: ipfs/ipfs-deploy-action@v1
40+
id: deploy
41+
with:
42+
path-to-deploy: ${{ env.BUILD_PATH }}
43+
storacha-key: ${{ secrets.STORACHA_KEY }}
44+
storacha-proof: ${{ secrets.STORACHA_PROOF }}
45+
github-token: ${{ github.token }}
46+
47+
deploy-gh-pages:
48+
if: |
49+
github.event.workflow_run.conclusion == 'success' &&
50+
github.event.workflow_run.head_branch == 'main'
51+
runs-on: ubuntu-latest
52+
permissions:
53+
pages: write
54+
id-token: write
55+
environment:
56+
name: github-pages
57+
url: ${{ steps.deployment.outputs.page_url }}
58+
steps:
59+
- name: Download build artifact
60+
uses: actions/download-artifact@v4
61+
with:
62+
name: cid-utils-build-${{ github.event.workflow_run.id }}
63+
path: cid-utils-build
64+
run-id: ${{ github.event.workflow_run.id }}
65+
github-token: ${{ github.token }}
66+
67+
- name: Upload Pages artifact
68+
uses: actions/upload-pages-artifact@v3
69+
with:
70+
path: cid-utils-build
71+
72+
- name: Deploy to GitHub Pages
73+
id: deployment
74+
uses: actions/deploy-pages@v4

0 commit comments

Comments
 (0)