Hi,
I set the below sample environment variables in my project:
dev_key=xxxx
prod_key=yyyy
test_key=zzzz
Then I want to insert those variables to the parameters configuration so, if the CIRCLE_BRANCH=dev then:
my_key=${CIRCLE_BRANCH}_key
and same for the other branches:
So the configuration would be:
parameters:
my_key:
type: string
default: ${CIRCLE_BRANCH}_key
For example, if circle CI branch is dev, then my_key must be xxxx (From environmental variables). But it doesn’t work and it only sends back the key (my_key = dev_key)
Do you have any idea how to solve the issue?
Br,
Meraj
Hi @meraj-kashi,
Thank you for sharing your question on our forum!
Environment variables such as CIRCLE_BRANCH
are not available when your config.yml file is compiled, and as such you won’t be able to use them directly as a parameter. You can however use pipeline values directly in parameters as described on the following page:
https://circleci.com/docs/2.0/pipeline-variables/#pipeline-values
This means it wouldn’t be possible to filter jobs or workflows for example based on a parameter using an environment variable.
If you are looking to store a command as a parameter and later evaluate it inside of a job (after the job starts), then something like this should work:
Assume there is a project environment variable DEV_KEY
set to equal test12345
.
I then push a commit to the dev
branch, triggering a new build.
Here is a sample config:
version: 2.1
jobs:
build:
parameters:
my_key:
type: string
default: "echo $<< pipeline.git.branch >>_KEY"
docker:
- image: cimg/node:17.3.1
steps:
- checkout
- run: << parameters.my_key >>
workflows:
workflow1:
jobs:
- build
In the above, I am using a pipeline value pipeline.git.branch
which is made available to use in your config.yml file.
Let me know if you have any further questions!
1 Like
Hi again @aaronclark,
Thanks for your solution, it works fine!
parameters:
my_key:
type: string
default: "echo $KEY_<< pipeline.git.branch >>"
Just a quick question, is there any built-in function that can capitalize (to uppercase) the value? I mean in the above example, the default value of my_key is equal to KEY_dev
, then is there any solution to change it to KEY_DEV
?
Br,
Meraj
Hi @meraj-kashi,
Glad to hear it was useful!
There is no built in feature within CircleCI to capitalize the characters in a string, but I believe you can do something like the following with bash:
echo $KEY_<< pipeline.git.branch >> | tr a-z A-Z
1 Like