Skip to content

Commit 114f9cc

Browse files
authored
Merge pull request #56 from actions/cancel
Explicitly handle cancellation on errors
2 parents 0dfe0f0 + c7c77bb commit 114f9cc

File tree

6 files changed

+115
-54
lines changed

6 files changed

+115
-54
lines changed

dist/index.js

Lines changed: 38 additions & 25 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pre/index.js

Lines changed: 37 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pre/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/deployment.js

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ class Deployment {
109109
const statusUrl =
110110
this.deploymentInfo != null
111111
? this.deploymentInfo['status_url']
112-
: `${this.githubApiUrl}/repos/${this.repositoryNwo}/pages/deployment/status/${process.env['GITHUB_SHA']}`
112+
: `${this.githubApiUrl}/repos/${this.repositoryNwo}/pages/deployment/status/${this.buildVersion}`
113113
core.setOutput('page_url', this.deploymentInfo != null ? this.deploymentInfo['page_url'] : '')
114114
const timeout = Number(core.getInput('timeout'))
115115
const reportingInterval = Number(core.getInput('reporting_interval'))
@@ -168,13 +168,19 @@ class Deployment {
168168
if (errorCount >= maxErrorCount) {
169169
core.info('Too many errors, aborting!')
170170
core.setFailed('Failed with status code: ' + res.status)
171-
break
171+
172+
// Explicitly cancel the deployment
173+
await this.cancel()
174+
return
172175
}
173176

174177
// Handle timeout
175178
if (Date.now() - startTime >= timeout) {
176179
core.info('Timeout reached, aborting!')
177180
core.setFailed('Timeout reached, aborting!')
181+
182+
// Explicitly cancel the deployment
183+
await this.cancel()
178184
return
179185
}
180186
}
@@ -185,5 +191,34 @@ class Deployment {
185191
}
186192
}
187193
}
194+
195+
async cancel() {
196+
// Don't attemp to cancel if no deployment was created
197+
if (!this.requestedDeployment) {
198+
return
199+
}
200+
201+
// Cancel the deployment
202+
try {
203+
const pagesCancelDeployEndpoint = `${this.githubApiUrl}/repos/${this.repositoryNwo}/pages/deployment/cancel/${this.buildVersion}`
204+
await axios.put(
205+
pagesCancelDeployEndpoint,
206+
{},
207+
{
208+
headers: {
209+
Accept: 'application/vnd.github.v3+json',
210+
Authorization: `Bearer ${this.githubToken}`,
211+
'Content-type': 'application/json'
212+
}
213+
}
214+
)
215+
core.info(`Deployment cancelled with ${pagesCancelDeployEndpoint}`)
216+
} catch (error) {
217+
core.setFailed(error)
218+
if (error.response && error.response.data) {
219+
core.info(JSON.stringify(error.response.data))
220+
}
221+
}
222+
}
188223
}
189224
module.exports = { Deployment }

src/index.js

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,34 +6,12 @@ require('regenerator-runtime/runtime')
66

77
const core = require('@actions/core')
88
// const github = require('@actions/github'); // TODO: Not used until we publish API endpoint to the @action/github package
9-
const axios = require('axios')
109

1110
const { Deployment } = require('./deployment')
1211
const deployment = new Deployment()
1312

14-
// TODO: If the artifact hasn't been created, we can create it and upload to artifact storage ourselves
15-
// const tar = require('tar')
16-
1713
async function cancelHandler(evtOrExitCodeOrError) {
18-
try {
19-
if (deployment.requestedDeployment) {
20-
const pagesCancelDeployEndpoint = `${deployment.githubApiUrl}/repos/${process.env.GITHUB_REPOSITORY}/pages/deployment/cancel/${process.env.GITHUB_SHA}`
21-
await axios.put(
22-
pagesCancelDeployEndpoint,
23-
{},
24-
{
25-
headers: {
26-
Accept: 'application/vnd.github.v3+json',
27-
Authorization: `Bearer ${deployment.githubToken}`,
28-
'Content-type': 'application/json'
29-
}
30-
}
31-
)
32-
core.info(`Deployment cancelled with ${pagesCancelDeployEndpoint}`)
33-
}
34-
} catch (e) {
35-
console.log('Deployment cancellation failed', e)
36-
}
14+
await deployment.cancel()
3715
process.exit(isNaN(+evtOrExitCodeOrError) ? 1 : +evtOrExitCodeOrError)
3816
}
3917

0 commit comments

Comments
 (0)