Variables¶
Goal
Learn how to...
- add local variable to your pipeline
- consume pre-defined variables
- add secrets in the UI
Task 1: Create a job variable¶
This exercise requires an updates version of our hello world program:
Add a variable called version
to the job called build
and modify the build command as follows:
See the official documentation about variables.
Afterwards check the pipeline in the GitLab UI. You should see a successful pipeline run.
Hint (Click if you are stuck)
- Use the
variable
keyword to define a variable inside the job calledbuild
- Replace the build command with the one provided above
Solution (Click if you are stuck)
.gitlab-ci.yml
:
If you want to jump to the solution, execute the following command:
git checkout upstream/160_gitlab_ci/020_variables/inline -- '*'
Task 2: Use a predefined variable¶
Read the official documentation about predefined variables and replace the job variable with the predefined variable CI_COMMIT_REF_NAME
.
Afterwards check the pipeline in the GitLab UI. You should see a successful pipeline run.
Hint (Click if you are stuck)
- Remove the
variable
keyword from the job calledbuild
- Replace the variable
${version}
with the predefined variable${CI_COMMIT_REF_NAME}
Solution (Click if you are stuck)
.gitlab-ci.yml
:
If you want to jump to the solution, execute the following command:
git checkout upstream/160_gitlab_ci/020_variables/predefined -- '*'
Task 3: Add a CI variable in the UI¶
This exercise requires an updates version of our hello world application:
The application now also prints the name of the author which must be supplied during compilation as well.
Read the official documentation about CI variables and extend the build command to provide main.Author
through a CI variable called AUTHOR
.
Afterwards check the pipeline in the GitLab UI. You should see a successful pipeline run.
Hint 1 (Click if you are stuck)
- Go to
Settings
>CI/CD
>Variables
- Add a variable called
AUTHOR
with your name
Hint 2 (Click if you are stuck)
The -ldflags
option needs to be extended with -X 'main.Author=${AUTHOR}'
Solution (Click if you are stuck)
.gitlab-ci.yml
:
If you want to jump to the solution, execute the following command: