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 )