Netlify deploy-preview #164
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Netlify deploy-preview | |
| on: | |
| workflow_dispatch: | |
| inputs: | |
| issue: | |
| description: Issue number | |
| required: true | |
| type: number | |
| status: | |
| description: Deployment status | |
| required: true | |
| type: choice | |
| options: | |
| - in_progress | |
| - failure | |
| - success | |
| deploy_id: | |
| description: Netlify deploy ID | |
| required: true | |
| type: string | |
| log: | |
| description: Netlify log URL | |
| required: true | |
| type: string | |
| jobs: | |
| debug: | |
| name: Debug inputs | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Debug inputs | |
| run: jq . <<< '${{ toJSON(github.event.inputs) }}' | |
| notify: | |
| name: Notify about deployment | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Create deployment | |
| id: deployment | |
| env: | |
| GH_REPO: ${{ github.repository }} | |
| GH_TOKEN: ${{ secrets.GH_TOKEN_MANAGE_ENVS }} | |
| ISSUE: ${{ github.event.inputs.issue }} | |
| STATUS: ${{ github.event.inputs.status }} | |
| DEPLOY: ${{ github.event.inputs.deploy_id }} | |
| run: | | |
| set -e ${RUNNER_DEBUG:+-x} | |
| env=staging-$ISSUE | |
| url=https://$env.cmbuckley.co.uk | |
| echo "url=$url" >> $GITHUB_OUTPUT | |
| ref=$(gh pr view $ISSUE --json headRefOid -q .headRefOid) | |
| # create environment unless it exists already | |
| gh api --silent repos/{owner}/{repo}/environments/$env 2>/dev/null \ | |
| || gh api -X PUT repos/{owner}/{repo}/environments/$env | |
| # find deployment for this Netlify ID | |
| deployment=$(gh api repos/{owner}/{repo}/deployments?environment=$env \ | |
| -q "map(select(.payload.deploy == \"$DEPLOY\")) | first.id") | |
| # create deployment if it doesn't exist | |
| [ -z "$deployment" ] && deployment=$(gh api repos/{owner}/{repo}/deployments -q .id \ | |
| -f ref=$ref \ | |
| -f environment=$env \ | |
| -F auto_merge=false \ | |
| -F required_contexts[]$([ $STATUS == 'success' ] && echo '=netlify/{owner}/deploy-preview') \ | |
| -f payload[deploy]=$DEPLOY \ | |
| -f 'description=Deploy by Netlify') | |
| # set status of the deployment | |
| gh api -X POST repos/{owner}/{repo}/deployments/$deployment/statuses \ | |
| -f state=$STATUS \ | |
| -f environment_url=$url | |
| - name: Get comment text | |
| id: get-text | |
| uses: actions/github-script@v8 | |
| with: | |
| script: | | |
| const status = context.payload.inputs.status, | |
| data = { | |
| in_progress: ['👷', 'in progress', false], | |
| failure: ['❌', 'failed', false], | |
| success: ['✅', 'ready', true], | |
| }; | |
| core.setOutput('emoji', data[status][0]); | |
| core.setOutput('title', data[status][1]); | |
| core.setOutput('preview', data[status][2]); | |
| - name: Find Comment | |
| id: find-comment | |
| uses: peter-evans/find-comment@v4 | |
| with: | |
| issue-number: ${{ github.event.inputs.issue }} | |
| comment-author: 'github-actions[bot]' | |
| body-includes: Deploy Preview | |
| - name: Create comment | |
| uses: peter-evans/create-or-update-comment@v5 | |
| with: | |
| issue-number: ${{ github.event.inputs.issue }} | |
| comment-id: ${{ steps.find-comment.outputs.comment-id }} | |
| edit-mode: replace | |
| body: | | |
| ### <span aria-hidden="true">${{ steps.get-text.outputs.emoji }}</span> Deploy Preview ${{ steps.get-text.outputs.title }}! | |
| | Name | Link | | |
| |----------------|------| | |
| ${{ steps.get-text.outputs.preview && format('| Deploy Preview | {0} |\n', steps.deployment.outputs.url) || ''}} | |
| | Deploy Log | ${{ github.event.inputs.log }} | |