2023-02-22 07:55:03 -06:00
const core = require ( '@actions/core' )
const github = require ( '@actions/github' )
2023-12-19 14:46:02 -06:00
const { DefaultArtifactClient } = require ( '@actions/artifact' )
2023-03-08 19:43:11 -06:00
2023-12-19 14:46:02 -06:00
async function getArtifactMetadata ( { artifactName } ) {
const artifactClient = new DefaultArtifactClient ( )
2023-10-27 16:13:35 -04:00
try {
2023-12-19 14:46:02 -06:00
core . info ( ` Fetching artifact metadata for ${ artifactName } in this workflow run ` )
const response = await artifactClient . listArtifacts ( )
const filteredArtifacts = response . artifacts . filter ( artifact => artifact . name === artifactName )
const artifactCount = filteredArtifacts . length
2023-10-27 17:40:12 -04:00
core . debug ( ` List artifact count: ${ artifactCount } ` )
if ( artifactCount === 0 ) {
2023-10-30 14:58:12 -04:00
throw new Error (
2023-12-19 14:46:02 -06:00
'No artifacts found for this workflow run. Ensure artifacts are uploaded with actions/artifact@v4 or later.'
2023-10-30 14:58:12 -04:00
)
2023-10-27 17:40:12 -04:00
} else if ( artifactCount > 1 ) {
2023-10-30 14:58:12 -04:00
throw new Error (
2023-12-19 14:46:02 -06:00
` Multiple artifacts unexpectedly found for this workflow run. Artifact count is ${ artifactCount } . `
2023-10-30 14:58:12 -04:00
)
2023-10-27 17:40:12 -04:00
}
2023-10-30 14:58:12 -04:00
2023-12-19 14:46:02 -06:00
const artifact = filteredArtifacts [ 0 ]
2023-10-27 17:40:12 -04:00
core . debug ( ` Artifact: ${ JSON . stringify ( artifact ) } ` )
2023-12-19 14:46:02 -06:00
if ( ! artifact . size ) {
2023-10-27 17:40:12 -04:00
core . warning ( 'Artifact size was not found. Unable to verify if artifact size exceeds the allowed size.' )
}
2023-10-30 14:58:12 -04:00
2023-12-19 14:46:02 -06:00
return artifact
2023-10-27 16:13:35 -04:00
} catch ( error ) {
2023-10-30 15:35:32 -04:00
core . error (
'Fetching artifact metadata failed. Is githubstatus.com reporting issues with API requests, Pages or Actions? Please re-run the deployment at a later time.' ,
error
)
2023-10-27 16:13:35 -04:00
throw error
}
}
2023-10-27 17:50:16 -04:00
async function createPagesDeployment ( { githubToken , artifactId , buildVersion , idToken , isPreview = false } ) {
2023-02-22 07:55:03 -06:00
const octokit = github . getOctokit ( githubToken )
const payload = {
2023-10-27 17:50:16 -04:00
artifact _id : artifactId ,
2023-02-22 07:55:03 -06:00
pages _build _version : buildVersion ,
oidc _token : idToken
}
if ( isPreview === true ) {
payload . preview = true
}
core . info ( ` Creating Pages deployment with payload: \n ${ JSON . stringify ( payload , null , '\t' ) } ` )
try {
2023-03-15 15:33:59 -04:00
const response = await octokit . request ( 'POST /repos/{owner}/{repo}/pages/deployments' , {
2023-02-22 07:55:03 -06:00
owner : github . context . repo . owner ,
repo : github . context . repo . repo ,
... payload
} )
return response . data
} catch ( error ) {
core . error ( 'Creating Pages deployment failed' , error )
throw error
}
}
async function getPagesDeploymentStatus ( { githubToken , deploymentId } ) {
const octokit = github . getOctokit ( githubToken )
core . info ( 'Getting Pages deployment status...' )
try {
2023-03-15 15:33:59 -04:00
const response = await octokit . request ( 'GET /repos/{owner}/{repo}/pages/deployments/{deploymentId}' , {
2023-02-22 07:55:03 -06:00
owner : github . context . repo . owner ,
repo : github . context . repo . repo ,
deploymentId
} )
return response . data
} catch ( error ) {
core . error ( 'Getting Pages deployment status failed' , error )
throw error
}
}
async function cancelPagesDeployment ( { githubToken , deploymentId } ) {
const octokit = github . getOctokit ( githubToken )
core . info ( 'Canceling Pages deployment...' )
try {
2023-03-15 15:33:59 -04:00
const response = await octokit . request ( 'POST /repos/{owner}/{repo}/pages/deployments/{deploymentId}/cancel' , {
2023-02-22 07:55:03 -06:00
owner : github . context . repo . owner ,
repo : github . context . repo . repo ,
deploymentId
} )
return response . data
} catch ( error ) {
core . error ( 'Canceling Pages deployment failed' , error )
throw error
}
}
module . exports = {
2023-10-27 16:13:35 -04:00
getArtifactMetadata ,
2023-02-22 07:55:03 -06:00
createPagesDeployment ,
getPagesDeploymentStatus ,
cancelPagesDeployment
}