App-Actions
view release on metacpan or search on metacpan
lib/App/Actions/Runner.pm view on Meta::CPAN
# Entry: None; temp_path must exist.
# Exit status: Returns nothing.
# Side effects: Writes event.json; sets event and base_env.
sub _build_base_env {
my ($self) = @_;
my $github = $self->_repository_env;
$self->{event} = {
repository => {
full_name => $github->{GITHUB_REPOSITORY},
name => (split m{/}, $github->{GITHUB_REPOSITORY})[-1],
owner => { login => $github->{GITHUB_REPOSITORY_OWNER} },
},
ref => $github->{GITHUB_REF},
after => $github->{GITHUB_SHA},
before => '0' x 40,
pusher => { name => $github->{GITHUB_ACTOR} },
};
my $event_path = "$self->{temp_path}/event.json";
write_container_file($event_path, JSON::PP->new->canonical->encode($self->{event}), 'event.json');
{
no autodie qw(chmod);
chmod($self->{share_files} ? oct('0644') : oct('0600'), $event_path);
}
$self->{base_env} = {
%{$github},
CI => 'true',
DEBIAN_FRONTEND => 'noninteractive',
GITHUB_ACTIONS => 'true',
# The step-specific variables exist from the start, empty, as on GitHub.
(map { $_ => '' } qw(GITHUB_ACTION GITHUB_BASE_REF GITHUB_ENV GITHUB_HEAD_REF GITHUB_JOB
GITHUB_OUTPUT GITHUB_PATH GITHUB_STATE GITHUB_STEP_SUMMARY GITHUB_TOKEN)),
GITHUB_EVENT_PATH => '/github/actions-runner/event.json',
GITHUB_WORKFLOW => $self->{workflow}->name,
GITHUB_WORKSPACE => '/github/workspace',
HOME => defined $self->{user} ? '/github/actions-runner/home' : '/root',
RUNNER_ARCH => $RUNNER_CONTEXT{arch},
RUNNER_NAME => $RUNNER_CONTEXT{name},
RUNNER_OS => $RUNNER_CONTEXT{os},
RUNNER_TEMP => $RUNNER_CONTEXT{temp},
RUNNER_TOOL_CACHE => $RUNNER_CONTEXT{tool_cache},
RUNNER_WORKSPACE => '/github',
($self->{verbose} ? (RUNNER_DEBUG => '1', ACTIONS_STEP_DEBUG => 'true') : ()),
};
return;
}
# _git
# Purpose: Ask git about the project.
# Entry: @args - git's arguments.
# Exit status: Returns its output, or '' when git fails or is absent.
# Side effects: Runs git (not fork plus a piped open, which Windows does not
# reliably support).
sub _git {
my ($self, @args) = @_;
my ($output) = App::Actions::Process::capture([ 'git', '-C', $self->{project_dir}, @args ], quiet_stderr => 1);
return defined $output ? $output : '';
}
# _run_groups
# Purpose: Run every selected job, each after the jobs it needs.
# Entry: None; _select_jobs has run.
# Exit status: Returns a hash reference of job id to its group result.
# Side effects: Runs the jobs.
sub _run_groups {
my ($self) = @_;
my $graph = { results => {}, visited => {}, running => {} };
$self->_run_base_job($_, $graph) for @{ $self->{job_order} };
return $graph->{results};
}
# _run_base_job
# Purpose: Run one job id - every matrix instance of it - after the jobs
# it needs, deciding each instance's condition first.
# Entry: $base_id; $graph - results, visited and running sets.
# Exit status: Returns { result, outputs, instances }; throws on a cycle.
# Side effects: Runs the instances, in parallel where it can; records them
# in the report.
sub _run_base_job {
my ($self, $base_id, $graph) = @_;
return $graph->{results}{$base_id} if $graph->{visited}{$base_id};
$self->throw('runner.dependency_cycle', { job => $base_id }) if $graph->{running}{$base_id};
my $instances = $self->{job_groups}{$base_id};
$self->throw('runner.unknown_job', { job => $base_id }) unless $instances && @{$instances};
$graph->{running}{$base_id} = 1;
# The jobs this one needs run first; one that was not selected is
# assumed to have succeeded, and the user is told so.
my @needs = @{ $instances->[0]{needs} || [] };
$self->_run_base_job($_, $graph) for grep { exists $self->{job_groups}{$_} } @needs;
my %need_results;
for my $need (@needs) {
$self->_complain('runner.need_not_selected', { job => $base_id, need => $need }) unless exists $graph->{results}{$need};
$need_results{$need} = $graph->{results}{$need} || { result => 'success', outputs => {} };
}
my $needs_failed = (grep { $_->{result} ne 'success' } values %need_results) ? 1 : 0;
my $strategy = $instances->[0]{strategy} || {};
my $fail_fast = exists $strategy->{'fail-fast'} ? _truthy_scalar($strategy->{'fail-fast'}) : 1;
# Every instance is planned before any runs: in parallel the runnable
# ones start together, so their conditions are decided first.
my $group = { result => 'skipped', stop => 0 };
my @runnable = grep { $self->_admit($_, \%need_results, $group) }
map { $self->_plan_instance($_, \%need_results, $needs_failed, $fail_fast) } @{$instances};
# An instance fail-fast stopped from starting is reported as skipped,
# whichever way the group ran.
my $outcomes = $self->_runs_in_parallel ? $self->_parallel->run(\@runnable, $fail_fast) : undef;
if ($outcomes) {
for my $outcome (@{$outcomes}) {
lib/App/Actions/Runner.pm view on Meta::CPAN
containers and networks are removed first. Press Ctrl-C again only if the
clean-up itself hangs.
=item * B<C<keep> keeps everything.> With C<keep>, containers and networks
are left running for you to inspect; remove them yourself afterwards (their
names start with C<action-run->).
=item * B<C<parallel> is ignored with C<keep>>, on Windows, and where
C<fork> does not work; jobs then run one at a time.
=item * B<C<user> other than yourself or root opens the runner folder to
all users.> See L</LIMITATIONS>.
=back
=head1 ENCODING
Text from the workflow (commands, names, environment values, secrets) is
handled as Perl character strings and passed to Docker as UTF-8 bytes, so
non-ASCII characters and emoji work in step commands and values. Step
scripts are written as UTF-8 with LF line endings. Paths (the project
folder, the temporary folder) are used as bytes, as your system stores them.
Output from the containers is passed to your terminal as bytes, unchanged.
Messages from the runner itself are in the language of L<App::Actions::I18N>;
non-English messages contain non-ASCII characters.
=head1 AUTHOR
Nigel Horne, C<njh@nigelhorne.com>
=head1 FORMAL SPECIFICATION
This section describes each routine in the Z notation, a precise
mathematical language for describing programs. You do not need to read it
to use the module: everything here is also said in plain words in the
sections above. It is for readers who want to check exactly what a routine
promises.
How to read it: a box names a routine. The lines above the middle rule
declare the values it works with. A name ending in C<?> is an input, and a
name ending in C<!> is an output. C<ÎState> means the routine changes
nothing, and C<ÎState> means it can change the state. The lines below the
rule are the conditions that are always true when the routine returns.
=head2 new
ââ NewRunner âââââââââââââââââââââââââââââââââââââââ
â workflow? : WORKFLOW
â projectDir? : PATH
â options? : OPTION ⸠VALUE
â r! : Runner
ââââââââââââââââââââââââââââââââââââââââââââââââââââ
â dom options? â dom optionRules
â isDirectory(projectDir?)
â ¬ (options?(pull) â§ options?(no_pull))
â r!.pullConcurrency =
â first(options?(pull_concurrency), env(ACTION_RUN_PULL_CONCURRENCY), 4) â ââ
â r!.parallel = (if options?(parallel) â ââ then options?(parallel) else 1)
â r!.dryRun = options?(dry_run) ⨠options?(explain)
â r!.keepImages = (if options?(keep_images) = ⥠then true else options?(keep_images))
â r!.logger = (if options?(logger) = ⥠then stderrLogger else options?(logger))
â â part : {images, containers, steps, parallel, report} ⢠part.logger = r!.logger
ââââââââââââââââââââââââââââââââââââââââââââââââââââ
=head2 option_rules
ââ OptionRules âââââââââââââââââââââââââââââââââââââ
â rules! : OPTION ⸠RULE
ââââââââââââââââââââââââââââââââââââââââââââââââââââ
â rules! = optionRules
ââââââââââââââââââââââââââââââââââââââââââââââââââââ
=head2 json_schema
ââ JsonSchema ââââââââââââââââââââââââââââââââââââââ
â schema! : JSON
ââââââââââââââââââââââââââââââââââââââââââââââââââââ
â schema! = decode(JSON_SCHEMA) â§ schema!.version = PREFLIGHT_SCHEMA_VERSION
ââââââââââââââââââââââââââââââââââââââââââââââââââââ
=head2 preflight_schema_version
ââ PreflightSchemaVersion ââââââââââââââââââââââââââ
â v! : ââ
ââââââââââââââââââââââââââââââââââââââââââââââââââââ
â v! = PREFLIGHT_SCHEMA_VERSION
ââââââââââââââââââââââââââââââââââââââââââââââââââââ
=head2 workflow
ââ Workflow ââââââââââââââââââââââââââââââââââââââââ
â ÎRunner
â w! : WORKFLOW
ââââââââââââââââââââââââââââââââââââââââââââââââââââ
â w! = workflow
ââââââââââââââââââââââââââââââââââââââââââââââââââââ
=head2 list_jobs
ââ ListJobs ââââââââââââââââââââââââââââââââââââââââ
â ÎRunner
â out! : seq LINE
â exit! : â¤
ââââââââââââââââââââââââââââââââââââââââââââââââââââ
â out! = ⨠j : selected(expand(workflow)) ⢠j.base_id â TAB â redact(j.display_name) â©
â exit! = 0
ââââââââââââââââââââââââââââââââââââââââââââââââââââ
=head2 run
ââ Run âââââââââââââââââââââââââââââââââââââââââââââ
â ÎRunner
â exit! : {0, 1, 2}
ââââââââââââââââââââââââââââââââââââââââââââââââââââ
â raised â exit! = 2
â ¬ raised â§ report.failed' â exit! = 1
â ¬ raised ⧠¬ report.failed' â exit! = 0
â cleanedUp'
ââââââââââââââââââââââââââââââââââââââââââââââââââââ
=head2 preflight
( run in 1.414 second using v1.01-cache-2.11-cpan-60884a8df76 )