Skip to content

Commit 49aac8d

Browse files
authored
feat: use post script to complete deployment (#4)
1 parent 75fb540 commit 49aac8d

File tree

13 files changed

+25710
-179
lines changed

13 files changed

+25710
-179
lines changed

.github/workflows/test.yml

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ jobs:
2323
with:
2424
token: ${{github.token}}
2525
type: delete-all
26+
environment: test
2627
- uses: ./
2728
name: Create first deployment
2829
id: create-first
@@ -32,13 +33,7 @@ jobs:
3233
environment: test
3334
logs: https://github.com/npm/action-deploy/actions?query=workflow%3Abuild-test
3435
environment_url: https://npmjs.com
35-
- uses: ./
36-
name: Finish first deployment with success
37-
with:
38-
token: ${{github.token}}
39-
type: finish
40-
status: success
41-
deployment_id: ${{steps.create-first.outputs.deployment_id}}
36+
job_status: ${{job.status}}
4237
- uses: ./
4338
name: Delete first deployment
4439
with:
@@ -54,10 +49,4 @@ jobs:
5449
environment: test
5550
logs: https://github.com/npm/action-deploy/actions?query=workflow%3Abuild-test
5651
environment_url: https://npmjs.com
57-
- uses: ./
58-
name: Finish second deployment
59-
with:
60-
token: ${{github.token}}
61-
type: finish
62-
status: success
63-
deployment_id: ${{steps.create-second.outputs.deployment_id}}
52+
job_status: ${{job.status}}

README.md

Lines changed: 3 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
Features:
44
- create a deployment (and invalidate all previous deployments)
5-
- finish a deployment (with success/failure state)
65
- delete all deployments in specific environment
76
- delete a deployment by id
87

@@ -17,9 +16,10 @@ Inputs:
1716
`token`|**Required** token to authorize calls to GitHub API, can be ${{github.token}} to create a deployment for the same repo
1817
`type`|**Required** type of an action. Should be `create` to create a deployment
1918
`logs`|url to the deployment logs
20-
`environment`|environment to create a deployments in, default to `context.ref` without prefixes (`'refs/heads/'`, `'deploy-'`), i.e. branch name
19+
`environment`|environment to create a deployments in, default to `$context.ref` without prefixes (`'refs/heads/'`, `'deploy-'`), i.e. branch name
2120
`environment_url`|link to the deployed application
2221
`description`|optional description, defaults to `"deployed by $context.actor"`
22+
`job_status`|pass `${{job.status}}` to set the deployment completion status post script accordingly
2323

2424
Outputs:
2525

@@ -38,47 +38,7 @@ Outputs:
3838
logs: https://your-app.com/deployment_logs
3939
environment: staging
4040
environment_url: https://staging.your-app.com
41-
```
42-
43-
### finish
44-
45-
Given in one of the previous steps you created a deployment, with `finish` you can set a status upon a deployment completion
46-
47-
Inputs:
48-
49-
|parameter | description
50-
|- | -
51-
`token` | **Required** token to authorize calls to GitHub API, can be ${{github.token}} to create a deployment for the same repo
52-
`type` | **Required** type of an action. Should be `finish`
53-
`deployment_id` | **Required** the `id` of the a deployment to finish
54-
`status` | can be any status, e.g. failure/success
55-
56-
Outputs: none
57-
58-
#### Example usage
59-
60-
```yaml
61-
- name: create a deployment
62-
uses: npm/action-deploy@v1
63-
id: create-deployment
64-
with:
65-
type: create
66-
token: ${{github.token}}
67-
logs: https://your-app.com/deployment_logs
68-
environment: staging
69-
environment_url: https://staging.your-app.com
70-
71-
# add your deployment steps here
72-
- name: placeholder for actual deployment
73-
run: sleep 10s
74-
75-
- name: finish deployment
76-
uses: npm/action-deploy@v1
77-
with:
78-
type: finish
79-
token: ${{github.token}}
80-
status: success
81-
deployment_id: ${{steps.create-deployment.outputs.deployment_id}}
41+
job_status: ${{job.status}} # use this to track success of the deployment in post script
8242
```
8343
8444
### delete-all

action.yml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ inputs:
77
description: GitHub token
88
type:
99
required: true
10-
description: a type of deployment action to perform, can be 'create', 'delete', 'delete-all' and 'finish'
10+
description: a type of deployment action to perform, can be 'create', 'delete' and 'delete-all'
1111
logs:
1212
required: false
1313
description: url to deployment logs
@@ -16,10 +16,10 @@ inputs:
1616
description: description of a deployment
1717
deployment_id:
1818
required: false
19-
description: an id for deployment if 'delete' or 'finish' type is used
19+
description: an id for deployment if 'delete' type is used
2020
status:
2121
required: false
22-
description: status for created or finished deployment
22+
description: status for created deployment
2323
environment:
2424
required: false
2525
description: environment to create or delete all deployments in, default to context.ref without prefixes ('refs/heads/', 'deploy-'), i.e. branch name
@@ -29,6 +29,10 @@ inputs:
2929
main_branch:
3030
required: false
3131
description: name of main branch of the repo, to get and store sha of the head commit and track diff being deployed
32+
job_status:
33+
required: false
34+
description: pass `{{job.status}}` to set the deployment completion status post script accordingly
3235
runs:
3336
using: 'node12'
34-
main: 'dist/index.js'
37+
main: 'dist/main/index.js'
38+
post: 'dist/post/index.js'

dist/index.js renamed to dist/main/index.js

Lines changed: 58 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -3529,18 +3529,9 @@ exports.run = void 0;
35293529
const core = __importStar(__webpack_require__(470));
35303530
const github = __importStar(__webpack_require__(469));
35313531
const create_1 = __webpack_require__(646);
3532-
const finish_1 = __webpack_require__(209);
35333532
const delete_all_1 = __webpack_require__(832);
35343533
const delete_1 = __webpack_require__(583);
3535-
// nullify getInput empty results
3536-
// to allow coalescence ?? operator
3537-
function getInput(name, options) {
3538-
const result = core.getInput(name, options);
3539-
if (result === '') {
3540-
return null;
3541-
}
3542-
return result;
3543-
}
3534+
const utils_1 = __webpack_require__(611);
35443535
function run() {
35453536
var _a, _b, _c, _d, _e, _f, _g, _h;
35463537
return __awaiter(this, void 0, void 0, function* () {
@@ -3554,30 +3545,30 @@ function run() {
35543545
let deploymentId;
35553546
let mainBranch;
35563547
const { actor, ref } = github.context;
3557-
console.log('### context ###');
3548+
console.log('### main.context ###');
35583549
console.log(`actor: ${actor}`);
35593550
console.log(`ref: ${ref}`);
35603551
console.log('\n');
35613552
try {
3562-
console.log('### inputs ###');
3563-
token = (_a = getInput('token', { required: true })) !== null && _a !== void 0 ? _a : '';
3564-
type = getInput('type', { required: true });
3553+
console.log('### main.inputs ###');
3554+
token = (_a = utils_1.getInput('token', { required: true })) !== null && _a !== void 0 ? _a : '';
3555+
type = utils_1.getInput('type', { required: true });
35653556
console.log(`type: ${type}`);
3566-
logsUrl = (_b = getInput('logs')) !== null && _b !== void 0 ? _b : '';
3557+
logsUrl = (_b = utils_1.getInput('logs')) !== null && _b !== void 0 ? _b : '';
35673558
console.log(`logs: ${logsUrl}`);
3568-
description = (_c = getInput('description')) !== null && _c !== void 0 ? _c : `deployed by ${actor}`;
3559+
description = (_c = utils_1.getInput('description')) !== null && _c !== void 0 ? _c : `deployed by ${actor}`;
35693560
console.log(`description: ${description}`);
3570-
status = ((_d = getInput('status')) !== null && _d !== void 0 ? _d : 'in_progress');
3561+
status = ((_d = utils_1.getInput('status')) !== null && _d !== void 0 ? _d : 'in_progress');
35713562
console.log(`status: ${status}`);
35723563
// default to branch name w/o `deploy-` prefix
3573-
environment = (_e = getInput('environment')) !== null && _e !== void 0 ? _e : ref.replace('refs/heads/', '').replace(/^deploy-/, '');
3564+
environment = (_e = utils_1.getInput('environment')) !== null && _e !== void 0 ? _e : ref.replace('refs/heads/', '').replace(/^deploy-/, '');
35743565
console.log(`environment: ${environment}`);
3575-
environmentUrl = (_f = getInput('environment_url')) !== null && _f !== void 0 ? _f : '';
3566+
environmentUrl = (_f = utils_1.getInput('environment_url')) !== null && _f !== void 0 ? _f : '';
35763567
console.log(`environmentUrl: ${environmentUrl}`);
3577-
mainBranch = (_g = getInput('main_branch')) !== null && _g !== void 0 ? _g : 'master';
3568+
mainBranch = (_g = utils_1.getInput('main_branch')) !== null && _g !== void 0 ? _g : 'master';
35783569
console.log(`main branch: ${mainBranch}`);
3579-
const shouldRequireDeploymentId = type === 'finish' || type === 'delete';
3580-
deploymentId = (_h = getInput('deployment_id', { required: shouldRequireDeploymentId })) !== null && _h !== void 0 ? _h : '0';
3570+
const shouldRequireDeploymentId = type === 'delete';
3571+
deploymentId = (_h = utils_1.getInput(utils_1.DEPLOYMENT_ID_STATE_NAME, { required: shouldRequireDeploymentId })) !== null && _h !== void 0 ? _h : '0';
35813572
console.log(`deploymentId: ${deploymentId}`);
35823573
}
35833574
catch (error) {
@@ -3592,25 +3583,16 @@ function run() {
35923583
case 'create':
35933584
try {
35943585
deploymentId = yield create_1.create(client, logsUrl, description, status, environment, environmentUrl, mainBranch);
3595-
console.log(`setOutput::deployment_id: ${deploymentId}`);
3596-
core.setOutput('deployment_id', deploymentId);
3586+
console.log(`saveState::${utils_1.DEPLOYMENT_ID_STATE_NAME}: ${deploymentId}`);
3587+
core.saveState(utils_1.DEPLOYMENT_ID_STATE_NAME, deploymentId); // for internal use
3588+
core.setOutput(utils_1.DEPLOYMENT_ID_STATE_NAME, deploymentId); // keep that output for external dependencies
35973589
}
35983590
catch (error) {
35993591
core.error(error);
36003592
core.setFailed(`Create deployment failed: ${JSON.stringify(error, null, 2)}`);
36013593
throw error;
36023594
}
36033595
break;
3604-
case 'finish':
3605-
try {
3606-
yield finish_1.finish(client, Number(deploymentId), status);
3607-
}
3608-
catch (error) {
3609-
core.error(error);
3610-
core.setFailed(`Finish deployment failed: ${JSON.stringify(error, null, 2)}`);
3611-
throw error;
3612-
}
3613-
break;
36143596
case 'delete':
36153597
try {
36163598
yield delete_1.deleteDeployment(client, Number(deploymentId));
@@ -3639,37 +3621,6 @@ if (process.env.NODE_ENV !== 'test')
36393621
run(); // eslint-disable-line @typescript-eslint/no-floating-promises
36403622

36413623

3642-
/***/ }),
3643-
3644-
/***/ 209:
3645-
/***/ (function(__unusedmodule, exports, __webpack_require__) {
3646-
3647-
"use strict";
3648-
3649-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3650-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3651-
return new (P || (P = Promise))(function (resolve, reject) {
3652-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
3653-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
3654-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
3655-
step((generator = generator.apply(thisArg, _arguments || [])).next());
3656-
});
3657-
};
3658-
Object.defineProperty(exports, "__esModule", { value: true });
3659-
exports.finish = void 0;
3660-
const github_1 = __webpack_require__(469);
3661-
function finish(client, deploymentId, status) {
3662-
return __awaiter(this, void 0, void 0, function* () {
3663-
const statuses = yield client.repos.listDeploymentStatuses(Object.assign(Object.assign({}, github_1.context.repo), { deployment_id: deploymentId }));
3664-
const lastStatus = statuses.data.sort((a, b) => a.id - b.id).slice(-1)[0];
3665-
console.log(`last status for deployment_id '${deploymentId}': ${JSON.stringify(lastStatus, null, 2)}`);
3666-
const statusResult = yield client.repos.createDeploymentStatus(Object.assign(Object.assign({}, github_1.context.repo), { deployment_id: deploymentId, state: status, environment_url: lastStatus.environment_url, log_url: lastStatus.log_url }));
3667-
console.log(`created deployment status: ${JSON.stringify(statusResult.data, null, 2)}`);
3668-
});
3669-
}
3670-
exports.finish = finish;
3671-
3672-
36733624
/***/ }),
36743625

36753626
/***/ 211:
@@ -8544,6 +8495,48 @@ exports.deleteDeployment = deleteDeployment;
85448495

85458496
module.exports = require("http");
85468497

8498+
/***/ }),
8499+
8500+
/***/ 611:
8501+
/***/ (function(__unusedmodule, exports, __webpack_require__) {
8502+
8503+
"use strict";
8504+
8505+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
8506+
if (k2 === undefined) k2 = k;
8507+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
8508+
}) : (function(o, m, k, k2) {
8509+
if (k2 === undefined) k2 = k;
8510+
o[k2] = m[k];
8511+
}));
8512+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
8513+
Object.defineProperty(o, "default", { enumerable: true, value: v });
8514+
}) : function(o, v) {
8515+
o["default"] = v;
8516+
});
8517+
var __importStar = (this && this.__importStar) || function (mod) {
8518+
if (mod && mod.__esModule) return mod;
8519+
var result = {};
8520+
if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
8521+
__setModuleDefault(result, mod);
8522+
return result;
8523+
};
8524+
Object.defineProperty(exports, "__esModule", { value: true });
8525+
exports.getInput = exports.DEPLOYMENT_ID_STATE_NAME = void 0;
8526+
const core = __importStar(__webpack_require__(470));
8527+
exports.DEPLOYMENT_ID_STATE_NAME = 'deployment_id';
8528+
// nullify getInput empty results
8529+
// to allow coalescence ?? operator
8530+
function getInput(name, options) {
8531+
const result = core.getInput(name, options);
8532+
if (result === '') {
8533+
return null;
8534+
}
8535+
return result;
8536+
}
8537+
exports.getInput = getInput;
8538+
8539+
85478540
/***/ }),
85488541

85498542
/***/ 614:

0 commit comments

Comments
 (0)