-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdispatch.sh
More file actions
executable file
·79 lines (65 loc) · 3.12 KB
/
Copy pathdispatch.sh
File metadata and controls
executable file
·79 lines (65 loc) · 3.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/sh
# dispatch.sh — Run any action locally by name, with sensible GITHUB_* defaults.
set -e
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
# Print the description of an action from its action.yml
_action_description() {
grep -m1 'description:' "${SCRIPT_DIR}/${1}/action.yml" 2>/dev/null \
| sed 's/^[[:space:]]*description:[[:space:]]*//' \
| tr -d "'"
}
# Display help
_help() {
printf 'Usage: %s <action> [args...]\n\n' "$(basename "$0")" >&2
printf 'Run a GitHub Action locally with sensible defaults.\n\n' >&2
printf 'Available actions:\n' >&2
for _dir in "${SCRIPT_DIR}"/*/; do
_name=$(basename "$_dir")
if [ -f "${_dir}run.sh" ]; then
_desc=$(_action_description "$_name")
printf ' %-30s %s\n' "$_name" "$_desc" >&2
fi
done
printf '\nEnvironment variables:\n' >&2
printf ' %-28s %s\n' GITHUB_TOKEN 'Authentication token (required by most actions)' >&2
printf ' %-28s %s\n' GITHUB_WORKSPACE 'Working directory (default: current directory)' >&2
printf ' %-28s %s\n' GITHUB_REPOSITORY 'owner/repo (default: inferred from git remote)' >&2
printf ' %-28s %s\n' GITHUB_REF 'Git ref (default: current branch)' >&2
printf ' %-28s %s\n' GITHUB_SHA 'Commit SHA (default: HEAD)' >&2
printf '\nExamples:\n' >&2
printf ' GITHUB_TOKEN=$TOKEN %s check-composer\n' "$(basename "$0")" >&2
printf ' GITHUB_TOKEN=$TOKEN %s run-phpstan app,src\n' "$(basename "$0")" >&2
}
ACTION="${1:-}"
if [ -z "$ACTION" ] || [ "$ACTION" = "--help" ] || [ "$ACTION" = "-h" ]; then
_help
exit 0
fi
shift
# Set default GITHUB_* environment variables if not already set
export GITHUB_WORKSPACE="${GITHUB_WORKSPACE:-$(pwd)}"
export GITHUB_REPOSITORY="${GITHUB_REPOSITORY:-$(git remote get-url origin 2>/dev/null | sed 's|.*github\.com[:/]||; s|\.git$||' || echo '')}"
export GITHUB_REF="${GITHUB_REF:-$(git symbolic-ref HEAD 2>/dev/null || echo 'refs/heads/main')}"
export GITHUB_SHA="${GITHUB_SHA:-$(git rev-parse HEAD 2>/dev/null || echo '')}"
export GITHUB_EVENT_NAME="${GITHUB_EVENT_NAME:-push}"
export GITHUB_ACTOR="${GITHUB_ACTOR:-$(git config user.name 2>/dev/null || echo '')}"
export GITHUB_OUTPUT="${GITHUB_OUTPUT:-/dev/stdout}"
export GITHUB_TOKEN="${GITHUB_TOKEN:-$(gh auth token 2>/dev/null || echo '')}"
# Mirror setup-php's PATH additions: the composite-action flow puts
# vendor/bin and the global Composer bin directory on PATH once per job, so
# individual run.sh scripts no longer do it themselves. dispatch.sh never
# runs setup-php, so it stands in for that step here.
if command -v composer >/dev/null 2>&1; then
export PATH="${GITHUB_WORKSPACE}/vendor/bin:$(composer config -g home 2>/dev/null || echo "${HOME:-/root}/.composer")/vendor/bin:${PATH}"
fi
ACTION_DIR="${SCRIPT_DIR}/${ACTION}"
if [ ! -d "$ACTION_DIR" ]; then
printf '::error::Unknown action: %s\n' "$ACTION" >&2
printf "Run '%s --help' for a list of available actions.\n" "$(basename "$0")" >&2
exit 1
fi
if [ ! -f "${ACTION_DIR}/run.sh" ]; then
printf '::error::Action %s has no run.sh (composite-only action, use it in a workflow instead)\n' "$ACTION" >&2
exit 1
fi
exec sh "${ACTION_DIR}/run.sh" "$@"