Skip to content

Commit

Permalink
Merge pull request #151 from dmvict/js_action
Browse files Browse the repository at this point in the history
READY: Extend Docker actions, resolve conditions
  • Loading branch information
dmvict authored Apr 9, 2024
2 parents c3b0492 + f446b6f commit 8382a40
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 20 deletions.
3 changes: 1 addition & 2 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ It is a cause of failed jobs. For this case, the action `wretry.action` can retr
- Can retry single action or single command ( multiline command ), but not both simultaneously.
- Retries `main`, `pre` and `post` stages of external actions.
- Always has `pre` and `post` stages. If external action has `pre` or/and `post` stage, then action run it also.
- Action handles conditions in `JavaScript` actions ( fields `pre-if` and `post-if` ). Some conditions can be unsolvable and then action skips the stage.
- Executes only the main script of Docker actions, excluding pre-entrypoint and post-entrypoint scripts.
- Action handles conditions in `JavaScript` and `Docker` actions ( fields `pre-if` and `post-if` ). Some conditions can be unsolvable and then action skips the stage.
- Resolves external action default inputs from next contexts : `github`, `env`, `job`, `matrix`, `inputs`.
- Retries actions with defined number of attempts ( default is 2 ).
- Retries actions with defined delay between attempts ( default is 0 ).
Expand Down
12 changes: 7 additions & 5 deletions src/Common.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,9 +220,14 @@ function envOptionsSetup( options )

function shouldExit( config, scriptType )
{
if( _.strBegins( config.runs.using, 'node' ) )
const using = config.runs.using;
if( _.strBegins( using, 'node' ) || using === 'docker' )
{
if( !config.runs[ scriptType ])
if( using === 'docker' && scriptType === 'main' )
return false;

const localScriptType = using === 'docker' ? `${ scriptType }-entrypoint` : scriptType;
if( !config.runs[ localScriptType ] )
return true;

if( config.runs[ `${ scriptType }-if` ] )
Expand All @@ -231,9 +236,6 @@ function shouldExit( config, scriptType )
}
}

if( config.runs.using === 'docker' && scriptType !== 'main' )
return true;

return false;
}

Expand Down
5 changes: 3 additions & 2 deletions src/Docker.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,12 @@ function commandArgsFrom( args, options )

//

function runCommandForm( imageName, inputs, args )
function runCommandForm( imageName, inputs, args, entrypoint )
{
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 commandEntrypoint = entrypoint === undefined ? '' : `--entrypoint ${ entrypoint } `;
const command = [ `docker run --name ${ tag } --label ${ repo } ${ commandEntrypoint }--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 =
Expand Down
10 changes: 3 additions & 7 deletions src/Retry.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ function retry( scriptType )
{
const actionFileDir = _.path.nativize( _.path.join( localActionDir, remoteActionPath.localVcsPath ) );
const config = common.actionConfigRead( actionFileDir );

if( common.shouldExit( config, scriptType ) )
return null;

Expand Down Expand Up @@ -122,16 +123,11 @@ function retry( scriptType )
}
else if( config.runs.using === 'docker' )
{
if( scriptType === 'pre' || scriptType === 'post' )
throw _.error.brief
(
`The required feature "${ scriptType }-entrypoint" does not implemented.`
+ '\nPlease, open an issue with the request for the feature.'
);
const docker = require( './Docker.js' );
const imageName = docker.imageBuild( actionFileDir, config.runs.image );
const args = docker.commandArgsFrom( config.runs.args, fullOptions );
const execPath = docker.runCommandForm( imageName, envOptions, args );
const entrypoint = scriptType === 'main' ? config.runs.entrypoint : config.runs[ `${ scriptType }-entrypoint` ];
const execPath = docker.runCommandForm( imageName, envOptions, args, entrypoint );

routine = () =>
{
Expand Down
4 changes: 2 additions & 2 deletions test/Action.test.s
Original file line number Diff line number Diff line change
Expand Up @@ -1871,15 +1871,15 @@ const Proto =
retryActionWithPostScript,
retryActionWithPreAndPostScript,

//

retryActionWithDefaultInputs,
retryActionWithDefaultInputsAsExpressions,
retryActionWithDefaultInputsFromJobContext,
retryActionWithDefaultInputsFromMatrixContext,

retryPrivateActionWithoutInputsMap,

//

retryDockerTrivialAction,
retryDockerActionWithInputs,
},
Expand Down
28 changes: 26 additions & 2 deletions test/Common.test.s
Original file line number Diff line number Diff line change
Expand Up @@ -1345,15 +1345,15 @@ function shouldExit( test )

test.case = 'all entries in config, pre';
var got = common.shouldExit( config, 'pre' );
test.identical( got, true );
test.identical( got, false );

test.case = 'all entries in config, main';
var got = common.shouldExit( config, 'main' );
test.identical( got, false );

test.case = 'all entries in config, post';
var got = common.shouldExit( config, 'post' );
test.identical( got, true );
test.identical( got, false );

test.close( 'without conditions' );

Expand Down Expand Up @@ -1383,6 +1383,30 @@ function shouldExit( test )
var got = common.shouldExit( config, 'pre' );
test.identical( got, true );

/* */

var config =
{
runs:
{
'using' : 'docker',
'image' : 'Dockerfile',
'pre-if' : 'env.TEST_CI == true',
'pre-entrypoint' : 'pre.sh',
'post-entrypoint' : 'post.sh',
}
};

test.case = 'all entries in config, pre with condition, should be false';
core.exportVariable( 'INPUT_ENV_CONTEXT', '{"TEST_CI": true}' );
var got = common.shouldExit( config, 'pre' );
test.identical( got, false );

test.case = 'all entries in config, pre with condition, should be true';
core.exportVariable( 'INPUT_ENV_CONTEXT', '{"TEST_CI": false}' );
var got = common.shouldExit( config, 'pre' );
test.identical( got, true );

test.close( 'with conditions' );
}

Expand Down
16 changes: 16 additions & 0 deletions test/Docker.test.s
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,18 @@ function runCommandForm( test )
var exp = /-v \".*\"\:\"\/github\/workspace\" -v \".*\"\:\".*\" repo\:tag/;
test.identical( _.strCount( got, exp ), 1 );

/* */

test.case = 'empty inputs options, with entrypoint';
var got = docker.runCommandForm( 'repo:tag', {}, [], '/usr/bin/java' );
var exp =
`docker run --name tag --label repo --entrypoint /usr/bin/java --workdir /github/workspace --rm -e HOME -e GITHUB_JOB -e GITHUB_REF -e GITHUB_SHA -e GITHUB_REPOSITORY -e GITHUB_REPOSITORY_OWNER -e GITHUB_REPOSITORY_OWNER_ID -e GITHUB_RUN_ID -e GITHUB_RUN_NUMBER -e GITHUB_RETENTION_DAYS -e GITHUB_RUN_ATTEMPT -e GITHUB_REPOSITORY_ID -e GITHUB_ACTOR_ID -e GITHUB_ACTOR -e GITHUB_TRIGGERING_ACTOR -e GITHUB_WORKFLOW -e GITHUB_HEAD_REF -e GITHUB_BASE_REF -e GITHUB_EVENT_NAME -e GITHUB_SERVER_URL -e GITHUB_API_URL -e GITHUB_GRAPHQL_URL -e GITHUB_REF_NAME -e GITHUB_REF_PROTECTED -e GITHUB_REF_TYPE -e GITHUB_WORKFLOW_REF -e GITHUB_WORKFLOW_SHA -e GITHUB_WORKSPACE -e GITHUB_ACTION -e GITHUB_EVENT_PATH -e GITHUB_ACTION_REPOSITORY -e GITHUB_ACTION_REF -e GITHUB_PATH -e GITHUB_ENV -e GITHUB_STEP_SUMMARY -e GITHUB_STATE -e GITHUB_OUTPUT -e RUNNER_OS -e RUNNER_ARCH -e RUNNER_NAME -e RUNNER_ENVIRONMENT -e RUNNER_TOOL_CACHE -e RUNNER_TEMP -e RUNNER_WORKSPACE -e ACTIONS_RUNTIME_URL -e ACTIONS_RUNTIME_TOKEN -e ACTIONS_CACHE_URL -e GITHUB_ACTIONS=true -e CI=true -v "/var/run/docker.sock":"/var/run/docker.sock" -v "/home/runner/work/_temp/_github_home":"/github/home" -v "/home/runner/work/_temp/_github_workflow":"/github/workflow" -v "/home/runner/work/_temp/_runner_file_commands":"/github/file_commands"`;
test.identical( _.strCount( got, exp ), 1 );
var exp = /-v \".*\"\:\"\/github\/workspace\" -v \".*\"\:\".*\" repo\:tag/;
test.identical( _.strCount( got, exp ), 1 );

/* */

test.case = 'not empty inputs options';
var got = docker.runCommandForm( 'repo:tag', { 'INPUT_STR' : 'str', 'INPUT_NUMBER' : '2' }, [] );
var exp =
Expand All @@ -166,6 +178,8 @@ function runCommandForm( test )
var exp = /-v \".*\"\:\"\/github\/workspace\" -v \".*\"\:\".*\" repo\:tag/;
test.identical( _.strCount( got, exp ), 1 );

/* */

test.case = 'not empty inputs options, env context';
process.env.INPUT_ENV_CONTEXT = '{"FOO": "bar"}';
var got = docker.runCommandForm( 'repo:tag', { 'INPUT_STR' : 'str', 'INPUT_NUMBER' : '2' }, [] );
Expand All @@ -176,6 +190,8 @@ function runCommandForm( test )
test.identical( _.strCount( got, exp ), 1 );
process.env.INPUT_ENV_CONTEXT = '{}';

/* */

test.case = 'empty inputs options, non empty args';
var got = docker.runCommandForm( 'repo:tag', {}, [ '""', 'foo' ] );
var exp =
Expand Down

0 comments on commit 8382a40

Please sign in to comment.