diff --git a/.github/workflows/pull-request-deploy.yml b/.github/workflows/pull-request-deploy.yml new file mode 100644 index 0000000000..3e1a33c4e8 --- /dev/null +++ b/.github/workflows/pull-request-deploy.yml @@ -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 = ""; + const body = `${commentIdentifier}\n + | Name | Link | + |:-:|------------------------| + | Latest commit | ${sha} | + | 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, + }); + } diff --git a/.github/workflows/pull-request.yml b/.github/workflows/pull-request.yml index 9fc7f9b023..bcec5558fd 100644 --- a/.github/workflows/pull-request.yml +++ b/.github/workflows/pull-request.yml @@ -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 @@ -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 = ""; - const body = `${commentIdentifier}\n - | Name | Link | - |:-:|------------------------| - | Latest commit | ${sha} | - | 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