Denomas CI/CD Hands-On Guide: Lab 5
Denomas CI/CD Hands-On Guide: Lab 5
LAB 5: VARIABLE HIERARCHY
Add custom variables
-
Go to the snippets page of the CI-CD Hands On Demo project.
-
Open the
ci-variablessnippet and click the Copy file contents icon in the upper right corner of the file. -
Open your CICD Demo project from previous labs.
-
Select your
.gitlab-ci.ymlfile to view its contents. Select the dropdown next to Edit, then select Edit this file only. -
Select Edit. Paste the snippet at the end of the file, with an empty line between the file’s previous content and the snippet’s content.
-
Near the top of your
.gitlab-ci.yml, in a new line below the entirestagessection, paste the following:1 2variables: INLINE_GLOBAL_VARIABLE: "I'm an inline variable set at the global level of the CI/CD configuration file" -
Inside the
environment variablesjob, just below that job’sstage: buildline (but before thescriptline), paste the following. Thevariableskeyword should be at the same indendation as that job’sstageamdscriptkeywords.1 2variables: INLINE_LOCAL_VARIABLE: "I'm an inline variable set at the job level of the CI/CD configuration file"When defining variables, watch your indentation. Global variables must be indented by 2 spaces, and must be immediately under a flush-left
variableskeyword that is outside any job definition. Local variables must be indented 4 spaces, and must be immediately under avariableskeyword that is indented 2 spaces and is within a job definition. Here’s a reference example of how to define global and local variables:1 2 3 4 5 6 7 8 9 10 11 12# NOTE: this code demonstrates how to define global and local variables. # Do NOT copy and paste this code into your .gitlab-ci.yml file. variables: GLOBAL_SCOPE_VAR: "value1" job-a: variables: LOCAL_SCOPE_VAR: "value2" script: - echo $LOCAL_SCOPE_VAR - echo $GLOBAL_SCOPE_VAR -
In the Commit message field, type
Add custom variables, ensure the Target Branch set tomain, and click Commit changes.
Add group- and project-level variables
- Navigate to your My Test Group group by selecting it from the breadcrumbs at the top of the page.
- In the left-hand navigation pane, select Settings > CI/CD
- In the Variables section, select the Expand button.
- Select Add Variable. Enter
group_level_variablein the Key dialog box. Hint: watch your spelling, capitalization, and underscores! This variable will be visible within all subgroups and projects that live in this group. - Type
I'm a variable set at the group levelin the Value dialog box. - Leave all other options at their defaults and select Add variable.
- Navigate to your project by clicking My Test Group - <USERNAME> in the breadcrumbs at the top of the page, and then clicking on your project.
- Repeat steps 2 to 6, entering
project_level_variablein the Key field andI'm a variable set at the project levelin the Value field. - Setting variables does not trigger a pipeline run, so click CI/CD > Pipelines in the left navigation pane, click the Run Pipeline button, and click the second Run Pipeline button.
- Select on the widget for the
environment variablesjob from your running pipeline and verify the variables and their values are correctly displayed in the job output.
Suggestions?
If you wish to make a change to the Hands-On Guide for Denomas CI/CD, please submit your changes via Merge Request!
17188382)
