Last active
October 25, 2019 12:54
-
-
Save f-steff/1181f8f5907ae9da73779aba8182df3b to your computer and use it in GitHub Desktop.
Fix for array variables imported into bash using Jenkins 'Inject environment variables' plugin
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
#!/bin/bash | |
# fsteff 2019 | |
# Workaround for bash not properly supporting export of environment array variables. | |
# When using Jenkins 'Inject environment variables' plugin, array variables ends up in a strange state, | |
# where they are listed as exported variables, but are not actual shell variables. | |
# Checking using `env` or `printenv` shows injected array variables unrolled (ie not as arrays) | |
# Checking using `declare -p`, `declare -xp` and `set` does not show injected array variables - but normal array variable are shown as arrays. | |
# The workaround below loops through the output of `printenv` and sets each injected variable it finds. | |
# NOTE: This must be run in the 'Execute Shell' where the variables are needed. | |
# NOTE2: Using as a script file, requires that it's executed as `. ./FixInjectedArraysInBash.sh` or `source ./FixInjectedArraysInBash.sh` | |
# https://issues.jenkins-ci.org/browse/JENKINS-59933?jql=project%20%3D%20JENKINS%20AND%20component%20%3D%20envinject-plugin | |
echo "Attempting to fix injected arrays." | |
tempFilename="TemporaryPrintEnvOutput.temp" | |
#tempFilename="$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1).temp" # Random string name. | |
printenv >${tempFilename} | |
while IFS= read -r line; do | |
var=${line%%=*} | |
val=${line#*=} | |
if [[ ${var} =~ \[[0-9]\] ]]; then | |
echo " ${line}" | |
eval ${var}=\${val} | |
fi | |
done < ${tempFilename} | |
rm -f ${tempFilename} | |
# Workaround end. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment