Inception
Hello everyone, This article is part of the CircleCI series, The knowledge in this series is built in sequence.
Today's example will try to cover How to Set and Manage Environment variables across all Projects, a Specific project, or even a job. With some code examples to provide The Know-how, and to get the value it provides.
Contexts Overview
As Defined by CircleCI Academy, Contexts are groups of environment variables that can be secured and shared across projects. After a context has been created, you can use the context key in the workflows section of a project config.yml file to give any job(s) access to the environment variables associated with the context.
Contexts Code example
Let's demonstrate how to create and use context environment variables.
In this example will configure our pipeline code to Test logging into Docker Hub by using docker Orb, and will create context environment variables for the user_name and password and access them securely in our YAML file instead of typing them as clear text in the YAML file.
Open Organization settings
Press on context, Then Create context
Here I created a context with Docker_Build_context name, Feel free to choose a name for your context.
In the context section, Add Environment variables like the below:
DOCKER_LOGIN should have your username of docker hub.
After Creating The Environment variables now let's configure our YAML file and view how to call environment variables securely.
version: 2.1
orbs:
docker: circleci/docker@2.1.4
jobs:
Build:
docker:
- image: cimg/base:stable
steps:
- checkout
- setup_remote_docker:
docker_layer_caching: true
- docker/check: # Docker Hub Login Test
docker-password: DOCKER_PASSWORD
docker-username: DOCKER_LOGIN
- run:
name: Try to print-out Env-vars
command: |
echo "$DOCKER_PASSWORD is secure to print here"
echo "$DOCKER_LOGIN is secure to print here"
workflows:
Build_Docker:
jobs:
- Build:
context:
- Docker_Build_Context
In this YAML files configuration, we're using docker/check
Orbs command to test our Environment variables credentials. Press here For more info about Orbs.
Then, Will try to print out The Environment variables to view and test how it's secure, and ensure that it will not print them in clear text.
And call the context that we created in the workflow section, So The Pipeline knows from where should fetch these variables.
- The Yaml file run result should be like the below:
That's it....
To summarize this, The context Environment variables are the secure way to generate and use the Environment variables, Also It's can be used across all projects.
Simple like that... ๐๐
Environment variables Overview
Briefly, Environment variables is the same as Context environment variables. however, it's just for a specific Project. Which means it's not cross-over all projects.
Environment variables are useful when you have some variables across projects with the same name however, their value should be not the same.
Environment variables Code example
Let's discover how to create and use Environment variables
Open your project, Then Press Project settings
Press on Environment variables, Then create Environment variables like the below:
Now, Let's call them from The YAML file
version: 2.1
orbs:
docker: circleci/docker@2.1.4
jobs:
Build:
docker:
# replace with your preferred image
- image: cimg/base:stable
steps:
- checkout
- setup_remote_docker:
docker_layer_caching: true
- docker/check:
docker-password: DOCKER_PASSWORD
docker-username: DOCKER_LOGIN
- run:
name: Try to print-out Env-vars
command: |
echo "$DOCKER_PASSWORD is secure to print here"
echo "$DOCKER_LOGIN is secure to print here"
workflows:
Build_Docker:
jobs:
- Build
Actually, It's the same YAML file configuration, However without calling the context in the workflow section, So The pipeline know it should fetch them from the default place -environment variable of the project settings-
The Result Will be like the below:
Great, Now we set Secure Environment variables across all projects and for a specific project.
YAML Environment variables
Also, You have the ability to set environment variables in The YAML file for a specific job, This can be useful when you want quickly set a variable, Or set multiple environment variables with the same name but their values are different at each job.
YAML variables code example
- Let's discover The YAML file configuration below
version: 2.1
jobs:
Build:
docker:
- image: cimg/base:stable
environment: # Set environment variable for this job
Name: Mohamed El Eraky
Email: Mohamed-ibrahim2021@outlook.com
steps:
- checkout
- run:
name: print out username and email
command: |
echo "$Name is my name"
echo "$Email is my email"
Build_two:
docker:
- image: cimg/base:stable
environment: # Set environment variable for this job
Name: Sama El Eraky
Email: Sama-Mohamed@outlook.com
steps:
- checkout
- run:
name: print out username and email
command: |
echo "$Name is my my Name"
echo "$Email is my email"
workflows:
Build_Docker:
jobs:
- Build
- Build_two
In this example, we created Two Jobs with the same name for the environment variables however the values are different.
The Build Job result Will be like the below:
The Build_Tow job result Will be like the below:
As viewed here, The YAML environment variables are not secure, and it's defined for specific jobs.
Very simple like That....๐๐
Hints
Context environment variables overwrite the project environment variables
Environment variables overwrite the YAML environment
all variables are case sensitive
The YAML environment works only inside the job with no secure printout.
built-in environment variables https://circleci.com/docs/variables/
Know-more:
That's it, Hope this article inspired you and will appreciate your feedback. Thank you.