Share script between .gitlab-ci.yml jobs - gitlab-ci

Have a look at the snippet below. I want to find a way to reduce duplicate code by placing command_A and command_B at a location where job1 and job2 both run it
job1:
script:
- command_A
- command_B
- command_C
job2:
script:
- command_A
- command_B
- command_D

In your case, probably the easiest would be to use YML Anchors.
Anchors are YML's way of reusing code - you can think of them a little bit like functions.
You can define a block of configuration somewhere and create a reference to it using &. Then, you can use it with *.
# Create an anchor called `&common`
.common: &common:
- command_A
- command_B
job1:
script:
# Merge the anchor into the `script` using `<<*:`
- <<:*common
- command_C
job2:
script:
- <<:*common
- command_D
To learn more, I found this article to be helpful, and of course, the official Gitlab docs on anchors.
Alternatively, you could simply put all common code in a before_script tag or use .extends keyword - you can see some examples on my blog.

The above answer by Corina is logically correct but technically wrong. I have put the correct script here. Please refer this link.
# Create an anchor called `&common`
.common: &common
- command_A
- command_B
job1:
script:
# Merge the anchor into the `script` using `<<*:`
- *common
- command_C
job2:
script:
- *common
- command_D

While YML Anchors works, the recommended way now is to use Extends
You can reduce complexity and duplicated configuration in your GitLab CI/CD configuration files by using:
YAML-specific features like anchors (&), aliases (*), and map merging(<<).
The extends keyword,
which is more flexible and readable. We recommend you use extends
where possible.
Your pipeline will be :
.common:
before_script:
- command_A
- command_B
job1:
extends:
- .common
script:
- command_C
job2:
extends:
- .common
script:
- command_D

Related

GitLab CI, rules not triggering job

We just started using GitLab and have some problems with the behaviour.
Standing on branch: feature/test, I would expect ALL of the jobs below to be triggered, but only All Branches except master/dev is triggered.
Anyone know what I'm doing wrong?
Reading the documentation here: https://docs.gitlab.com/13.8/ee/ci/yaml/#rules
stages:
- pre-build
- build
- post-build
- test
- publish
- deploy
All Branches except master/dev:
stage: build
image: openjdk:16-slim
script:
- 'cd .tools'
rules:
- if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH'
when: never
- if: '$GIT_COMMIT_BRANCH =~ /^dev.*/'
when: never
- when: always
Only Branch Regex:
stage: build
image: openjdk:16-slim
script:
- 'cd .tools'
rules:
- if: '$GIT_COMMIT_BRANCH =~ /^feature.*/'
Only Branch Specific:
stage: build
image: openjdk:16-slim
script:
- 'cd .tools'
rules:
- if: '$GIT_COMMIT_BRANCH == "feature/test"'
The variable you're using doesn't exist: $GIT_COMMIT_BRANCH, or at least it isn't defined by Gitlab CI, so you'd have to be defining it yourself somewhere, which I don't see in the yaml file. I think you want to use CI_COMMIT_BRANCH, which is predefined by Gitlab, and contains the name of the branch that is being built.
The reason your jobs aren't running correctly is since the variable doesn't exist, the conditionals are evaluating as 'false', so the last two jobs will never run, and the "All branches except master/dev" will run for all except your default branch due to the second conditional. The first conditional, checking if the branch is the default branch, works fine. The second one evaluates as false since the variable doesn't exist, so it falls to the third rules line: when: always.
You should be able to fix it by changing the variable name. However, note that CI_COMMIT_BRANCH doesn't always exist. If a pipeline was started from a merge request, this variable will be empty. Also, if you use tags in your development workflow, this will be empty. The CI_COMMIT_REF_NAME variable on the other hand will hold the branch or tag name of the item being built, and will exist if a Merge Request started the pipeline. In that case, it holds the value of CI_MERGE_REQUEST_SOURCE_BRANCH_NAME (they're equivalent).
But, if you don't use tags and either don't use Merge Requests or handle those pipelines separately, CI_COMMIT_BRANCH will work fine.

how to pass value from commit to GitLab CI pipeline as variable?

I need to dynamically pass value to GitLab CI pipeline to pass the value further to jobs. The problem is: the value cannot be stored in the code and no pipeline reconfiguration should be needed (e.g. I can pass the value in "variables" section of .gitlab-ci.yml but it means store value in the code, or changes in "Environment variables" section of "CI / CD Settings" means manual reconfiguration). Also, branch name cannot be used for that purpose too.
It is not a secret string but a keyword which modifies pipeline execution.
So, how can I do it?
You didn't specify the source of this value.
You say "pass value from commit to ..."
If it's some meta information about the commit itself, look at the list of Predefined environment variables
There's quite a lot of vars named CI_COMMIT_* which might work for you.
However,
if it's some value that you generate in the pipeline in one job and want to pass to another job - it's a different case.
There is a long-living request to Pass variables between jobs, which is still not implemented.
The workaround for this moment is to use artifacts - files to pass information between jobs in stages.
Our use case is to extract Java app version from pom.xml and pass it to some packaging job later.
Here is how we do it in our .gitlab-ci.yml:
...
variables:
VARIABLES_FILE: ./variables.txt # "." is required for image that have sh not bash
...
get-version:
stage: prepare
image: ...
script:
- APP_VERSION=...
- echo "export APP_VERSION=$APP_VERSION" > $VARIABLES_FILE
artifacts:
paths:
- $VARIABLES_FILE
...
package:
stage: package
image: ...
script:
- source $VARIABLES_FILE
- echo "Use env var APP_VERSION here as you like ..."

