Swap out the Pages Deployment creation interaction for the wrapped one from api-client

Also remove requestedDeployment field in favor of deploymentInfo.pending
This commit is contained in:
James M. Greene
2023-02-22 08:15:23 -06:00
parent c2918206ce
commit e7873ec09d

View File

@@ -24,7 +24,6 @@ class Deployment {
this.actionsId = context.actionsId this.actionsId = context.actionsId
this.githubToken = context.githubToken this.githubToken = context.githubToken
this.workflowRun = context.workflowRun this.workflowRun = context.workflowRun
this.requestedDeployment = false
this.deploymentInfo = null this.deploymentInfo = null
this.githubApiUrl = context.githubApiUrl this.githubApiUrl = context.githubApiUrl
this.githubServerUrl = context.githubServerUrl this.githubServerUrl = context.githubServerUrl
@@ -47,30 +46,25 @@ class Deployment {
artifactName: this.artifactName artifactName: this.artifactName
}) })
const payload = { const deployment = await createPagesDeployment({
artifact_url: artifactUrl, githubToken: this.githubToken,
pages_build_version: this.buildVersion, artifactUrl,
oidc_token: idToken buildVersion: this.buildVersion,
} idToken,
if (this.isPreview === true) { isPreview: this.isPreview
payload.preview = true
}
core.info(`Creating deployment with payload:\n${JSON.stringify(payload, null, '\t')}`)
const response = await axios.post(pagesDeployEndpoint, payload, {
headers: {
Accept: 'application/vnd.github.v3+json',
Authorization: `Bearer ${this.githubToken}`,
'Content-type': 'application/json'
}
}) })
this.requestedDeployment = true this.deploymentInfo = {
core.info(`Created deployment for ${this.buildVersion}`) ...deployment,
if (response && response.data) { id: deployment?.['status_url']?.split('/')?.pop() || this.buildVersion,
core.info(JSON.stringify(response.data)) pending: true
this.deploymentInfo = response.data
} }
core.info(`Created deployment for ${this.buildVersion}, ID: ${this.deploymentInfo.id}`)
core.info(JSON.stringify(deployment))
return deployment
} catch (error) { } catch (error) {
core.info(error.stack) core.error(error.stack)
// output raw error in debug mode. // output raw error in debug mode.
core.debug(JSON.stringify(error)) core.debug(JSON.stringify(error))
@@ -87,14 +81,14 @@ class Deployment {
} }
errorMessage += `Responded with: ${message}` errorMessage += `Responded with: ${message}`
} else if (error.response.status == 403) { } else if (error.response.status == 403) {
errorMessage += `Ensure GITHUB_TOKEN has permission "pages: write".` errorMessage += 'Ensure GITHUB_TOKEN has permission "pages: write".'
} else if (error.response.status == 404) { } else if (error.response.status == 404) {
const pagesSettingsUrl = `${this.githubServerUrl}/${this.repositoryNwo}/settings/pages` const pagesSettingsUrl = `${this.githubServerUrl}/${this.repositoryNwo}/settings/pages`
errorMessage += `Ensure GitHub Pages has been enabled: ${pagesSettingsUrl}` errorMessage += `Ensure GitHub Pages has been enabled: ${pagesSettingsUrl}`
} else if (error.response.status >= 500) { } else if (error.response.status >= 500) {
errorMessage += `Server error, is githubstatus.com reporting a Pages outage? Please re-run the deployment at a later time.` errorMessage += 'Server error, is githubstatus.com reporting a Pages outage? Please re-run the deployment at a later time.'
} }
throw errorMessage throw new Error(errorMessage)
} else { } else {
throw error throw error
} }
@@ -138,16 +132,19 @@ class Deployment {
if (res.data.status == 'succeed') { if (res.data.status == 'succeed') {
core.info('Reported success!') core.info('Reported success!')
core.setOutput('status', 'succeed') core.setOutput('status', 'succeed')
if (this.deploymentInfo) { this.deploymentInfo.pending = false }
break break
} else if (res.data.status == 'deployment_failed') { } else if (res.data.status == 'deployment_failed') {
// Fall into permanent error, it may be caused by ongoing incident or malicious deployment content or exhausted automatic retry times. // Fall into permanent error, it may be caused by ongoing incident or malicious deployment content or exhausted automatic retry times.
core.setFailed('Deployment failed, try again later.') core.setFailed('Deployment failed, try again later.')
if (this.deploymentInfo) { this.deploymentInfo.pending = false }
break break
} else if (res.data.status == 'deployment_content_failed') { } else if (res.data.status == 'deployment_content_failed') {
// The uploaded artifact is invalid. // The uploaded artifact is invalid.
core.setFailed( core.setFailed(
'Artifact could not be deployed. Please ensure the content does not contain any hard links, symlinks and total size is less than 10GB.' 'Artifact could not be deployed. Please ensure the content does not contain any hard links, symlinks and total size is less than 10GB.'
) )
if (this.deploymentInfo) { this.deploymentInfo.pending = false }
break break
} else if (errorStatus[res.data.status]) { } else if (errorStatus[res.data.status]) {
// A temporary error happened, will query the status again // A temporary error happened, will query the status again
@@ -196,8 +193,8 @@ class Deployment {
} }
async cancel() { async cancel() {
// Don't attemp to cancel if no deployment was created // Don't attempt to cancel if no deployment was created
if (!this.requestedDeployment) { if (!this.deploymentInfo?.pending) {
return return
} }
@@ -216,6 +213,9 @@ class Deployment {
} }
) )
core.info(`Deployment cancelled with ${pagesCancelDeployEndpoint}`) core.info(`Deployment cancelled with ${pagesCancelDeployEndpoint}`)
if (this.deploymentInfo) {
this.deploymentInfo.pending = false
}
} catch (error) { } catch (error) {
core.setFailed(error) core.setFailed(error)
if (error.response && error.response.data) { if (error.response && error.response.data) {