2021-02-08 12:58:32 +08:00
"use strict" ;
var _ _awaiter = ( this && this . _ _awaiter ) || function ( thisArg , _arguments , P , generator ) {
function adopt ( value ) { return value instanceof P ? value : new P ( function ( resolve ) { resolve ( value ) ; } ) ; }
return new ( P || ( P = Promise ) ) ( function ( resolve , reject ) {
function fulfilled ( value ) { try { step ( generator . next ( value ) ) ; } catch ( e ) { reject ( e ) ; } }
function rejected ( value ) { try { step ( generator [ "throw" ] ( value ) ) ; } catch ( e ) { reject ( e ) ; } }
function step ( result ) { result . done ? resolve ( result . value ) : adopt ( result . value ) . then ( fulfilled , rejected ) ; }
step ( ( generator = generator . apply ( thisArg , _arguments || [ ] ) ) . next ( ) ) ;
} ) ;
} ;
var _ _importDefault = ( this && this . _ _importDefault ) || function ( mod ) {
return ( mod && mod . _ _esModule ) ? mod : { "default" : mod } ;
} ;
Object . defineProperty ( exports , "__esModule" , { value : true } ) ;
exports . deploy = exports . init = void 0 ;
const core _1 = require ( "@actions/core" ) ;
const io _1 = require ( "@actions/io" ) ;
const fs _1 = _ _importDefault ( require ( "fs" ) ) ;
const constants _1 = require ( "./constants" ) ;
const execute _1 = require ( "./execute" ) ;
const worktree _1 = require ( "./worktree" ) ;
const util _1 = require ( "./util" ) ;
/* Initializes git in the workspace. */
function init ( action ) {
return _ _awaiter ( this , void 0 , void 0 , function * ( ) {
try {
2021-11-18 20:58:07 +08:00
( 0 , core _1 . info ) ( ` Deploying using ${ action . tokenType } … 🔑 ` ) ;
( 0 , core _1 . info ) ( 'Configuring git…' ) ;
yield ( 0 , execute _1 . execute ) ( ` git config user.name " ${ action . name } " ` , action . workspace , action . silent ) ;
yield ( 0 , execute _1 . execute ) ( ` git config user.email " ${ action . email ? action . email : '<>' } " ` , action . workspace , action . silent ) ;
2021-02-08 12:58:32 +08:00
try {
if ( ( process . env . CI && ! action . sshKey ) || action . isTest ) {
/ * E n s u r e s t h a t p r e v i o u s l y s e t G i t c o n f i g s d o n o t i n t e r f e r e w i t h t h e d e p l o y m e n t .
Only runs in the GitHub Actions CI environment if a user is not using an SSH key .
* /
2021-11-18 20:58:07 +08:00
yield ( 0 , execute _1 . execute ) ( ` git config --local --unset-all http.https:// ${ action . hostname } /.extraheader ` , action . workspace , action . silent ) ;
2021-02-08 12:58:32 +08:00
}
if ( action . isTest === constants _1 . TestFlag . UNABLE _TO _UNSET _GIT _CONFIG ) {
throw new Error ( ) ;
}
}
catch ( _a ) {
2021-11-18 20:58:07 +08:00
( 0 , core _1 . info ) ( 'Unable to unset previous git config authentication as it may not exist, continuing…' ) ;
2021-02-08 12:58:32 +08:00
}
try {
2021-11-18 20:58:07 +08:00
yield ( 0 , execute _1 . execute ) ( ` git remote rm origin ` , action . workspace , action . silent ) ;
2021-02-08 12:58:32 +08:00
if ( action . isTest === constants _1 . TestFlag . UNABLE _TO _REMOVE _ORIGIN ) {
throw new Error ( ) ;
}
}
catch ( _b ) {
2021-11-18 20:58:07 +08:00
( 0 , core _1 . info ) ( 'Attempted to remove origin but failed, continuing…' ) ;
2021-02-08 12:58:32 +08:00
}
2021-11-18 20:58:07 +08:00
yield ( 0 , execute _1 . execute ) ( ` git remote add origin ${ action . repositoryPath } ` , action . workspace , action . silent ) ;
( 0 , core _1 . info ) ( 'Git configured… 🔧' ) ;
2021-02-08 12:58:32 +08:00
}
catch ( error ) {
2021-11-18 20:58:07 +08:00
throw new Error ( ` There was an error initializing the repository: ${ ( 0 , util _1 . suppressSensitiveInformation ) ( ( 0 , util _1 . extractErrorMessage ) ( error ) , action ) } ❌ ` ) ;
2021-02-08 12:58:32 +08:00
}
} ) ;
}
exports . init = init ;
/* Runs the necessary steps to make the deployment. */
function deploy ( action ) {
return _ _awaiter ( this , void 0 , void 0 , function * ( ) {
const temporaryDeploymentDirectory = 'github-pages-deploy-action-temp-deployment-folder' ;
const temporaryDeploymentBranch = ` github-pages-deploy-action/ ${ Math . random ( )
. toString ( 36 )
. substr ( 2 , 9 ) } ` ;
2021-11-18 20:58:07 +08:00
( 0 , core _1 . info ) ( 'Starting to commit changes…' ) ;
2021-02-08 12:58:32 +08:00
try {
2021-11-18 20:58:07 +08:00
const commitMessage = ! ( 0 , util _1 . isNullOrUndefined ) ( action . commitMessage )
2021-02-08 12:58:32 +08:00
? action . commitMessage
: ` Deploying to ${ action . branch } ${ process . env . GITHUB _SHA
? ` from @ ${ process . env . GITHUB _REPOSITORY } @ ${ process . env . GITHUB _SHA } `
: '' } 🚀 ` ;
// Checks to see if the remote exists prior to deploying.
const branchExists = action . isTest & constants _1 . TestFlag . HAS _REMOTE _BRANCH ||
2021-11-18 20:58:07 +08:00
( yield ( 0 , execute _1 . execute ) ( ` git ls-remote --heads ${ action . repositoryPath } refs/heads/ ${ action . branch } ` , action . workspace , action . silent ) ) ;
yield ( 0 , worktree _1 . generateWorktree ) ( action , temporaryDeploymentDirectory , branchExists ) ;
2021-02-08 12:58:32 +08:00
// Ensures that items that need to be excluded from the clean job get parsed.
let excludes = '' ;
if ( action . clean && action . cleanExclude ) {
for ( const item of action . cleanExclude ) {
excludes += ` --exclude ${ item } ` ;
}
}
if ( action . targetFolder ) {
2021-11-18 20:58:07 +08:00
( 0 , core _1 . info ) ( ` Creating target folder if it doesn't already exist… 📌 ` ) ;
yield ( 0 , io _1 . mkdirP ) ( ` ${ temporaryDeploymentDirectory } / ${ action . targetFolder } ` ) ;
2021-02-08 12:58:32 +08:00
}
/ *
Pushes all of the build files into the deployment directory .
Allows the user to specify the root if '.' is provided .
rsync is used to prevent file duplication . * /
2021-11-18 20:58:07 +08:00
yield ( 0 , execute _1 . execute ) ( ` rsync -q -av --checksum --progress ${ action . folderPath } /. ${ action . targetFolder
2021-02-08 12:58:32 +08:00
? ` ${ temporaryDeploymentDirectory } / ${ action . targetFolder } `
: temporaryDeploymentDirectory } $ { action . clean
? ` --delete ${ excludes } ${ ! fs _1 . default . existsSync ( ` ${ action . folderPath } /CNAME ` )
? '--exclude CNAME'
: '' } $ { ! fs _1 . default . existsSync ( ` ${ action . folderPath } /.nojekyll ` )
? '--exclude .nojekyll'
: '' } `
: '' } -- exclude . ssh -- exclude . git -- exclude . github $ { action . folderPath === action . workspace
? ` --exclude ${ temporaryDeploymentDirectory } `
: '' } ` , action.workspace, action.silent);
2021-08-26 22:04:42 +08:00
if ( action . singleCommit ) {
2021-11-18 20:58:07 +08:00
yield ( 0 , execute _1 . execute ) ( ` git add --all . ` , ` ${ action . workspace } / ${ temporaryDeploymentDirectory } ` , action . silent ) ;
2021-08-26 22:04:42 +08:00
}
2021-02-08 12:58:32 +08:00
// Use git status to check if we have something to commit.
// Special case is singleCommit with existing history, when
// we're really interested if the diff against the upstream branch
// changed.
const checkGitStatus = branchExists && action . singleCommit
? ` git diff origin/ ${ action . branch } `
: ` git status --porcelain ` ;
2021-11-18 20:58:07 +08:00
( 0 , core _1 . info ) ( ` Checking if there are files to commit… ` ) ;
2021-02-08 12:58:32 +08:00
const hasFilesToCommit = action . isTest & constants _1 . TestFlag . HAS _CHANGED _FILES ||
2021-11-18 20:58:07 +08:00
( yield ( 0 , execute _1 . execute ) ( checkGitStatus , ` ${ action . workspace } / ${ temporaryDeploymentDirectory } ` , true // This output is always silenced due to the large output it creates.
2021-02-08 12:58:32 +08:00
) ) ;
2021-05-10 22:08:09 +08:00
if ( ( ! action . singleCommit && ! hasFilesToCommit ) ||
// Ignores the case where single commit is true with a target folder to prevent incorrect early exiting.
( action . singleCommit && ! action . targetFolder && ! hasFilesToCommit ) ) {
2021-02-08 12:58:32 +08:00
return constants _1 . Status . SKIPPED ;
}
// Commits to GitHub.
2021-11-18 20:58:07 +08:00
yield ( 0 , execute _1 . execute ) ( ` git add --all . ` , ` ${ action . workspace } / ${ temporaryDeploymentDirectory } ` , action . silent ) ;
yield ( 0 , execute _1 . execute ) ( ` git checkout -b ${ temporaryDeploymentBranch } ` , ` ${ action . workspace } / ${ temporaryDeploymentDirectory } ` , action . silent ) ;
yield ( 0 , execute _1 . execute ) ( ` git commit -m " ${ commitMessage } " --quiet --no-verify ` , ` ${ action . workspace } / ${ temporaryDeploymentDirectory } ` , action . silent ) ;
2021-02-08 12:58:32 +08:00
if ( ! action . dryRun ) {
2021-11-18 20:58:07 +08:00
yield ( 0 , execute _1 . execute ) ( ` git push --force ${ action . repositoryPath } ${ temporaryDeploymentBranch } : ${ action . branch } ` , ` ${ action . workspace } / ${ temporaryDeploymentDirectory } ` , action . silent ) ;
2021-02-08 12:58:32 +08:00
}
2021-11-18 20:58:07 +08:00
( 0 , core _1 . info ) ( ` Changes committed to the ${ action . branch } branch… 📦 ` ) ;
2021-02-08 12:58:32 +08:00
return constants _1 . Status . SUCCESS ;
}
catch ( error ) {
2021-11-18 20:58:07 +08:00
throw new Error ( ` The deploy step encountered an error: ${ ( 0 , util _1 . suppressSensitiveInformation ) ( ( 0 , util _1 . extractErrorMessage ) ( error ) , action ) } ❌ ` ) ;
2021-02-08 12:58:32 +08:00
}
finally {
// Cleans up temporary files/folders and restores the git state.
2021-11-18 20:58:07 +08:00
( 0 , core _1 . info ) ( 'Running post deployment cleanup jobs… 🗑️' ) ;
yield ( 0 , execute _1 . execute ) ( ` git checkout -B ${ temporaryDeploymentBranch } ` , ` ${ action . workspace } / ${ temporaryDeploymentDirectory } ` , action . silent ) ;
yield ( 0 , execute _1 . execute ) ( ` chmod -R 777 ${ temporaryDeploymentDirectory } ` , action . workspace , action . silent ) ;
yield ( 0 , execute _1 . execute ) ( ` git worktree remove ${ temporaryDeploymentDirectory } --force ` , action . workspace , action . silent ) ;
yield ( 0 , io _1 . rmRF ) ( temporaryDeploymentDirectory ) ;
2021-02-08 12:58:32 +08:00
}
} ) ;
}
exports . deploy = deploy ;