|
| 1 | +name: Deploy pull request to acceptance |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_run: |
| 5 | + workflows: ["Build pull request"] |
| 6 | + types: [completed] |
| 7 | + |
| 8 | +permissions: |
| 9 | + contents: read |
| 10 | + |
| 11 | +jobs: |
| 12 | + deploy_acceptance: |
| 13 | + if: >- |
| 14 | + github.event.workflow_run.event == 'pull_request' && |
| 15 | + github.event.workflow_run.conclusion == 'success' |
| 16 | + runs-on: ubuntu-latest |
| 17 | + timeout-minutes: 30 |
| 18 | + concurrency: |
| 19 | + group: acceptance-${{ github.event.workflow_run.head_repository.full_name }}-${{ github.event.workflow_run.head_branch }} |
| 20 | + cancel-in-progress: false |
| 21 | + environment: |
| 22 | + name: acceptance |
| 23 | + url: ${{ steps.get_brancher_hostname.outputs.BRANCHER_URL }} |
| 24 | + permissions: |
| 25 | + actions: read |
| 26 | + contents: read |
| 27 | + pull-requests: write |
| 28 | + statuses: write |
| 29 | + container: quay.io/hypernode/deploy:latest-php8.4-node22 |
| 30 | + steps: |
| 31 | + - name: Resolve the pull request behind this build |
| 32 | + id: pr |
| 33 | + uses: actions/github-script@v7 |
| 34 | + with: |
| 35 | + script: | |
| 36 | + const run = context.payload.workflow_run; |
| 37 | +
|
| 38 | + // Populated for same-repository branches, empty for forks. |
| 39 | + let number = (run.pull_requests || []).map((p) => p.number)[0]; |
| 40 | + if (!number) { |
| 41 | + const { data: prs } = |
| 42 | + await github.rest.repos.listPullRequestsAssociatedWithCommit({ |
| 43 | + owner: context.repo.owner, |
| 44 | + repo: context.repo.repo, |
| 45 | + commit_sha: run.head_sha, |
| 46 | + }); |
| 47 | + number = prs.filter((p) => p.state === 'open').map((p) => p.number)[0]; |
| 48 | + } |
| 49 | + if (!number) { |
| 50 | + core.setFailed(`No open pull request found for commit ${run.head_sha}`); |
| 51 | + return; |
| 52 | + } |
| 53 | +
|
| 54 | + const { data: pr } = await github.rest.pulls.get({ |
| 55 | + owner: context.repo.owner, |
| 56 | + repo: context.repo.repo, |
| 57 | + pull_number: number, |
| 58 | + }); |
| 59 | + core.setOutput('number', pr.number); |
| 60 | + core.setOutput('head_sha', run.head_sha); |
| 61 | + core.setOutput('head_ref', pr.head.ref); |
| 62 | + core.setOutput('base_sha', pr.base.sha); |
| 63 | +
|
| 64 | + - name: Check out the trusted deploy configuration |
| 65 | + uses: actions/checkout@v4 |
| 66 | + with: |
| 67 | + ref: ${{ github.event.repository.default_branch }} |
| 68 | + fetch-depth: 0 |
| 69 | + - name: Add repository to git safe directories |
| 70 | + run: git config --global --add safe.directory $GITHUB_WORKSPACE |
| 71 | + |
| 72 | + - name: download build artifact |
| 73 | + uses: actions/download-artifact@v4 |
| 74 | + with: |
| 75 | + name: deployment-build |
| 76 | + path: build/ |
| 77 | + run-id: ${{ github.event.workflow_run.id }} |
| 78 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 79 | + |
| 80 | + - uses: webfactory/ssh-agent@e83874834305fe9a4a2997156cb26c5de65a8555 # v0.10.0 |
| 81 | + with: |
| 82 | + ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }} |
| 83 | + - run: mkdir -p $HOME/.ssh |
| 84 | + |
| 85 | + - name: deploy to acceptance |
| 86 | + run: hypernode-deploy deploy acceptance -vvv --reuse-brancher |
| 87 | + env: |
| 88 | + HYPERNODE_API_TOKEN: ${{ secrets.HYPERNODE_API_TOKEN }} |
| 89 | + # workflow_run does not set GITHUB_HEAD_REF, which deploy.php uses to |
| 90 | + # label the brancher. Without this every pull request shares one node. |
| 91 | + CI_REF: ${{ steps.pr.outputs.head_ref }} |
| 92 | + |
| 93 | + - name: Get brancher hostname |
| 94 | + id: get_brancher_hostname |
| 95 | + run: echo "BRANCHER_URL=https://$(jq .hostnames[0] deployment-report.json -r)" >> $GITHUB_OUTPUT |
| 96 | + |
| 97 | + - name: Get changed pages |
| 98 | + id: changed_pages |
| 99 | + env: |
| 100 | + BASE_SHA: ${{ steps.pr.outputs.base_sha }} |
| 101 | + HEAD_SHA: ${{ steps.pr.outputs.head_sha }} |
| 102 | + BRANCHER_URL: ${{ steps.get_brancher_hostname.outputs.BRANCHER_URL }} |
| 103 | + run: | |
| 104 | + # The built commit is fetched by SHA as data to diff against (the |
| 105 | + # pull request ref may already point at a newer push); it is never |
| 106 | + # checked out or executed. |
| 107 | + git fetch --no-tags --quiet origin "$HEAD_SHA" |
| 108 | + result="$(python3 ci/bin/get_changed_urls.py \ |
| 109 | + "$BASE_SHA" \ |
| 110 | + "$HEAD_SHA" \ |
| 111 | + --base-url="$BRANCHER_URL" |
| 112 | + )" |
| 113 | + echo "$result" |
| 114 | + { |
| 115 | + echo "CHANGED_PAGES<<EOF" |
| 116 | + echo "$result" |
| 117 | + echo "EOF" |
| 118 | + } >> "$GITHUB_OUTPUT" |
| 119 | + shell: bash |
| 120 | + |
| 121 | + - name: Comment hostname on PR |
| 122 | + uses: thollander/actions-comment-pull-request@24bffb9b452ba05a4f3f77933840a6a841d1b32b # v3.0.1 |
| 123 | + with: |
| 124 | + pr-number: ${{ steps.pr.outputs.number }} |
| 125 | + comment-tag: acceptance-environment |
| 126 | + message: | |
| 127 | + Acceptance server is available at ${{ steps.get_brancher_hostname.outputs.BRANCHER_URL }}. |
| 128 | + ${{ steps.changed_pages.outputs.CHANGED_PAGES }} |
| 129 | +
|
| 130 | + # workflow_run runs do not appear in the pull request's check list. |
| 131 | + - name: Mirror result onto the pull request head commit |
| 132 | + if: always() && steps.pr.outputs.head_sha |
| 133 | + uses: actions/github-script@v7 |
| 134 | + env: |
| 135 | + HEAD_SHA: ${{ steps.pr.outputs.head_sha }} |
| 136 | + JOB_STATUS: ${{ job.status }} |
| 137 | + BRANCHER_URL: ${{ steps.get_brancher_hostname.outputs.BRANCHER_URL }} |
| 138 | + with: |
| 139 | + script: | |
| 140 | + const ok = process.env.JOB_STATUS === 'success'; |
| 141 | + const runUrl = `${context.serverUrl}/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}`; |
| 142 | + await github.rest.repos.createCommitStatus({ |
| 143 | + owner: context.repo.owner, |
| 144 | + repo: context.repo.repo, |
| 145 | + sha: process.env.HEAD_SHA, |
| 146 | + state: ok ? 'success' : 'failure', |
| 147 | + context: 'Deploy pull request to acceptance', |
| 148 | + target_url: ok ? (process.env.BRANCHER_URL || runUrl) : runUrl, |
| 149 | + description: ok |
| 150 | + ? 'Acceptance environment deployed' |
| 151 | + : 'Acceptance deploy failed', |
| 152 | + }); |
0 commit comments