Managing and Storing Data, Variables, and Secrets
Managing and Storing Secrets and Sensitive Information
In one of our previous examples, while we were learning how to use the .pre and .post stages, we used Slack in the post-test job. This is how our code looked like:
# Post a message to Slack when the pipeline completes
post-test:
stage: .post
script:
- >
curl -X POST
-H 'Content-type: application/json'
--data '{"text":"Pipeline completed"}'
"https://hooks.slack.com/services/xxxxx/yyyyy/zzzzz"
There's a problem with this approach and it is the fact that we exposed the Slack webhook URL in the .gitlab-ci.yml file. This is considered a security risk because anyone with access to the repository can see the webhook URL and use it to post messages to Slack. The URL should be kept secret. In other scenarios, the URL could be a database password, an API key, or any other sensitive information. These information are not only stored in the .gitlab-ci.yml file but can also show up in the pipeline logs. If you are sending logs to a third-party service, the same information could be exposed to these services as well.
ℹ️ To store secret and sensitive information in GitLab, you should use variables instead of hardcoding them in the CI/CD configuration file.
Go to your project, click on Settings -> CI/CD -> Variables and add a new variable by clicking on the Add variable button. Note that these variables can be set at the project, group, or instance level. In our case, we will set a project-level variable.
ℹ️ Group-level variables are inherited by all projects in the group, while instance-level variables are inherited by all projects in your GitLab instance.
When adding a variable, there are different options you must set:
Type: The type of the variable. It can be
VariableorFile. If you selectFile, you can upload a file containing the secret information. We will use theVariabletype in this example.Environments: The environments for which the variable is available. You can select
Allor specify a specific environment. We will selectAllin this example.Visibility: The visibility of the variable. It can be
VisibleorMasked. If you selectVisible, the variable will be displayed in the pipeline logs. If you selectMasked, the variable will be hidden in the pipeline logs. If you chooseMasked and hidden, the variable will be masked in the logs and you will never be able to reveal it in the UI after saving it. We will selectMaskedin this example.Flags: The flags for the variable. You can select
Protect variableto export the variable to pipelines running on protected branches or tags only. You can also selectExpands variable reference, this will treat$as the start of a reference to another variable. For example, you can create a variable calledBASE_URLwith the valuehttps://example.comand another variable calledAPI_URLwith the value$BASE_URL/api. When you use theAPI_URLvariable in your pipeline, it should expand tohttps://example.com/apithanks to theExpands variable referenceflag. We can activate both flags in this example. We are using the main branch which is protected by default. If you have changed this setting, you can deactivate theProtect variableflag.Description: A description of the variable. This is optional. We will use
Slack webhook URLas the description in this example.Key: The key of the variable. This is the name of the variable. We will use
SLACK_WEBHOOKas the key in this example.Value: The value of the variable. This is the secret information you want to store. We will copy and paste the Slack webhook URL in this field.
Once the variable is added, we will rewrite the .gitlab-ci.yml with the SLACK_WEBHOOK variable without replacing its value (we will keep $SLACK_WEBHOOK):
Cloud Native CI/CD with GitLab
From Commit to Production ReadyEnroll now to unlock all content and receive all future updates for free.
