Skip to content

Commit eec356d

Browse files
committed
Manual environment creation again, more notification states
The magic environment creation doesn't work with forks because the ref at Netlify is the name of the ref on the fork. It's not possible to call a workflow_dispatch on a PR ref, so the environment needs to be manually created. Also subscribe to deploy preview started, failed and succeeded events, not just succeeded.
1 parent f9846d6 commit eec356d

File tree

2 files changed

+59
-18
lines changed

2 files changed

+59
-18
lines changed
Lines changed: 55 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,73 @@
1-
name: Netlify deploy-preview success
1+
name: Netlify deploy-preview
22
on:
33
workflow_dispatch:
44
inputs:
55
issue:
66
description: Issue number
7+
required: true
8+
type: number
9+
status:
10+
description: Deployment status
11+
required: true
12+
type: choice
13+
options:
14+
- in_progress
15+
- failure
16+
- success
717
log:
818
description: Netlify log URL
19+
required: true
20+
type: string
921

1022
jobs:
1123
notify:
1224
name: Notify about deployment
1325
runs-on: ubuntu-latest
14-
environment:
15-
name: staging-${{ github.event.inputs.issue }}
16-
url: ${{ steps.get-url.outputs.url }}
17-
1826
steps:
19-
- name: Get deployment URL
20-
id: get-url
27+
- name: Create deployment
28+
id: deployment
29+
env:
30+
GH_REPO: ${{ github.repository }}
31+
GH_TOKEN: ${{ secrets.GH_TOKEN_MANAGE_ENVS }}
2132
run: |
22-
echo "url=https://staging-${{ github.event.inputs.issue }}.cmbuckley.co.uk" >> $GITHUB_OUTPUT
33+
ref=$(gh pr view ${{ github.event.inputs.issue }} --json headRefOid -q .headRefOid)
34+
env=staging-${{ github.event.inputs.issue }}
35+
url=https://$env.cmbuckley.co.uk
36+
echo "url=$url" >> $GITHUB_OUTPUT
37+
38+
# create environment, deployment and success status
39+
gh api -X PUT repos/{owner}/{repo}/environments/$env
40+
deployment=$(gh api repos/{owner}/{repo}/deployments \
41+
-f ref=$ref \
42+
-f environment=$env \
43+
-F required_contexts[]=netlify/{owner}/deploy-preview -q .id)
44+
gh api -X POST repos/{owner}/{repo}/deployments/$deployment/statuses \
45+
-f state=success \
46+
-f environment_url=$url
47+
48+
- name: Get comment text
49+
id: get-text
50+
uses: actions/github-script@v7
51+
with:
52+
script: |
53+
const status = github.events.inputs.status,
54+
data = {
55+
in_progress: ['👷', 'in progress', false],
56+
failure: ['❌', 'failed', false],
57+
success: ['✅', 'ready', true],
58+
};
59+
60+
core.setOutput('emoji', data[status][0]);
61+
core.setOutput('title', data[status][1]);
62+
core.setOutput('preview', data[status][2]);
2363
2464
- name: Find Comment
25-
uses: peter-evans/find-comment@v3
2665
id: find-comment
66+
uses: peter-evans/find-comment@v3
2767
with:
2868
issue-number: ${{ github.event.inputs.issue }}
2969
comment-author: 'github-actions[bot]'
30-
body-includes: Deploy Preview ready
70+
body-includes: Deploy Preview
3171

3272
- name: Create comment
3373
uses: peter-evans/create-or-update-comment@v4
@@ -36,9 +76,9 @@ jobs:
3676
comment-id: ${{ steps.find-comment.outputs.comment-id }}
3777
edit-mode: replace
3878
body: |
39-
### <span aria-hidden="true"></span> Deploy Preview ready!
79+
### <span aria-hidden="true">${{ steps.get-text.outputs.emoji }}</span> Deploy Preview ${{ steps.get-text.outputs.title }}!
4080
41-
| Name | Link |
42-
|----------------|----------------------------------|
43-
| Deploy Preview | ${{ steps.get-url.outputs.url }} |
44-
| Deploy Log | ${{ github.event.inputs.log }} |
81+
| Name | Link |
82+
|----------------|------|
83+
${{ steps.get-text.outputs.preview && format('| Deploy Preview | {0} |\n', steps.deployment.outputs.url) || ''}}
84+
| Deploy Log | ${{ github.event.inputs.log }} |

_cf/netlify-deployment-status/index.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,14 @@ async function handleRequest(request) {
8888
if (!payload) { return error("Must POST JSON") }
8989
console.log('Payload:', payload)
9090

91-
if (payload.context == "deploy-preview" && payload.state == "ready") {
91+
if (payload.context == "deploy-preview") {
9292
const apiUrl = (payload.review_url || "")
9393
.replace('//github.com', '//api.github.com/repos')
9494
.replace(/pull\/\d+$/, '');
9595
let workflowDispatch;
9696

9797
if (!payload.review_id) { return error("Missing PR number") }
98-
console.log(`Dispatching to ${apiUrl} with issue ${payload.review_id}`);
98+
console.log(`Dispatching to ${apiUrl} with issue ${payload.review_id} and status ${payload.state}`);
9999

100100
try {
101101
workflowDispatch = await fetch(apiUrl + 'actions/workflows/deploy-preview.yml/dispatches', {
@@ -107,9 +107,10 @@ async function handleRequest(request) {
107107
"Content-Type": "application/json",
108108
},
109109
body: JSON.stringify({
110-
ref: `pull/${payload.review_id}/head`,
110+
ref: 'main',
111111
inputs: {
112112
issue: payload.review_id.toString(),
113+
status: {building: 'in_progress', ready: 'success', error: 'failure'}[payload.state],
113114
log: `${payload.admin_url}/deploys/${payload.id}`,
114115
}
115116
})

0 commit comments

Comments
 (0)