2019-11-19 23:06:27 +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 _ _importStar = ( this && this . _ _importStar ) || function ( mod ) {
if ( mod && mod . _ _esModule ) return mod ;
var result = { } ;
if ( mod != null ) for ( var k in mod ) if ( Object . hasOwnProperty . call ( mod , k ) ) result [ k ] = mod [ k ] ;
result [ "default" ] = mod ;
return result ;
} ;
Object . defineProperty ( exports , "__esModule" , { value : true } ) ;
const core = _ _importStar ( require ( "@actions/core" ) ) ;
2020-01-12 09:26:08 +08:00
const execute _1 = require ( "./execute" ) ;
2019-11-19 23:06:27 +08:00
const util _1 = require ( "./util" ) ;
const constants _1 = require ( "./constants" ) ;
/ * * G e n e r a t e s t h e b r a n c h i f i t d o e s n ' t e x i s t o n t h e r e m o t e .
* @ returns { Promise }
* /
function init ( ) {
return _ _awaiter ( this , void 0 , void 0 , function * ( ) {
try {
2020-01-12 09:26:08 +08:00
if ( util _1 . isNullOrUndefined ( constants _1 . action . accessToken ) &&
util _1 . isNullOrUndefined ( constants _1 . action . gitHubToken ) ) {
2019-11-19 23:06:27 +08:00
return core . setFailed ( "You must provide the action with either a Personal Access Token or the GitHub Token secret in order to deploy." ) ;
}
if ( constants _1 . action . build . startsWith ( "/" ) || constants _1 . action . build . startsWith ( "./" ) ) {
return core . setFailed ( ` The deployment folder cannot be prefixed with '/' or './'. Instead reference the folder name directly. ` ) ;
}
2020-01-12 09:26:08 +08:00
yield execute _1 . execute ( ` git init ` , constants _1 . workspace ) ;
yield execute _1 . execute ( ` git config user.name ${ constants _1 . action . name } ` , constants _1 . workspace ) ;
yield execute _1 . execute ( ` git config user.email ${ constants _1 . action . email } ` , constants _1 . workspace ) ;
2020-01-15 04:17:26 +08:00
yield execute _1 . execute ( ` git remote rm origin ` , constants _1 . workspace ) ;
yield execute _1 . execute ( ` git remote add origin ${ constants _1 . repositoryPath } ` , constants _1 . workspace ) ;
2020-01-12 09:26:08 +08:00
yield execute _1 . execute ( ` git fetch ` , constants _1 . workspace ) ;
2019-11-19 23:06:27 +08:00
}
catch ( error ) {
core . setFailed ( ` There was an error initializing the repository: ${ error } ` ) ;
}
finally {
return Promise . resolve ( "Initialization step complete..." ) ;
}
} ) ;
}
exports . init = init ;
2019-12-22 05:51:39 +08:00
/ * * S w i t c h e s t o t h e b a s e b r a n c h .
* @ returns { Promise }
* /
function switchToBaseBranch ( ) {
return _ _awaiter ( this , void 0 , void 0 , function * ( ) {
2020-01-14 23:04:59 +08:00
yield execute _1 . execute ( ` git checkout --progress --force ${ constants _1 . action . baseBranch ? constants _1 . action . baseBranch : constants _1 . action . defaultBranch } ` , constants _1 . workspace ) ;
2019-12-22 05:51:39 +08:00
return Promise . resolve ( "Switched to the base branch..." ) ;
} ) ;
}
exports . switchToBaseBranch = switchToBaseBranch ;
2019-11-19 23:06:27 +08:00
/ * * G e n e r a t e s t h e b r a n c h i f i t d o e s n ' t e x i s t o n t h e r e m o t e .
* @ returns { Promise }
* /
function generateBranch ( ) {
return _ _awaiter ( this , void 0 , void 0 , function * ( ) {
try {
2019-12-22 03:58:59 +08:00
console . log ( ` Creating ${ constants _1 . action . branch } branch... 🔧 ` ) ;
2019-12-22 05:51:39 +08:00
yield switchToBaseBranch ( ) ;
2020-01-14 23:04:59 +08:00
yield execute _1 . execute ( ` git checkout --orphan ${ constants _1 . action . branch } ` , constants _1 . workspace ) ;
2020-01-12 09:26:08 +08:00
yield execute _1 . execute ( ` git reset --hard ` , constants _1 . workspace ) ;
yield execute _1 . execute ( ` git commit --allow-empty -m "Initial ${ constants _1 . action . branch } commit." ` , constants _1 . workspace ) ;
yield execute _1 . execute ( ` git push ${ constants _1 . repositoryPath } ${ constants _1 . action . branch } ` , constants _1 . workspace ) ;
yield execute _1 . execute ( ` git fetch ` , constants _1 . workspace ) ;
2019-11-19 23:06:27 +08:00
}
catch ( error ) {
2019-12-22 03:58:59 +08:00
core . setFailed ( ` There was an error creating the deployment branch: ${ error } ❌ ` ) ;
2019-11-19 23:06:27 +08:00
}
finally {
2019-12-22 03:58:59 +08:00
return Promise . resolve ( "Deployment branch creation step complete... ✅" ) ;
2019-11-19 23:06:27 +08:00
}
} ) ;
}
exports . generateBranch = generateBranch ;
/ * * R u n s t h e n e c e s s a r y s t e p s t o m a k e t h e d e p l o y m e n t .
* @ returns { Promise }
* /
function deploy ( ) {
return _ _awaiter ( this , void 0 , void 0 , function * ( ) {
const temporaryDeploymentDirectory = "gh-action-temp-deployment-folder" ;
const temporaryDeploymentBranch = "gh-action-temp-deployment-branch" ;
/ *
Checks to see if the remote exists prior to deploying .
If the branch doesn ' t exist it gets created here as an orphan .
* /
2020-01-12 09:26:08 +08:00
const branchExists = yield execute _1 . execute ( ` git ls-remote --heads ${ constants _1 . repositoryPath } ${ constants _1 . action . branch } | wc -l ` , constants _1 . workspace ) ;
2019-11-19 23:06:27 +08:00
if ( ! branchExists ) {
console . log ( "Deployment branch does not exist. Creating...." ) ;
yield generateBranch ( ) ;
}
// Checks out the base branch to begin the deployment process.
2019-12-22 05:51:39 +08:00
yield switchToBaseBranch ( ) ;
2020-01-12 09:26:08 +08:00
yield execute _1 . execute ( ` git fetch ${ constants _1 . repositoryPath } ` , constants _1 . workspace ) ;
yield execute _1 . execute ( ` git worktree add --checkout ${ temporaryDeploymentDirectory } origin/ ${ constants _1 . action . branch } ` , constants _1 . workspace ) ;
2019-12-21 06:36:30 +08:00
// Ensures that items that need to be excluded from the clean job get parsed.
let excludes = "" ;
if ( constants _1 . action . clean && constants _1 . action . cleanExclude ) {
try {
const excludedItems = JSON . parse ( constants _1 . action . cleanExclude ) ;
excludedItems . forEach ( ( item ) => ( excludes += ` --exclude ${ item } ` ) ) ;
}
catch ( _a ) {
2019-12-22 03:58:59 +08:00
console . log ( "There was an error parsing your CLEAN_EXCLUDE items. Please refer to the README for more details. ❌" ) ;
2019-12-21 06:36:30 +08:00
}
}
2019-11-19 23:06:27 +08:00
/ *
Pushes all of the build files into the deployment directory .
2019-11-22 22:58:00 +08:00
Allows the user to specify the root if '.' is provided .
rysync is used to prevent file duplication . * /
2020-01-12 09:26:08 +08:00
yield execute _1 . execute ( ` rsync -q -av --progress ${ constants _1 . action . build } /. ${ constants _1 . action . targetFolder
2019-12-16 23:29:29 +08:00
? ` ${ temporaryDeploymentDirectory } / ${ constants _1 . action . targetFolder } `
2019-12-21 06:36:30 +08:00
: temporaryDeploymentDirectory } $ { constants _1 . action . clean
? ` --delete ${ excludes } --exclude CNAME --exclude .nojekyll `
: "" } -- exclude . git -- exclude . github $ { constants _1 . action . build === constants _1 . root ? ` --exclude ${ temporaryDeploymentDirectory } ` : "" } ` , constants_1.workspace);
2020-01-12 09:26:08 +08:00
const hasFilesToCommit = yield execute _1 . execute ( ` git status --porcelain ` , temporaryDeploymentDirectory ) ;
2019-11-20 22:21:36 +08:00
if ( ! hasFilesToCommit && ! constants _1 . isTest ) {
2019-12-22 03:58:59 +08:00
console . log ( "There is nothing to commit. Exiting... ✅" ) ;
2019-11-20 07:07:27 +08:00
return Promise . resolve ( ) ;
}
2019-11-19 23:06:27 +08:00
// Commits to GitHub.
2020-01-12 09:26:08 +08:00
yield execute _1 . execute ( ` git add --all . ` , temporaryDeploymentDirectory ) ;
2020-01-14 23:04:59 +08:00
yield execute _1 . execute ( ` git checkout -b ${ temporaryDeploymentBranch } ` , temporaryDeploymentDirectory ) ;
2020-01-12 09:26:08 +08:00
yield execute _1 . execute ( ` git commit -m "Deploying to ${ constants _1 . action . branch } from ${ constants _1 . action . baseBranch } ${ process . env . GITHUB _SHA } " --quiet ` , temporaryDeploymentDirectory ) ;
yield execute _1 . execute ( ` git push --force ${ constants _1 . repositoryPath } ${ temporaryDeploymentBranch } : ${ constants _1 . action . branch } ` , temporaryDeploymentDirectory ) ;
2019-12-22 03:58:59 +08:00
// Cleans up temporary files/folders and restores the git state.
2019-12-22 05:51:39 +08:00
console . log ( "Running post deployment cleanup jobs... 🔧" ) ;
2020-01-12 09:26:08 +08:00
yield execute _1 . execute ( ` rm -rf ${ temporaryDeploymentDirectory } ` , constants _1 . workspace ) ;
yield execute _1 . execute ( ` git checkout --progress --force ${ constants _1 . action . defaultBranch } ` , constants _1 . workspace ) ;
2019-11-19 23:06:27 +08:00
return Promise . resolve ( "Commit step complete..." ) ;
} ) ;
}
exports . deploy = deploy ;