How to use variables while matching branches in gitlab-ci?

How can I match a branch name a variable? If I set the variable to 1.1.0 I want it to only match release branches that are for that version. I can't seem to do variable replacement here or in a regex (as seen commented out). Are there any options? The 1.1.0 would actually be coming from the project variables, not hard coded in the file so hard coding it really isn't an option here.
This does not seem to do variable replacement.
stages:
- build
variables:
buildNum: $CI_PIPELINE_ID
stagingVersion: 1.1.0
before_script:
- export MYTEAM_BUILD_NUM=${buildNum}
- export MYTEAM_VERSION=${stagingVersion}
build_staging:
stage: build
only:
- release/v${stagingVersion}#myTeam/myProject
# - /^release\/v${stagingVersion}#myTeam\/myProject$/
script:
- doStuff
I was able to come up with a hacky solution to achieve this. It's not great but it gets the job done. It will fail jobs that match the branch criteria but do not match the branch version criteria:
only:
- /^release\/v\d+.\d+.\d+$/#myTeam/myProject
script:
- if ! [ "${CI_COMMIT_REF_NAME}#${CI_PROJECT_PATH}" = "release/v${stagingVersion}#myTeam/myProject" ]; then echo "Staging branch? NO; Exiting"; exit 1; else echo "Staging branch? YES; Continuing"; fi
The only section first filters down to branches that match the release/v0.0.0#myTeam/myProject format
Then the first line of the script runs a bash command which compares the stagingVersion (set in gitlab project variables to the current version for the staging environment) and the $CI_COMMIT_REF_NAME
If the branches match, it prints a success message, if they do not match, it runs exit 1 and fails the build

Does Gitlab-CI support variable expansion within only:refs?

I've got a large .gitlab-ci.yml file with lots of jobs in it. Many of these jobs are filtered to only run on certain branches. When managing this file it would be convenient to define the names of these branches as variables at the top of the file so that only the variables need to be updated if the branch names change. This is a pretty standard practice for constants in most programming languages.
Unfortunately, it doesn't look like this works in Gitlab-CI:
variables:
THIS_DOES_NOT_WORK: "this_works"
lots:
only:
refs:
- this_works
script:
- echo "lots"
of:
only:
refs:
- $THIS_DOES_NOT_WORK
script:
- echo "of"
jobs:
only:
refs:
- $THIS_DOES_NOT_WORK
script:
- echo "jobs"
In the above example, only the "lots" job will be run since the THIS_DOES_NOT_WORK variable is not expanded in the "of" and "jobs" jobs.
The closest documentation which I can find doesn't mention anything about the only:refs keyword. It does go into details on the only:variables keyword. This keyword could provide a nice workaround if we could do something like this instead:
variables:
THIS_DOES_NOT_WORK: "this_works"
lots:
only:
variables:
- $CI_COMMIT_REF_NAME == "this_works"
script:
- echo "lots"
of:
only:
variables:
- $CI_COMMIT_REF_NAME == $THIS_DOES_NOT_WORK
script:
- echo "of"
jobs:
only:
variables:
- $CI_COMMIT_REF_NAME == $THIS_DOES_NOT_WORK
script:
- echo "jobs"
In this case it's explicitly stated in the documentation that this won't work.
The only:variables keyword used for filtering on variable comparisons is ironically incapable of expanding variables.
Is there some other workaround here? Am I missing something?
According to the https://docs.gitlab.com/ee/ci/variables/where_variables_can_be_used.html, it seems that the capability of the ci pipeline increased and it can actually expand variables, now. For me the solution was to use parts of your example:
jobs:
only:
variables:
- $THIS_DOES_WORK == $CI_COMMIT_REF_NAME
script:
- echo "jobs"
In my case $THIS_DOES_WORK is a variable passed from the gitlab ui via the CI/CD variables tab. Gitlab states the constraints variables used in this scope have:
The variable must be in the form of $variable. Not supported are the following:
Variables that are based on the environment’s name (CI_ENVIRONMENT_NAME, CI_ENVIRONMENT_SLUG).
Any other variables related to environment (currently only CI_ENVIRONMENT_URL).
Persisted variables.
Additionally, you should pay attention that the variable is not set to protected, if working on an unprotected branch.

GitLab only builds for specific tag names

Is there a way to instruct the pipeline to only do a step for certain tags that matches a regular expression?
I would like it to do a deploy when I push a tag on the format 1.2.3 (for example). Is there a way to do this?
This should only be run for refs that are not branches named matching the given regex.
job:
only:
- /^(\d+\.)?(\d+\.)?(\*|\d+)$/
except:
- branches
Yes, you can do this with the only option:
job:
# Use regexp
only:
- /^issue-.*$/
job:
# Use special keywords
only:
- tags
- triggers
- schedules
See only/except (basic).
You can also make use of rules:
job:
script: echo "Hello, World!"
rules:
- if: '$CI_COMMIT_TAG =~ /^\d+\.\d+\.\d+$/'