Skip to content

Commit

Permalink
ci: refactor into composite action for reusability
Browse files Browse the repository at this point in the history
  • Loading branch information
2color committed Jan 30, 2025
1 parent 79a7bfe commit 92c9b8f
Show file tree
Hide file tree
Showing 2 changed files with 183 additions and 12 deletions.
150 changes: 150 additions & 0 deletions .github/actions/deploy-to-ipfs/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
name: 'Deploy to IPFS'
description: 'Deploy a directory to IPFS via Storacha, with optional Pinata and Filebase pinning'
branding:
icon: 'box'
color: 'blue'

inputs:
node-version:
description: 'Node.js version to use'
default: '20'
required: false
kubo-version:
description: 'Kubo version to use for pinning https://dist.ipfs.tech/kubo/versions'
default: 'v0.33.0'
required: false
path-to-deploy:
description: 'Path to the directory containing the frontend build to merkelize into a CAR file and deploy to IPFS'
required: true
storacha-key:
description: 'Storacha key for authentication'
required: true
storacha-proof:
description: 'Storacha proof for authentication'
required: true
pinata-pinning-url:
description: 'Pinata Pinning Service URL'
default: 'https://api.pinata.cloud/psa'
pinata-jwt-token:
description: 'Pinata JWT token for authentication'
required: false
filebase-bucket:
description: 'Filebase bucket name'
required: false
filebase-access-key:
description: 'Filebase access key'
required: false
filebase-secret-key:
description: 'Filebase secret key'
required: false
github-token:
description: 'GitHub token for updating commit status and PR comments'
required: true
set-github-status:
description: 'Set GitHub commit status and PR comments'
default: 'true'

outputs:
cid:
description: 'The IPFS CID of the uploaded content'
value: ${{ steps.merkelize.outputs.cid }}

runs:
using: "composite"
steps:
- name: Install ipfs-car
shell: bash
run: npm install -g ipfs-car@2

- name: Merkelize into CAR file
id: merkleize
shell: bash
run: |
CID=$(npx ipfs-car pack ${{ inputs.path-to-deploy }} --no-wrap --output build.car 2>&1 | tail -n 1)
echo "cid=$CID" >> "$GITHUB_OUTPUT"
echo $CID
- name: Configure and upload CAR to Storacha
shell: bash
env:
W3_PRINCIPAL: ${{ inputs.storacha-key }}
run: |
npm install -g @web3-storage/w3cli
w3 space add ${{ inputs.storacha-proof }}
if ! w3 up --car build.car; then
echo "::error::Failed to upload to IPFS"
exit 1
fi
- name: Upload CAR to Filebase
if: ${{ inputs.filebase-access-key != '' }}
shell: bash
env:
AWS_ACCESS_KEY_ID: ${{ inputs.filebase-access-key }}
AWS_SECRET_ACCESS_KEY: ${{ inputs.filebase-secret-key }}
FILEBASE_BUCKET: ${{ inputs.filebase-bucket }}
run: |
aws --endpoint https://s3.filebase.com s3 cp build.car s3://${FILEBASE_BUCKET} --metadata 'import=car'
- name: Setup Kubo
if: ${{ inputs.pinata-jwt-token != ''}}
uses: ipfs/download-ipfs-distribution-action@v1
with:
name: kubo
version: ${{ inputs.kubo-version }}

- name: Pin CID to Pinata
if: ${{ inputs.pinata-jwt-token != ''}}
shell: bash
run: |
ipfs init
ipfs pin remote service add pinata "${{ inputs.pinata-pinning-url }}" ${{ inputs.pinata-jwt-token }}
ipfs pin remote add --service=pinata --background --name="build-${{ github.sha }}" ${{ steps.merkelize.outputs.cid }}
- name: Set GitHub commit status
if: ${{ inputs.set-github-status }}
uses: actions/github-script@v7
with:
github-token: ${{ inputs.github-token }}
script: |
const cid = '${{ steps.merkelize.outputs.cid }}';
// For PR events, we need to use the head SHA
const sha = context.eventName === 'pull_request'
? context.payload.pull_request.head.sha
: context.sha;
await github.rest.repos.createCommitStatus({
owner: context.repo.owner,
repo: context.repo.repo,
sha: sha,
state: 'success',
target_url: `https://w3s.link/ipfs/${cid}`,
description: `CID: ${cid}`,
context: 'IPFS Preview'
});
- name: Find Comment to update
if: ${{ inputs.set-github-status && github.event_name == 'pull_request' }}
uses: peter-evans/find-comment@v3
id: fc
with:
issue-number: ${{ github.event.pull_request.number }}
comment-author: 'github-actions[bot]'
body-includes: '🚀 Build'
token: ${{ inputs.github-token }}

- name: Create or update comment
if: ${{ inputs.set-github-status && github.event_name == 'pull_request' }}
uses: peter-evans/create-or-update-comment@v4
with:
token: ${{ inputs.github-token }}
comment-id: ${{ steps.fc.outputs.comment-id }}
issue-number: ${{ github.event.pull_request.number }}
body: |
### 🚀 Build Preview on IPFS ready
- 🔎 Commit: ${{ github.event.pull_request.head.sha }}
- 🔏 CID `${{ steps.merkelize.outputs.cid }}`
- 📦 [Preview](https://w3s.link/ipfs/${{ steps.merkelize.outputs.cid }})
- 🔗 [Service Worker Preview](https://inbrowser.link/ipfs/${{ steps.merkelize.outputs.cid }})
edit-mode: replace
45 changes: 33 additions & 12 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,42 @@ concurrency:

jobs:
build-and-deploy:
uses: ./.github/workflows/ipfs-deploy.yml
with:
node-version: '20'
kubo-version: 'v0.33.0-rc3'
build-command: 'npm run build'
build-output-dir: 'out'
pinata: true
secrets:
STORACHA_KEY: ${{ secrets.STORACHA_KEY }}
STORACHA_PROOF: ${{ secrets.STORACHA_PROOF }}
PINATA_JWT_TOKEN: ${{ secrets.PINATA_JWT_TOKEN }}
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'npm'

- name: Install dependencies
run: npm ci

- name: Build project
run: npm run build

- name: Upload build artifacts
uses: actions/upload-artifact@v4
with:
name: build-output
path: out

- uses: ../actions/deploy-to-ipfs
id: deploy
with:
path-to-deploy: out
storacha-key: ${{ secrets.STORACHA_KEY }}
storacha-proof: ${{ secrets.STORACHA_PROOF }}
pinata-jwt-token: ${{ secrets.PINATA_JWT_TOKEN }}
github-token: ${{ github.token }}

update-dnslink:
runs-on: 'ubuntu-latest'
needs: build-and-deploy
if: github.ref == 'refs/heads/main' # only update for main branch
if: github.ref == 'refs/heads/main' # only update DNSLink for main branch
steps:
- name: Update DNSLink
run: |
Expand Down

0 comments on commit 92c9b8f

Please sign in to comment.