Skip to content

Commit

Permalink
action: add support for user-defined environment variables
Browse files Browse the repository at this point in the history
  • Loading branch information
takase1121 committed Sep 15, 2024
1 parent 4be0e4c commit 61d19cd
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 7 deletions.
1 change: 1 addition & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,5 @@ runs:
INPUT_PASSWORD: ${{ inputs.password }}
INPUT_DOCKER_NETWORK: ${{ inputs.docker_network }}
INPUT_PLATFORM: ${{ inputs.platform }}
INPUT_ACTIONS_ENV: ${{ toJson(env) }}
run: "$GITHUB_ACTION_PATH/entrypoint.sh"
43 changes: 36 additions & 7 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ main() {
local COLOR_RED="\e[0;31m"
local COLOR_RESET="\e[0m"

declare -A env_vars=(
[HOME]=1 [CI]=1 [GITHUB_ACTIONS]=1 [GITHUB_WORKSPACE]=1 [GITHUB_EVENT_PATH]=1 [GITHUB_OUTPUT]=1
[GITHUB_ENV]=1 [GITHUB_STATE]=1 [GITHUB_STEP_SUMMARY]=1 [GITHUB_PATH]=1
)
local script
IFS=" " read -r -a argv <<< "$INPUT_OPTIONS"
IFS=" " read -r -a run_args <<< "$INPUT_RUN_ARGS"
Expand Down Expand Up @@ -37,15 +41,40 @@ main() {
argv+=(--platform "$INPUT_PLATFORM")
fi

# load all environment variables
for prefix in GITHUB_ RUNNER_ ACTIONS_; do
while IFS= read -r env; do
# add important environment variables that mustn't be overriden
argv+=(
-e "HOME=/github/home"
-e "CI=true"
-e "GITHUB_ACTIONS=true"
-e "GITHUB_WORKSPACE=/github/workspace"
-e "GITHUB_EVENT_PATH=${GITHUB_EVENT_PATH/#$RUNNER_TEMP//github/workflow}"
-e "GITHUB_OUTPUT=${GITHUB_OUTPUT/#$RUNNER_TEMP//github/file_commands}"
-e "GITHUB_ENV=${GITHUB_ENV/#$RUNNER_TEMP//github/file_commands}"
-e "GITHUB_STATE=${GITHUB_STATE/#$RUNNER_TEMP//github/file_commands}"
-e "GITHUB_STEP_SUMMARY=${GITHUB_STEP_SUMMARY/#$RUNNER_TEMP//github/file_commands}"
-e "GITHUB_PATH=${GITHUB_PATH/#$RUNNER_TEMP//github/file_commands}"
)

# load user set environment variables
while IFS= read -r env; do
# insert if it doesn't override important variables that can't be overriden
if ! [[ -v env_vars["$env"] ]]; then
argv+=(-e "$env")
done <<< "$(env | grep -o "^${prefix}[^=]*")"
done
env_vars["$env"]=1
fi
done <<< "$(jq -r 'keys | .[]' <<< "$INPUT_ACTIONS_ENV")"

# load extra environment variables
argv+=(-e HOME -e CI=true -e GITHUB_ACTIONS=true)
# load GHA environment variables
while IFS= read -r env; do
# insert if it doesn't override important variables that can't be overriden
if [[ "$env" = GITHUB_* ]] \
|| [[ "$env" = RUNNER_* ]] \
|| [[ "$env" = ACTIONS_* ]] \
&& ! [[ -v env_vars["$env"] ]]; then
argv+=(-e "$env")
env_vars["$env"]=1
fi
done <<< "$(env | grep -o "^[^=]*")"

# add important volumes
# some directories here are taken from action steps that uses docker:// protocols
Expand Down

0 comments on commit 61d19cd

Please sign in to comment.