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" ) ) ;
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 {
if ( ! constants _1 . action . accessToken && ! constants _1 . action . gitHubToken ) {
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. ` ) ;
}
yield util _1 . execute ( ` git init ` , constants _1 . workspace ) ;
2019-11-22 23:01:06 +08:00
yield util _1 . execute ( ` git config user.name ${ constants _1 . action . name } ` , constants _1 . workspace ) ;
yield util _1 . execute ( ` git config user.email ${ constants _1 . action . email } ` , 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 ;
/ * * 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 {
console . log ( ` Creating ${ constants _1 . action . branch } branch... ` ) ;
yield util _1 . execute ( ` git switch ${ constants _1 . action . baseBranch || "master" } ` , constants _1 . workspace ) ;
yield util _1 . execute ( ` git switch --orphan ${ constants _1 . action . branch } ` , constants _1 . workspace ) ;
yield util _1 . execute ( ` git reset --hard ` , constants _1 . workspace ) ;
yield util _1 . execute ( ` git commit --allow-empty -m "Initial ${ constants _1 . action . branch } commit." ` , constants _1 . workspace ) ;
yield util _1 . execute ( ` git push ${ constants _1 . repositoryPath } ${ constants _1 . action . branch } ` , constants _1 . workspace ) ;
// Switches back to the base branch.
yield util _1 . execute ( ` git switch ${ constants _1 . action . baseBranch || "master" } ` , constants _1 . workspace ) ;
}
catch ( error ) {
core . setFailed ( ` There was an error creating the deployment branch: ${ error } ` ) ;
}
finally {
return Promise . resolve ( "Deployment branch creation step complete..." ) ;
}
} ) ;
}
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 .
* /
const branchExists = yield util _1 . execute ( ` git ls-remote --heads ${ constants _1 . repositoryPath } ${ constants _1 . action . branch } | wc -l ` , constants _1 . workspace ) ;
if ( ! branchExists ) {
console . log ( "Deployment branch does not exist. Creating...." ) ;
yield generateBranch ( ) ;
}
// Checks out the base branch to begin the deployment process.
yield util _1 . execute ( ` git checkout ${ constants _1 . action . baseBranch || "master" } ` , constants _1 . workspace ) ;
2019-11-27 21:36:19 +08:00
yield util _1 . execute ( ` git fetch ${ constants _1 . repositoryPath } ` , constants _1 . workspace ) ;
2019-11-19 23:06:27 +08:00
yield util _1 . execute ( ` git worktree add --checkout ${ temporaryDeploymentDirectory } origin/ ${ constants _1 . action . branch } ` , constants _1 . workspace ) ;
/ *
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 . * /
2019-12-16 23:29:29 +08:00
yield util _1 . execute ( ` rsync -q -av --progress ${ constants _1 . action . build } /. ${ constants _1 . action . targetFolder
? ` ${ temporaryDeploymentDirectory } / ${ constants _1 . action . targetFolder } `
: temporaryDeploymentDirectory } $ { constants _1 . action . clean ? ` --delete --exclude CNAME --exclude .nojekyll ` : "" } -- exclude . git -- exclude . github $ { constants _1 . action . build === constants _1 . root ? ` --exclude ${ temporaryDeploymentDirectory } ` : "" } ` , constants_1.workspace);
2019-11-20 07:07:27 +08:00
const hasFilesToCommit = yield util _1 . execute ( ` git status --porcelain ` , temporaryDeploymentDirectory ) ;
2019-11-20 22:21:36 +08:00
if ( ! hasFilesToCommit && ! constants _1 . isTest ) {
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.
yield util _1 . execute ( ` git add --all . ` , temporaryDeploymentDirectory ) ;
yield util _1 . execute ( ` git switch -c ${ temporaryDeploymentBranch } ` , temporaryDeploymentDirectory ) ;
yield util _1 . execute ( ` git commit -m "Deploying to ${ constants _1 . action . branch } from ${ constants _1 . action . baseBranch } ${ process . env . GITHUB _SHA } " --quiet ` , temporaryDeploymentDirectory ) ;
yield util _1 . execute ( ` git push --force ${ constants _1 . repositoryPath } ${ temporaryDeploymentBranch } : ${ constants _1 . action . branch } ` , temporaryDeploymentDirectory ) ;
return Promise . resolve ( "Commit step complete..." ) ;
} ) ;
}
exports . deploy = deploy ;