-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for passing all secrets, vars, and envs to digger
- Loading branch information
Showing
2 changed files
with
90 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
#!/bin/bash | ||
|
||
# Function to validate environment variable name | ||
is_valid_env_name() { | ||
local name="$1" | ||
[[ $name =~ ^[a-zA-Z_][a-zA-Z0-9_]*$ ]] | ||
} | ||
|
||
# Function to parse context JSON | ||
# $1: variable name | ||
# $2: should_parse flag (true/false) | ||
parse_context_json() { | ||
local json_var="$1" | ||
local should_parse="$2" | ||
local json_content="${!json_var}" | ||
|
||
if [ -n "$json_content" ]; then | ||
# Check if content is valid JSON | ||
if echo "$json_content" | jq empty 2>/dev/null; then | ||
if [ "$should_parse" = "true" ]; then | ||
# Parse JSON into individual env vars | ||
while IFS='=' read -r key value; do | ||
if [ -n "$key" ] && is_valid_env_name "$key"; then | ||
# Remove quotes from value | ||
value="${value%\"}" | ||
value="${value#\"}" | ||
export "$key=$value" | ||
fi | ||
done < <(echo "$json_content" | jq -r 'to_entries | .[] | .key + "=" + (.value | tostring)') | ||
else | ||
# Pass through JSON as-is | ||
if is_valid_env_name "$json_var"; then | ||
export "$json_var=$json_content" | ||
fi | ||
fi | ||
else | ||
# Not JSON, export as-is | ||
if is_valid_env_name "$json_var"; then | ||
export "$json_var=$json_content" | ||
fi | ||
fi | ||
fi | ||
} | ||
|
||
# Process SECRETS_CONTEXT - parse into individual vars | ||
parse_context_json "SECRETS_CONTEXT" "true" | ||
|
||
# Process VARIABLES_CONTEXT - parse into individual vars | ||
parse_context_json "VARIABLES_CONTEXT" "true" | ||
|
||
# Export all regular environment variables | ||
while IFS='=' read -r key value; do | ||
if [[ "$key" != "SECRETS_CONTEXT" && "$key" != "VARIABLES_CONTEXT" ]]; then | ||
if is_valid_env_name "$key"; then | ||
# Pass through all other environment variables | ||
parse_context_json "$key" "false" | ||
fi | ||
fi | ||
done < <(env) | ||
|
||
# Handle TF_VAR_ environment variables | ||
while IFS='=' read -r key value; do | ||
if [[ $key == TF_VAR_* ]]; then | ||
var_name="${key#TF_VAR_}" | ||
lowercase_key="TF_VAR_${var_name,,}" | ||
|
||
if [ -z "${!lowercase_key}" ] && is_valid_env_name "$lowercase_key"; then | ||
export "$lowercase_key=$value" | ||
fi | ||
fi | ||
done < <(env) | ||
|
||
# If digger-spec is provided, export it | ||
if [ -n "$INPUT_DIGGER_SPEC" ]; then | ||
export DIGGER_RUN_SPEC="$INPUT_DIGGER_SPEC" | ||
fi | ||
|
||
# Execute the command passed as arguments | ||
exec "$@" |