-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
75 lines (61 loc) · 1.8 KB
/
index.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
const core = require('@actions/core');
const github = require('@actions/github');
const { owner, repo } = github.context.repo;
const ref = process.env.GITHUB_REF;
const environment = core.getInput('environment', {
required: true,
});
const preview_url = core.getInput('preview_url');
const description = core.getInput('description');
const token = core.getInput('token', { required: true });
const octokit = github.getOctokit(token);
async function createDeployment() {
const req = {
owner,
repo,
ref,
environment,
description,
auto_merge: false,
required_contexts: [],
};
const response = await octokit.rest.repos.createDeployment(req);
if (response.status >= 400) {
throw new Error('Failed to create a new deployment');
}
return response;
}
async function getFailureURL() {
const {repo: {owner, repo}, serverUrl, runId} = github.context;
return `${serverUrl}/${owner}/${repo}/actions/runs/${runId}`
}
async function main() {
const response = await createDeployment();
const isSuccessful = preview_url && preview_url.length > 0;
const deploymentStatus = {
repo,
owner,
deployment_id: response.data.id,
auto_inactive: false,
};
if (isSuccessful) {
deploymentStatus.state = 'success';
deploymentStatus.environment_url = preview_url;
} else {
deploymentStatus.state = 'failure';
deploymentStatus.environment_url = await getFailureURL();
}
await octokit.rest.repos.createDeploymentStatus(deploymentStatus);
return deploymentStatus.state;
}
main().then((result) => {
if (process.env.WORKFLOW_CI) {
const expected = process.env.EXPECTED_RESULT
if (result !== expected) {
core.setFailed(`Expected ${expected} but got ${result}`);
}
}
}).catch(function (error) {
core.setFailed(error.message);
process.exit(1);
});