Skip to content

Commit

Permalink
Merge pull request #56 from actions/cancel
Browse files Browse the repository at this point in the history
Explicitly handle cancellation on errors
  • Loading branch information
yoannchaudet committed Aug 24, 2022
2 parents 0dfe0f0 + c7c77bb commit 114f9cc
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 54 deletions.
63 changes: 38 additions & 25 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

39 changes: 37 additions & 2 deletions pre/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pre/index.js.map

Large diffs are not rendered by default.

39 changes: 37 additions & 2 deletions src/deployment.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ class Deployment {
const statusUrl =
this.deploymentInfo != null
? this.deploymentInfo['status_url']
: `${this.githubApiUrl}/repos/${this.repositoryNwo}/pages/deployment/status/${process.env['GITHUB_SHA']}`
: `${this.githubApiUrl}/repos/${this.repositoryNwo}/pages/deployment/status/${this.buildVersion}`
core.setOutput('page_url', this.deploymentInfo != null ? this.deploymentInfo['page_url'] : '')
const timeout = Number(core.getInput('timeout'))
const reportingInterval = Number(core.getInput('reporting_interval'))
Expand Down Expand Up @@ -168,13 +168,19 @@ class Deployment {
if (errorCount >= maxErrorCount) {
core.info('Too many errors, aborting!')
core.setFailed('Failed with status code: ' + res.status)
break

// Explicitly cancel the deployment
await this.cancel()
return
}

// Handle timeout
if (Date.now() - startTime >= timeout) {
core.info('Timeout reached, aborting!')
core.setFailed('Timeout reached, aborting!')

// Explicitly cancel the deployment
await this.cancel()
return
}
}
Expand All @@ -185,5 +191,34 @@ class Deployment {
}
}
}

async cancel() {
// Don't attemp to cancel if no deployment was created
if (!this.requestedDeployment) {
return
}

// Cancel the deployment
try {
const pagesCancelDeployEndpoint = `${this.githubApiUrl}/repos/${this.repositoryNwo}/pages/deployment/cancel/${this.buildVersion}`
await axios.put(
pagesCancelDeployEndpoint,
{},
{
headers: {
Accept: 'application/vnd.github.v3+json',
Authorization: `Bearer ${this.githubToken}`,
'Content-type': 'application/json'
}
}
)
core.info(`Deployment cancelled with ${pagesCancelDeployEndpoint}`)
} catch (error) {
core.setFailed(error)
if (error.response && error.response.data) {
core.info(JSON.stringify(error.response.data))
}
}
}
}
module.exports = { Deployment }
24 changes: 1 addition & 23 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,12 @@ require('regenerator-runtime/runtime')

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

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

// TODO: If the artifact hasn't been created, we can create it and upload to artifact storage ourselves
// const tar = require('tar')

async function cancelHandler(evtOrExitCodeOrError) {
try {
if (deployment.requestedDeployment) {
const pagesCancelDeployEndpoint = `${deployment.githubApiUrl}/repos/${process.env.GITHUB_REPOSITORY}/pages/deployment/cancel/${process.env.GITHUB_SHA}`
await axios.put(
pagesCancelDeployEndpoint,
{},
{
headers: {
Accept: 'application/vnd.github.v3+json',
Authorization: `Bearer ${deployment.githubToken}`,
'Content-type': 'application/json'
}
}
)
core.info(`Deployment cancelled with ${pagesCancelDeployEndpoint}`)
}
} catch (e) {
console.log('Deployment cancellation failed', e)
}
await deployment.cancel()
process.exit(isNaN(+evtOrExitCodeOrError) ? 1 : +evtOrExitCodeOrError)
}

Expand Down

0 comments on commit 114f9cc

Please sign in to comment.