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 ( ) ) ;
} ) ;
} ;
Object . defineProperty ( exports , "__esModule" , { value : true } ) ;
2020-01-20 04:33:14 +08:00
const core _1 = require ( "@actions/core" ) ;
const constants _1 = require ( "./constants" ) ;
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" ) ;
/ * * 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 ) &&
2020-01-20 02:15:19 +08:00
util _1 . isNullOrUndefined ( constants _1 . action . gitHubToken ) &&
util _1 . isNullOrUndefined ( constants _1 . action . ssh ) ) {
2020-01-20 04:33:14 +08:00
core _1 . setFailed ( "You must provide the action with either a Personal Access Token or the GitHub Token secret in order to deploy. If you wish to use an ssh deploy token then you must set SSH to true." ) ;
2020-01-20 05:19:40 +08:00
throw Error ( "No deployment token/method was provided. You must provide the action with either a Personal Access Token or the GitHub Token secret in order to deploy. If you wish to use an ssh deploy token then you must set SSH to true." ) ;
2019-11-19 23:06:27 +08:00
}
if ( constants _1 . action . build . startsWith ( "/" ) || constants _1 . action . build . startsWith ( "./" ) ) {
2020-01-20 04:33:14 +08:00
core _1 . setFailed ( ` The deployment folder cannot be prefixed with '/' or './'. Instead reference the folder name directly. ` ) ;
2020-01-20 05:19:53 +08:00
throw Error ( "Incorrectly formatted build folder. The deployment folder cannot be prefixed with '/' or './'. Instead reference the folder name directly." ) ;
2019-11-19 23:06:27 +08:00
}
2020-01-20 04:33:14 +08:00
console . log ( ` Deploying using ${ constants _1 . tokenType } ... 🔑 ` ) ;
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:14:15 +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-15 03:22:01 +08:00
yield execute _1 . execute ( ` git fetch ` , constants _1 . workspace ) ;
2019-11-19 23:06:27 +08:00
}
catch ( error ) {
2020-01-20 04:33:14 +08:00
console . log ( ` There was an error initializing the repository: ${ error } ` ) ;
2019-11-19 23:06:27 +08:00
}
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 {
2020-01-20 04:33:14 +08:00
if ( util _1 . isNullOrUndefined ( constants _1 . action . branch ) ) {
2020-01-20 05:16:28 +08:00
throw Error ( "Branch is required." ) ;
2020-01-20 04:33:14 +08:00
}
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 ) ;
2020-01-15 03:22:01 +08:00
yield execute _1 . execute ( ` git fetch ` , constants _1 . workspace ) ;
2019-11-19 23:06:27 +08:00
}
catch ( error ) {
2020-01-20 04:33:14 +08:00
core _1 . 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 ) ;
2020-01-20 05:16:28 +08:00
if ( ! branchExists && ! constants _1 . action . isTest ) {
2019-11-19 23:06:27 +08:00
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 `
2020-01-20 02:15:19 +08:00
: "" } -- exclude . ssh -- 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 ) ;
2020-01-20 04:33:14 +08:00
if ( ! hasFilesToCommit && ! constants _1 . action . 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-19 03:41:49 +08:00
yield execute _1 . execute ( ` git commit -m " ${ ! util _1 . isNullOrUndefined ( constants _1 . action . commitMessage )
? constants _1 . action . commitMessage
: ` Deploying to ${ constants _1 . action . branch } from ${ constants _1 . action . baseBranch } ` } - $ { process . env . GITHUB _SHA } 🚀 " -- quiet ` , temporaryDeploymentDirectory);
2020-01-12 09:26:08 +08:00
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 ;