-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'js_action' into keep_env
- Loading branch information
Showing
13 changed files
with
728 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,212 @@ | ||
const core = require( '@actions/core' ); | ||
if( typeof wTools === 'undefined' ) | ||
require( '../node_modules/Joined.s' ); | ||
const _ = wTools; | ||
const ChildProcess = require( 'child_process' ); | ||
let common = require( './Common.js' ); | ||
if( _.map.keys( common ).length === 0 ) | ||
{ | ||
const path = require.resolve( './Common.js' ); | ||
common = require.cache[ path ].exports; | ||
} | ||
let GithubActionsParser = null; | ||
|
||
// | ||
|
||
/* | ||
To run commands synchronously and with no `deasync` this wrapper is used because | ||
_.process uses Consequence in each `start*` routine and for synchronous execution it requires `deasync`. | ||
`deasync` is the binary module. To prevent failures during action build and decrease time of setup the action, | ||
we exclude `deasync` and compile action code. | ||
*/ | ||
|
||
function execSyncNonThrowing( command ) | ||
{ | ||
try | ||
{ | ||
return ChildProcess.execSync( command, { stdio : 'pipe' } ); | ||
} | ||
catch( err ) | ||
{ | ||
_.error.attend( err ); | ||
return err; | ||
} | ||
} | ||
|
||
// | ||
|
||
function exists() | ||
{ | ||
return !_.error.is( execSyncNonThrowing( 'docker -v' ) ); | ||
} | ||
|
||
// | ||
|
||
function imageBuild( actionPath, image ) | ||
{ | ||
const docker = this; | ||
|
||
_.sure | ||
( | ||
docker.exists(), | ||
'Current OS has no Docker utility.\n' | ||
+ 'Please, visit ' | ||
+ 'https://docs.github.com/en/actions/using-github-hosted-runners/about-github-hosted-runners#preinstalled-software\n' | ||
+ 'and select valid workflow runner.' | ||
); | ||
|
||
if( image === 'Dockerfile' ) | ||
{ | ||
const actionName = _.path.name( actionPath ); | ||
const imageName = `${ actionName }_repo:${ actionName }_tag`.toLowerCase(); | ||
const dockerfilePath = _.path.nativize( _.path.join( actionPath, 'Dockerfile' ) ); | ||
const command = `docker build -t ${ imageName } -f ${ dockerfilePath } ${ _.path.nativize( actionPath ) }`; | ||
const build = execSyncNonThrowing( command ); | ||
if( _.error.is( build ) ) | ||
throw _.error.brief( build ); | ||
|
||
core.info( `Dockerfile for action : ${ dockerfilePath }.` ); | ||
core.info( command ); | ||
core.info( build.toString() ); | ||
|
||
return imageName; | ||
} | ||
|
||
_.sure | ||
( | ||
false, | ||
`The action does not support requested Docker image type "${ image }".` | ||
+ '\nPlease, open an issue with the request for the feature.' | ||
); | ||
} | ||
|
||
// | ||
|
||
function runCommandForm( imageName, inputs ) | ||
{ | ||
const [ repo, tag ] = imageName.split( ':' ); | ||
_.sure( _.str.defined( repo ) && _.str.defined( tag ), 'Expects image name in format "[repo]:[tag]".' ); | ||
const command = [ `docker run --name ${ tag } --label ${ repo } --workdir /github/workspace --rm` ]; | ||
const env_keys = _.map.keys( JSON.parse( core.getInput( 'env_context' ) ) ); | ||
const inputs_keys = _.map.keys( inputs ); | ||
const postfix_command_envs = | ||
[ | ||
'HOME', | ||
'GITHUB_JOB', | ||
'GITHUB_REF', | ||
'GITHUB_SHA', | ||
'GITHUB_REPOSITORY', | ||
'GITHUB_REPOSITORY_OWNER', | ||
'GITHUB_REPOSITORY_OWNER_ID', | ||
'GITHUB_RUN_ID', | ||
'GITHUB_RUN_NUMBER', | ||
'GITHUB_RETENTION_DAYS', | ||
'GITHUB_RUN_ATTEMPT', | ||
'GITHUB_REPOSITORY_ID', | ||
'GITHUB_ACTOR_ID', | ||
'GITHUB_ACTOR', | ||
'GITHUB_TRIGGERING_ACTOR', | ||
'GITHUB_WORKFLOW', | ||
'GITHUB_HEAD_REF', | ||
'GITHUB_BASE_REF', | ||
'GITHUB_EVENT_NAME', | ||
'GITHUB_SERVER_URL', | ||
'GITHUB_API_URL', | ||
'GITHUB_GRAPHQL_URL', | ||
'GITHUB_REF_NAME', | ||
'GITHUB_REF_PROTECTED', | ||
'GITHUB_REF_TYPE', | ||
'GITHUB_WORKFLOW_REF', | ||
'GITHUB_WORKFLOW_SHA', | ||
'GITHUB_WORKSPACE', | ||
'GITHUB_ACTION', | ||
'GITHUB_EVENT_PATH', | ||
'GITHUB_ACTION_REPOSITORY', | ||
'GITHUB_ACTION_REF', | ||
'GITHUB_PATH', | ||
'GITHUB_ENV', | ||
'GITHUB_STEP_SUMMARY', | ||
'GITHUB_STATE', | ||
'GITHUB_OUTPUT', | ||
'RUNNER_OS', | ||
'RUNNER_ARCH', | ||
'RUNNER_NAME', | ||
'RUNNER_ENVIRONMENT', | ||
'RUNNER_TOOL_CACHE', | ||
'RUNNER_TEMP', | ||
'RUNNER_WORKSPACE', | ||
'ACTIONS_RUNTIME_URL', | ||
'ACTIONS_RUNTIME_TOKEN', | ||
'ACTIONS_CACHE_URL', | ||
'GITHUB_ACTIONS=true', | ||
'CI=true', | ||
]; | ||
const postfix_command_paths = | ||
[ | ||
'"/var/run/docker.sock":"/var/run/docker.sock"', | ||
'"/home/runner/work/_temp/_github_home":"/github/home"', | ||
'"/home/runner/work/_temp/_github_workflow":"/github/workflow"', | ||
'"/home/runner/work/_temp/_runner_file_commands":"/github/file_commands"', | ||
`"${ process.env.GITHUB_WORKSPACE }":"/github/workspace"`, | ||
`"${ process.env.GITHUB_OUTPUT }":"${ process.env.GITHUB_OUTPUT }"`, | ||
]; | ||
|
||
/* */ | ||
|
||
if( env_keys.length > 0 ) | ||
command.push( '-e', env_keys.join( ' -e ' ) ); | ||
if( inputs_keys.length > 0 ) | ||
command.push( '-e', inputs_keys.join( ' -e ' ) ); | ||
command.push( '-e', postfix_command_envs.join( ' -e ' ) ); | ||
command.push( '-v', postfix_command_paths.join( ' -v ' ) ); | ||
command.push( imageName ); | ||
|
||
return command.join( ' ' ); | ||
} | ||
|
||
// | ||
|
||
function commandArgsFrom( args, inputs ) | ||
{ | ||
const result = []; | ||
if( args === undefined ) | ||
return result; | ||
|
||
for( let i = 0 ; i < args.length ; i++ ) | ||
{ | ||
let value = args[ i ]; | ||
if( _.str.is( value ) ) | ||
if( value.startsWith( '${{' ) && value.endsWith( '}}' ) ) | ||
{ | ||
if( GithubActionsParser === null ) | ||
GithubActionsParser = require( 'github-actions-parser' ); | ||
value = GithubActionsParser.evaluateExpression( value, | ||
{ | ||
get : ( name ) => | ||
{ | ||
let context = common.contextGet( name ); | ||
if( _.map.keys( context ).length === 0 && name === 'inputs' ) | ||
return inputs; | ||
return context; | ||
} | ||
}); | ||
} | ||
result.push( value ); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
// -- | ||
// export | ||
// -- | ||
|
||
const Self = | ||
{ | ||
exists, | ||
imageBuild, | ||
runCommandForm, | ||
commandArgsFrom, | ||
}; | ||
|
||
module.exports = Self; |
Oops, something went wrong.