|
| 1 | +name: PR Preview Deployment |
| 2 | +on: |
| 3 | + pull_request: |
| 4 | + types: [opened, synchronize, reopened] |
| 5 | + |
| 6 | +jobs: |
| 7 | + build_and_deploy: |
| 8 | + # do not run in forks |
| 9 | + if: github.event.pull_request.head.repo.full_name == github.repository |
| 10 | + runs-on: ubuntu-latest |
| 11 | + concurrency: |
| 12 | + group: pr-${{ github.event.pull_request.number || github.event.number }} |
| 13 | + cancel-in-progress: true |
| 14 | + |
| 15 | + steps: |
| 16 | + - uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4.3.1 |
| 17 | + |
| 18 | + - name: Initialize examples submodule |
| 19 | + run: | |
| 20 | + git config --global url."https://github.com/".insteadOf "git@github.com:" |
| 21 | + git submodule update --init --depth 1 .sources/examples |
| 22 | +
|
| 23 | + - uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4.4.0 |
| 24 | + with: |
| 25 | + node-version: 22 |
| 26 | + cache: npm |
| 27 | + |
| 28 | + - uses: actions/github-script@d7906e4ad0b1822421a7e6a35d5ca353c962f410 # v6.4.1 |
| 29 | + with: |
| 30 | + script: | |
| 31 | + const comments = require('./.github/workflows/scripts/comments.cjs'); |
| 32 | + const maybeComment = await comments.get(context, github); |
| 33 | + if (maybeComment) { |
| 34 | + await comments.update(context, github, maybeComment.id, `🤖 Your PR preview is being built...`); |
| 35 | + } else { |
| 36 | + await comments.create(context, github, `🤖 Your PR preview is being built...`); |
| 37 | + } |
| 38 | +
|
| 39 | + - uses: actions/setup-python@7f4fc3e22c37d6ff65e88745f38bd3157c663f7c # v4.9.1 |
| 40 | + with: |
| 41 | + python-version: "3.10" |
| 42 | + |
| 43 | + - name: Install icp-cli |
| 44 | + run: npm i -g @icp-sdk/icp-cli@0.2.6 @icp-sdk/ic-wasm |
| 45 | + |
| 46 | + - run: npm ci |
| 47 | + |
| 48 | + - name: Build & Deploy |
| 49 | + run: | |
| 50 | + # Setup identity |
| 51 | + mkdir -p ~/.local/share/icp-cli/identity/keys |
| 52 | + echo $POOL_CONTROLLER_IDENTITY | base64 -d > ~/.local/share/icp-cli/identity/keys/preview-deploy.pem |
| 53 | + sed -i 's/\\r\\n/\r\n/g' ~/.local/share/icp-cli/identity/keys/preview-deploy.pem |
| 54 | + icp identity import preview-deploy --from-pem ~/.local/share/icp-cli/identity/keys/preview-deploy.pem --storage plaintext |
| 55 | + icp identity default preview-deploy |
| 56 | +
|
| 57 | + # Request preview canister from the pool |
| 58 | + pip install icp-py-core "cbor2<6" |
| 59 | + canister_id=$(python3 .github/workflows/scripts/request-canister.py ${{ github.event.pull_request.number }}) |
| 60 | +
|
| 61 | + # Override canister ID mapping for ic environment |
| 62 | + echo "{\"frontend\":\"$canister_id\"}" > .icp/data/mappings/ic.ids.json |
| 63 | +
|
| 64 | + echo "PREVIEW_CANISTER_ID=$canister_id" >> $GITHUB_ENV |
| 65 | +
|
| 66 | + # Deploy (icp.yaml recipe handles the build) |
| 67 | + icp deploy frontend -e ic --mode reinstall |
| 68 | +
|
| 69 | + env: |
| 70 | + POOL_CONTROLLER_IDENTITY: ${{ secrets.POOL_CONTROLLER_IDENTITY }} |
| 71 | + POOL_CANISTER_ID: ${{ secrets.POOL_CANISTER_ID }} |
| 72 | + |
| 73 | + - name: Report build error |
| 74 | + uses: actions/github-script@d7906e4ad0b1822421a7e6a35d5ca353c962f410 # v6.4.1 |
| 75 | + if: ${{ failure() }} |
| 76 | + with: |
| 77 | + script: | |
| 78 | + const comments = require('./.github/workflows/scripts/comments.cjs'); |
| 79 | + const maybeComment = await comments.get(context, github); |
| 80 | + if (maybeComment) { |
| 81 | + await comments.update(context, github, maybeComment.id, `🤖 Preview build failed.`); |
| 82 | + } else { |
| 83 | + await comments.create(context, github, `🤖 Preview build failed.`); |
| 84 | + } |
| 85 | +
|
| 86 | + - uses: actions/github-script@d7906e4ad0b1822421a7e6a35d5ca353c962f410 # v6.4.1 |
| 87 | + with: |
| 88 | + script: | |
| 89 | + const comments = require('./.github/workflows/scripts/comments.cjs'); |
| 90 | + const maybeComment = await comments.get(context, github); |
| 91 | + if (maybeComment) { |
| 92 | + await comments.update(context, github, maybeComment.id, `🤖 Here's your preview: https://${process.env.PREVIEW_CANISTER_ID}.icp0.io`); |
| 93 | + } else { |
| 94 | + await comments.create(context, github, `🤖 Here's your preview: https://${process.env.PREVIEW_CANISTER_ID}.icp0.io`); |
| 95 | + } |
0 commit comments