feat: add env subst command [CPE-1773] #910
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Checklist
=========
Internal Checklist
Changes
=======
This PR adds a new command
env
, with a sub-commandsubst
which implements the envsubst library (~2MB) to provide a universal method for CircleCI jobs to safely substitute variables within strings.
Example:
Rationale
=========
CircleCI's orbs rely on a number of "standard" dependencies in order to work, which we require of our users to have installed for the majority of orbs to work, such as
curl
andJQ
. Recently we have seen that an additional tool is necessary to properly evaluate strings containing environment variables on all platforms properly.
The way we develop orbs has changed in the last year to change patterns in a way that is more maintainable but has had unintended consequences.
Old orb method:
Notice here, there is bash inside the yaml where the
run
statement is found. Inside this bash, we have yet another syntax for CircleCI's parameters.
To make orbs more maintainable, we now move the bash script out of the orb source so that the user can execute it locally and get proper syntax highlighting in their IDE for the bash code. To accommodate parameters, we switched to something like this:
Here, the script is imported at the time the orb is packed and it relies on the value of the parameter being within an environment variable
The problem
The problem with this new way of building orbs, is we now have environment variables with environment variables within them being interpreted as strings. We originally tried using the
eval
linux command, but this has many unintended consequences, such as treating=
literally within a string where it should not.
Solution
Using this tool, users can 'sanitize' environment variables like so.
MYVAR=$(circleci env subst $MYVAR)
Why not require users to install envsubst?
We have taken this approach for all other tools such as BASH (vs shell), CURL, and JQ. However, those tools are not inherent to the function of orbs the product, instead these tools are necessary by individual orbs to accomplish tasks, and it may be reasonable that to use an orb which parses JSON, to require JQ. For this tool, this would be used by all orbs as part of a "standard library" to properly interpret parameters, not accomplish any specific task as described by the orb. Additionally we have seen that many tools such as awk and sed, which add a lot of friction for orb developers, also behave differently on different operating systems.
Considerations
==============
How the command name was chosen
In the local CircleCI CLI we have a command for
contexts
but contexts are not the only type of environment variable that can be updated via API. It might make sense eventually to move all "secret management" tools within our CLIs to a command such asenv
so that we may group secret management commands together. So theoretically, maybe the public CLI would eventually change fromcircleci contexts
tocircleci env contexts
. This would give a home for all secrets tools, including the proposedsubst
command in this PR. I would eventually like this command to be found in both CLIs so that scripts can be run locally.Screenshots
============