Skip to content

Commit f9a166d

Browse files
committed
Create or reuse environment/deployment
1 parent eab51e2 commit f9a166d

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

.github/workflows/deploy-preview.yml

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ on:
1414
- in_progress
1515
- failure
1616
- success
17+
deploy_id:
18+
description: Netlify deploy ID
19+
required: true
20+
type: string
1721
log:
1822
description: Netlify log URL
1923
required: true
@@ -35,21 +39,35 @@ jobs:
3539
env:
3640
GH_REPO: ${{ github.repository }}
3741
GH_TOKEN: ${{ secrets.GH_TOKEN_MANAGE_ENVS }}
42+
ISSUE: ${{ github.event.inputs.issue }}
43+
STATUS: ${{ github.event.inputs.status }}
44+
DEPLOY: ${{ github.event.inputs.deploy_id }}
3845
run: |
39-
ref=$(gh pr view ${{ github.event.inputs.issue }} --json potentialMergeCommit -q .potentialMergeCommit.oid)
40-
env=staging-${{ github.event.inputs.issue }}
46+
ref=$(gh pr view $ISSUE --json potentialMergeCommit -q .potentialMergeCommit.oid)
47+
env=staging-$ISSUE
4148
url=https://$env.cmbuckley.co.uk
4249
echo "url=$url" >> $GITHUB_OUTPUT
4350
44-
# create environment, deployment and success status
45-
gh api -X PUT repos/{owner}/{repo}/environments/$env
46-
deployment=$(gh api repos/{owner}/{repo}/deployments -q .id \
51+
# create environment unless it exists already
52+
gh api --silent repos/{owner}/{repo}/environments/$env 2>/dev/null \
53+
|| gh api -X PUT repos/{owner}/{repo}/environments/$env
54+
55+
# find deployment for this Netlify ID
56+
deployment=$(gh api repos/{owner}/{repo}/deployments?environment=$env \
57+
-q "map(select(.payload.deploy == \"$DEPLOY\")) | first.id")
58+
59+
# create deployment if it doesn't exist
60+
[ -z "$deployment" ] && deployment=$(gh api repos/{owner}/{repo}/deployments -q .id \
4761
-f ref=$ref \
4862
-f environment=$env \
4963
-F auto_merge=false
5064
-F required_contexts[]=netlify/{owner}/deploy-preview)
65+
-f payload[deploy]=$DEPLOY
66+
-f 'description=Deploy by Netlify'
67+
68+
# set status of the deployment
5169
gh api -X POST repos/{owner}/{repo}/deployments/$deployment/statuses \
52-
-f state=success \
70+
-f state=$STATUS \
5371
-f environment_url=$url
5472
5573
- name: Get comment text

_cf/netlify-deployment-status/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ async function handleRequest(request) {
111111
inputs: {
112112
issue: payload.review_id.toString(),
113113
status: {building: 'in_progress', ready: 'success', error: 'failure'}[payload.state],
114+
deploy_id: payload.id,
114115
log: `${payload.admin_url}/deploys/${payload.id}`,
115116
}
116117
})

0 commit comments

Comments
 (0)