-
Notifications
You must be signed in to change notification settings - Fork 35
/
set-netlify-deployment-status.js
executable file
·141 lines (126 loc) · 3.82 KB
/
set-netlify-deployment-status.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/usr/bin/env node
const fetch = require('node-fetch');
const run = async () => {
const NETLIFY_API = 'https://api.netlify.com/api/v1';
const {
GH_TOKEN,
DRONE_BUILD_NUMBER,
DRONE_REPO,
DRONE_COMMIT_SHA,
DRONE_BUILD_LINK,
NETLIFY_SITE_ID,
NETLIFY_AUTH_TOKEN,
} = process.env;
if (!DRONE_BUILD_NUMBER) {
console.info('Missing information about build number.');
return;
}
if (!GH_TOKEN) {
console.info("Won't be able to communicate Netlify deployment to Github.");
console.info('GH_TOKEN environment variable is required!');
return;
}
if (!DRONE_REPO || !DRONE_COMMIT_SHA) {
console.info(
'Current script depends on Drone CI 0.8 environment variables.'
);
console.info(
'Please see https://0-8-0.docs.drone.io/environment-reference'
);
console.log('Required: DRONE_REPO, DRONE_COMMIT_SHA');
return;
}
try {
const deploymentsResponse = await fetch(
`${NETLIFY_API}/sites/${NETLIFY_SITE_ID}/deploys`,
{
method: 'GET',
headers: {
Accept: 'application/json',
'Accept-Charset': 'utf-8',
'Content-Type': 'application/json',
Authorization: `Bearer ${NETLIFY_AUTH_TOKEN}`,
},
}
);
const deployments = await deploymentsResponse.json();
const currentDeployment = deployments.find(
(deployment) => deployment.title === `Drone build: ${DRONE_BUILD_NUMBER}`
);
const siteDeploymentResponse = await fetch(
`${NETLIFY_API}/sites/${NETLIFY_SITE_ID}/deploys/${currentDeployment.id}`,
{
method: 'GET',
headers: {
Accept: 'application/json',
'Accept-Charset': 'utf-8',
'Content-Type': 'application/json',
Authorization: `Bearer ${NETLIFY_AUTH_TOKEN}`,
},
}
);
const siteDeployment = await siteDeploymentResponse.json();
await fetch(
`https://api.github.com/repos/${DRONE_REPO}/statuses/${DRONE_COMMIT_SHA}`,
{
method: 'POST',
headers: {
Accept: 'application/json',
'Accept-Charset': 'utf-8',
'Content-Type': 'application/json',
Authorization: `Bearer ${GH_TOKEN}`,
},
body: JSON.stringify({
state: 'success',
target_url: siteDeployment.deploy_ssl_url,
description: 'Preview with URL based on current branch',
context: 'drone/netlify-preview/branch-alias',
}),
}
);
await fetch(
`https://api.github.com/repos/${DRONE_REPO}/statuses/${DRONE_COMMIT_SHA}`,
{
method: 'POST',
headers: {
Accept: 'application/json',
'Accept-Charset': 'utf-8',
'Content-Type': 'application/json',
Authorization: `Bearer ${GH_TOKEN}`,
},
body: JSON.stringify({
state: 'success',
target_url: `https://${currentDeployment.id}--europa-component-library.netlify.app`,
description: 'Preview with URL based on the deployment ID',
context: 'drone/netlify-preview/deployment-id',
}),
}
);
} catch (error) {
// @see https://developer.github.com/v3/repos/statuses
await fetch(
`https://api.github.com/repos/${DRONE_REPO}/statuses/${DRONE_COMMIT_SHA}`,
{
method: 'POST',
headers: {
Accept: 'application/json',
'Accept-Charset': 'utf-8',
'Content-Type': 'application/json',
Authorization: `Bearer ${GH_TOKEN}`,
},
body: JSON.stringify({
state: 'error',
target_url: DRONE_BUILD_LINK,
description: `[${error.name}: ${error.message}]`,
context: 'drone/netlify-preview',
}),
}
);
}
console.log('Status on pull request successfully updated!');
};
try {
run();
} catch (error) {
console.error(error);
}