|
| 1 | +#################################################################################################### |
| 2 | +# Helpers for defining environment variables for CircleCI. |
| 3 | +# |
| 4 | +# In CircleCI, each step runs in a new shell. The way to share ENV variables across steps is to |
| 5 | +# export them from `$BASH_ENV`, which is automatically sourced at the beginning of every step (for |
| 6 | +# the default `bash` shell). |
| 7 | +# |
| 8 | +# See also https://circleci.com/docs/2.0/env-vars/#using-bash_env-to-set-environment-variables. |
| 9 | +#################################################################################################### |
| 10 | + |
| 11 | +# Set and print an environment variable. |
| 12 | +# |
| 13 | +# Use this function for setting environment variables that are public, i.e. it is OK for them to be |
| 14 | +# visible to anyone through the CI logs. |
| 15 | +# |
| 16 | +# Usage: `setPublicVar <name> <value>` |
| 17 | +function setPublicVar() { |
| 18 | + setSecretVar $1 "$2"; |
| 19 | + echo "$1=$2"; |
| 20 | +} |
| 21 | + |
| 22 | +# Set (without printing) an environment variable. |
| 23 | +# |
| 24 | +# Use this function for setting environment variables that are secret, i.e. should not be visible to |
| 25 | +# everyone through the CI logs. |
| 26 | +# |
| 27 | +# Usage: `setSecretVar <name> <value>` |
| 28 | +function setSecretVar() { |
| 29 | + # WARNING: Secrets (e.g. passwords, access tokens) should NOT be printed. |
| 30 | + # (Keep original shell options to restore at the end.) |
| 31 | + local -r originalShellOptions=$(set +o); |
| 32 | + set +x -eu -o pipefail; |
| 33 | + |
| 34 | + echo "export $1=\"${2:-}\";" >> $BASH_ENV; |
| 35 | + |
| 36 | + # Restore original shell options. |
| 37 | + eval "$originalShellOptions"; |
| 38 | +} |
| 39 | + |
| 40 | + |
| 41 | +# Create a function to set an environment variable, when called. |
| 42 | +# |
| 43 | +# Use this function for creating setter for public environment variables that require expensive or |
| 44 | +# time-consuming computaions and may not be needed. When needed, you can call this function to set |
| 45 | +# the environment variable (which will be available through `$BASH_ENV` from that point onwards). |
| 46 | +# |
| 47 | +# Arguments: |
| 48 | +# - `<name>`: The name of the environment variable. The generated setter function will be |
| 49 | +# `setPublicVar_<name>`. |
| 50 | +# - `<code>`: The code to run to compute the value for the variable. Since this code should be |
| 51 | +# executed lazily, it must be properly escaped. For example: |
| 52 | +# ```sh |
| 53 | +# # DO NOT do this: |
| 54 | +# createPublicVarSetter MY_VAR "$(whoami)"; # `whoami` will be evaluated eagerly |
| 55 | +# |
| 56 | +# # DO this isntead: |
| 57 | +# createPublicVarSetter MY_VAR "\$(whoami)"; # `whoami` will NOT be evaluated eagerly |
| 58 | +# ``` |
| 59 | +# |
| 60 | +# Usage: `createPublicVarSetter <name> <code>` |
| 61 | +# |
| 62 | +# Example: |
| 63 | +# ```sh |
| 64 | +# createPublicVarSetter MY_VAR 'echo "FOO"'; |
| 65 | +# echo $MY_VAR; # Not defined |
| 66 | +# |
| 67 | +# setPublicVar_MY_VAR; |
| 68 | +# source $BASH_ENV; |
| 69 | +# echo $MY_VAR; # FOO |
| 70 | +# ``` |
| 71 | +function createPublicVarSetter() { |
| 72 | + echo "setPublicVar_$1() { setPublicVar $1 \"$2\"; }" >> $BASH_ENV; |
| 73 | +} |
0 commit comments