Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 98 additions & 0 deletions .github/workflows/pull-request-deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
name: Pull Request Deploy

on:
workflow_run:
workflows: ["Pull Request Build"]
types: [completed]

jobs:
deploy:
runs-on: ubuntu-latest
if: ${{ github.event.workflow_run.conclusion == 'success' }}
permissions:
pull-requests: write
actions: read
steps:
- name: Set up Node.js 20
uses: actions/setup-node@v6
with:
node-version: 20

- name: Install dependencies
run: |
npm install netlify-cli@21.2.0 -g yarn
- name: Download build artifact
uses: actions/download-artifact@v4
with:
name: build
run-id: ${{ github.event.workflow_run.id }}
github-token: ${{ secrets.GITHUB_TOKEN }}
path: ./build

- name: Download PR data artifact
uses: actions/download-artifact@v4
with:
name: pr-data
run-id: ${{ github.event.workflow_run.id }}
github-token: ${{ secrets.GITHUB_TOKEN }}

- name: Load PR data
run: |
cat pr-data.env >> $GITHUB_ENV
- name: Deploy to Netlify
run: >
netlify deploy
--no-build
--site ${{ secrets.NETLIFY_SITE_ID }}
--auth ${{ secrets.NETLIFY_AUTH_TOKEN }}
--message "Deploy from GitHub Action (pull-request: ${{ env.PR_NUMBER }}, ${{ env.HEAD_SHA }})"
--dir ./build
--json > deploy_output.json
- name: Get Netlify Deploy URL
id: netlify
run: |
DEPLOY_URL=$(jq -r '.deploy_url' deploy_output.json)
echo "deploy_url=$DEPLOY_URL" >> $GITHUB_OUTPUT
- name: Add or Update PR comment
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const sha = process.env.HEAD_SHA;
const pr_number = process.env.PR_NUMBER;
const deployUrl = `${{ steps.netlify.outputs.deploy_url }}`;
const commentIdentifier = "<!-- deploy-comment -->";
const body = `${commentIdentifier}\n
| Name | Link |
|:-:|------------------------|
|<span aria-hidden="true">🔨</span> Latest commit | ${sha} |
|<span aria-hidden="true">😎</span> Deploy Preview | ${deployUrl} |
`
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: pr_number,
});
const existingComment = comments.find(comment => comment.body.includes(commentIdentifier));
if (existingComment) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: existingComment.id,
body: body,
});
} else {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: pr_number,
body: body,
});
}
94 changes: 15 additions & 79 deletions .github/workflows/pull-request.yml
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
name: Pull Request Preview
name: Pull Request Build

on:
pull_request_target:
pull_request:

concurrency:
group: ${{ github.workflow }}-${{ github.event.number }}
cancel-in-progress: true

jobs:
prepare:
build:
runs-on: ubuntu-latest
permissions: {} # disable all permission
permissions:
contents: read
actions: write
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.head.sha }}

- name: Set up Node.js 20
uses: actions/setup-node@v6
Expand All @@ -37,79 +37,15 @@ jobs:
name: build
path: ./build

deploy:
runs-on: ubuntu-latest
permissions:
pull-requests: write
needs: prepare
steps:
- name: Set up Node.js 20
uses: actions/setup-node@v6
with:
node-version: 20

- name: Install dependencies
- name: Save PR data to artifact
run: |
npm install netlify-cli@21.2.0 -g yarn

- name: Download build from artifacts
uses: actions/download-artifact@v4
with:
name: build
path: ./build
{
echo "PR_NUMBER=${{ github.event.pull_request.number }}"
echo "HEAD_SHA=${{ github.event.pull_request.head.sha }}"
} > pr-data.env

- name: Deploy to Netlify
run: >
netlify deploy
--no-build
--site ${{ secrets.NETLIFY_SITE_ID }}
--auth ${{ secrets.NETLIFY_AUTH_TOKEN }}
--message "Deploy from GitHub Action (pull-request: ${{ github.event.number }}, ${{ github.event.pull_request.head.sha }})"
--dir ./build
--json > deploy_output.json

- name: Get Netlify Deploy URL
id: netlify
run: |
DEPLOY_URL=$(jq -r '.deploy_url' deploy_output.json)
echo "deploy_url=$DEPLOY_URL" >> $GITHUB_OUTPUT

- name: Add or Update PR comment
uses: actions/github-script@v7
- name: Upload PR data artifact
uses: actions/upload-artifact@v4
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const sha = `${{ github.event.pull_request.head.sha }}`
const pr_number = `${{ github.event.number }}`;
const deployUrl = `${{ steps.netlify.outputs.deploy_url }}`;
const commentIdentifier = "<!-- deploy-comment -->";
const body = `${commentIdentifier}\n
| Name | Link |
|:-:|------------------------|
|<span aria-hidden="true">🔨</span> Latest commit | ${sha} |
|<span aria-hidden="true">😎</span> Deploy Preview | ${deployUrl} |
`

const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: pr_number,
});

const existingComment = comments.find(comment => comment.body.includes(commentIdentifier));

if (existingComment) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: existingComment.id,
body: body,
});
} else {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: pr_number,
body: body,
});
}
name: pr-data
path: pr-data.env
Loading