2019-11-08 06:46:04 +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" ) ) ;
2019-11-11 04:27:06 +08:00
const io _1 = require ( "@actions/io" ) ;
2019-11-08 06:46:04 +08:00
const util _1 = require ( "./util" ) ;
const constants _1 = require ( "./constants" ) ;
2019-11-09 21:29:29 +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 }
* /
2019-11-08 06:46:04 +08:00
function init ( ) {
return _ _awaiter ( this , void 0 , void 0 , function * ( ) {
try {
2019-11-09 21:29:29 +08:00
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." ) ;
2019-11-08 06:46:04 +08:00
}
2019-11-11 03:28:04 +08:00
/*if (action.build.startsWith("/ ") || action.build.startsWith(" . / " ) ) {
return core . setFailed (
` The deployment folder cannot be prefixed with '/' or './'. Instead reference the folder name directly. `
) ;
} * /
2019-11-11 04:36:26 +08:00
yield util _1 . execute ( ` git init ` , constants _1 . workspace ) ;
yield util _1 . execute ( ` git config user.name ${ constants _1 . action . pusher . name } ` , constants _1 . workspace ) ;
yield util _1 . execute ( ` git config user.email ${ constants _1 . action . pusher . email } ` , constants _1 . workspace ) ;
2019-11-08 06:46:04 +08:00
}
catch ( error ) {
core . setFailed ( ` There was an error initializing the repository: ${ error } ` ) ;
}
finally {
2019-11-09 21:29:29 +08:00
return Promise . resolve ( "Initialization step complete..." ) ;
2019-11-08 06:46:04 +08:00
}
} ) ;
}
exports . init = init ;
2019-11-09 21:29:29 +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 }
* /
2019-11-08 06:46:04 +08:00
function generateBranch ( ) {
return _ _awaiter ( this , void 0 , void 0 , function * ( ) {
try {
console . log ( ` Creating ${ constants _1 . action . branch } branch... ` ) ;
2019-11-09 21:37:58 +08:00
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 ) ;
2019-11-08 06:46:04 +08:00
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 ) ;
2019-11-11 03:16:01 +08:00
// Switches back to the base branch.
yield util _1 . execute ( ` git switch ${ constants _1 . action . baseBranch || "master" } ` , constants _1 . workspace ) ;
2019-11-08 06:46:04 +08:00
}
catch ( error ) {
core . setFailed ( ` There was an error creating the deployment branch: ${ error } ` ) ;
}
finally {
2019-11-09 21:29:29 +08:00
return Promise . resolve ( "Deployment branch creation step complete..." ) ;
2019-11-08 06:46:04 +08:00
}
} ) ;
}
exports . generateBranch = generateBranch ;
2019-11-09 21:29:29 +08:00
/ * * 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 }
* /
2019-11-08 06:46:04 +08:00
function deploy ( ) {
return _ _awaiter ( this , void 0 , void 0 , function * ( ) {
2019-11-11 04:27:06 +08:00
const temporaryDeploymentDirectory = 'gh-action-temp-deployment-folder' ;
const temporaryDeploymentBranch = 'gh-action-temp-deployment-branch' ;
2019-11-08 06:46:04 +08:00
/ *
2019-11-09 21:29:29 +08:00
Checks to see if the remote exists prior to deploying .
If the branch doesn ' t exist it gets created here as an orphan .
2019-11-11 04:27:06 +08:00
* /
const branchExists = yield util _1 . execute ( ` git ls-remote --heads ${ constants _1 . repositoryPath } ${ constants _1 . action . branch } | wc -l ` , constants _1 . workspace ) ;
2019-11-08 06:46:04 +08:00
if ( ! branchExists ) {
2019-11-11 04:27:06 +08:00
console . log ( "Deployment branch does not exist. Creating...." ) ;
yield generateBranch ( ) ;
}
2019-11-11 04:42:45 +08:00
// Checks out the base branch to begin the deployment process.
yield util _1 . execute ( ` git checkout ${ constants _1 . action . baseBranch || "master" } --- ` , constants _1 . workspace ) ;
yield util _1 . execute ( ` git fetch origin ` , constants _1 . workspace ) ;
2019-11-11 04:27:06 +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 .
Allows the user to specify the root if '.' is provided . * /
yield io _1 . cp ( ` ${ constants _1 . action . build } /. ` , temporaryDeploymentDirectory , {
recursive : true ,
force : true
} ) ;
// 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 ) ;
2019-11-09 21:29:29 +08:00
return Promise . resolve ( "Commit step complete..." ) ;
2019-11-08 06:46:04 +08:00
} ) ;
}
exports . deploy = deploy ;