for this condition , the pipeline didn't trigger ? why? - gitlab

[rules:
if: '$CI_COMMIT_BRANCH =~ /^SPRINT[-][0-9]+/i'
when: always
if: '$CI_COMMIT_BRANCH !~ /^SPRINT[-][0-9]+/i'
when: never ]
If I add like this , its triggers, for the pipeline only for (sprint) name related branches but I want the rules like
[ rules:
if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH && '$CI_COMMIT_BRANCH =~ /^SPRINT[-][0-9]+/i'
when: always
if: $CI_COMMIT_BRANCH != $CI_DEFAULT_BRANCH && '$CI_COMMIT_BRANCH !~ /^SPRINT[-][0-9]+/i'
when: never]
-If I gave like this, its doesn't trigger for sprint related branches. I want to run the pipeline for (sprint)named branches and also default branch and also specific named branches like dev , stage which I'm using in a single command line
.
.
.
.
.
.
If I split the command also, I doesn't works
for eg:
[rules:
if: '$CI_COMMIT_BRANCH =~ /^SPRINT[-_][0-9]+/i'
when: always
if: '$CI_COMMIT_BRANCH !~ /^SPRINT[-_][0-9]+/i'
when: never
if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH || $CI_COMMIT_BRANCH == 'dev'
when: always
if: $CI_COMMIT_BRANCH != $CI_DEFAULT_BRANCH || $CI_COMMIT_BRANCH != 'dev'
when: never]- It doesn't trigger in dev branch
. can you please anyone help me to sort it out
Cyril I

Rules are evaluated one by one and evaluation stops on the first expression that evaluates to true. Your first two regex rules (=~ then !~ for the same regex pattern) will prevent any further rules from being evaluated because one of those two expressions will always evaluate true.

Related

GitLab CI - Check if branch is within list of branches

I want to be able to set a list of branches on the GitLab CICD variables on the UI and have the pipeline check that list to see if it should run a specific job.
I am trying to turn this rule:
rules:
- if: $CI_COMMIT_BRANCH == "poc5"
- if: $CI_COMMIT_BRANCH == "poc5-s1"
- if: $CI_COMMIT_BRANCH == "poc5-s2"
- if: $CI_COMMIT_BRANCH == "poc5-s3"
Into this:
rules:
- if: $CI_COMMIT_BRANCH in $DEFAULT_BRANCHES
Where $DEFAULT_BRANCHES is a variable defined in the projects CICD variables as follows:
DEFAULT_BRANCHES:
poc5
poc5-s1
poc5-s2
poc5-s3
or
poc5 poc5-s1 poc5-s2 poc5-s3
The best I could come up with was the following, which results in an invalid syntax for GitLab CI rules:
rules:
- if: echo $DEFAULT_BRANCHES | tr ' ' '\n' | grep -q -E "^${CI_COMMIT_BRANCH}$"
I am essentially trying to not have the list of branches hard coded into the pipeline yml file.
Is there a way to achieve what I am trying to do?
A solution is:
# .gitlab-ci.yml
if: "$CI_COMMIT_BRANCH == 'poc5' || $CI_COMMIT_BRANCH == 'poc5-s1' || $CI_COMMIT_BRANCH == 'poc5-s2' || $CI_COMMIT_BRANCH == 'poc5-s3'"
when: on_success
But a shorter version with regex is:
# .gitlab-ci
if: $CI_COMMIT_BRANCH =~ /^poc5(-s[1-3])?$/
when: on_success

Gitlab CI/CD workflow rule variables only allow one if condition

I am currently trying to structure my .gitlab-ci.yml file like following:
There is one pipeline called "a" which should run only from the develop branch
There is one pipeline called "b" which should run only from the develop branch but only when triggered from the web
In order to avoid putting these rules in the jobs directly (in reality there are about 30 jobs) I want to reuse those rules globally. I could not find a better way other than using workflow:rules.
workflow:
rules:
- if $CI_COMMIT_BRANCH == "develop"
variables:
RUN_A: "true"
- if $CI_COMMIT_BRANCH == "develop" && $CI_PIPELINE_SOURCE == "web"
variables:
RUN_B: "true"
job-a:
rules:
- if: $RUN_A == "true"
script:
- exit 0
job-b:
rules:
- if: $RUN_B == "true"
script:
- exit 0
The problem is, that if there is a web trigger on develop, it will not set "RUN_B" to true. It will just have "RUN_A" with true. I added another job to print out the variables just to make sure:
test:
image: alpine
script:
- echo $RUN_A
- echo $RUN_B
This will only print true for RUN_A but nothing for RUN_B. I could not find anything in Gitlabs documentation that states it will only use the first matching rule. Anyhow, is there a better way to handle this? What am I missing?
Because GitLab ci Rules evaluated in order until the first match.
in your first rule if $CI_COMMIT_BRANCH == "develop" is match, the pipeline is create, in this times variable RUN_A: "true" is set, but variable RUN_B is undefined.
In your case, you can modify your .gitlab-ci.yml. variable setting and rules order.
workflow:
rules:
- if: $CI_COMMIT_BRANCH == "develop" && $CI_PIPELINE_SOURCE == "web"
variables:
RUN_B: "true"
RUN_A: "true"
- if: $CI_COMMIT_BRANCH == "develop"
variables:
RUN_A: "true"
default:
image: alpine
job-a:
rules:
- if: $RUN_A == "true"
script:
- exit 0
job-b:
rules:
- if: $RUN_B == "true"
script:
- exit 0
test:
script:
- echo $RUN_A
- echo $RUN_B

why im getting Cleaning up file based variables 00:01 ERROR: Job failed: exit code 1 in gitlab pipeline creation

commit-message-validator:
stage: validate-commit-message
script:
- echo "$CI_COMMIT_MESSAGE"
- echo "$CI_COMMIT_BRANCH"
- echo "check the Prefix of the commit message should have one of 'fix' || 'feat' || 'major' || 'minor' in case sensitive"
- exit 1
rules:
- if: $CI_COMMIT_MESSAGE =~ /fix:/
when: never
- if: $CI_COMMIT_MESSAGE =~ /feat:/
when: never
- if: $CI_COMMIT_MESSAGE =~ /major:/
when: never
- if: $CI_COMMIT_MESSAGE =~ /minor:/
when: never
- if: "$CI_COMMIT_MESSAGE =~ /^chore\(release\):.*/"
when: never
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
when: never
- when: always
enter image description here
The last step in your script block is exit 1. Gitlab CI will fail any job that has an exit code > 0.
The commit-message-validator job doesn't currently do any checks, it simply calls echo 3 times. If you want it to check for fix, feat, major, minor you will need to do write some logic in the script block to do so.

Can we use "only" instead of "rules" in gitlab?

In my gitlab ci/cd script , we are using rules
workflow:
rules:
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH || $CI_COMMIT_BRANCH == "developer" || $CI_COMMIT_BRANCH == "stage"|| ($CI_COMMIT_BRANCH =~ (/^([A-Z]([0-9][-_])?)?SPRINT(([-_][A-Z][0-9])?)+/i))
when: always
- if: $CI_COMMIT_BRANCH != $CI_DEFAULT_BRANCH || $CI_COMMIT_BRANCH != "developer" || $CI_COMMIT_BRANCH != "stage"|| ($CI_COMMIT_BRANCH !~ (/^([A-Z]([0-9][-_])?)?SPRINT(([-_][A-Z][0-9])?)+/i))
when: never
for specific job , we are using only
only:
- developer
- stage
FINE. but I want the scenerio like
only:
- developer
- stage
- (/^([A-Z]([0-9][-_])?)?SPRINT(([-_][A-Z][0-9])?)+/i))
the sprint which I used is not working . If I use "rules" , its working , but I want "only" to be used in my script . Please help me to sort it out this.
An only: directove does support regular expressions that match against branch names.
But the syntax is like /^feature-.*/, so check if the outer () are in the way.
Plus, there seems to be an imbalance in the 5 ( vs. 6 ).
So either:
(/^([A-Z]([0-9][-_])?)?SPRINT(([-_][A-Z][0-9])?)+/i)
Or
/^([A-Z]([0-9][-_])?)?SPRINT(([-_][A-Z][0-9])?)+/i

How to run a gitlab job except a variable has some value?

I want to run a gitlab job in every case EXCEPT the variable RUN is defined and contains the value run3.
So again:
variable RUN is undefined: run the job
variable RUN is defined and does not contain the value run3: run the job
variable RUN is defined and does contain the value run3: DO NOT run the job
I tried the following rules:
rules:
- if: $RUN != null && $RUN == "run3"
when: never
rules:
- if: $RUN != "" && $RUN == "run3"
when: never
rules:
- if: $RUN == "run3"
when: never
Without the variable RUN being defined, no job was run. I expected the job to have been run.
How to fix it? Where is the problem in the logic?
After some time-consuming try-and-error trials I found the solution: As you cannot test if a variable exists, you have to create the rule the other way around. It means, you run the job if the value does not match:
The correct rule is then:
- if: $RUN != "run3"
when: always
- when: never
The first part only evaluates to true if the variable exists AND if the variable does not evaluate to run3. In that case the job is run.
The second part then tells to NOT run the job in any other case.
You're close
rules:
- if: $RUN == "run3"
when: never
This is correct, but you also need a default - when:
To emulate the default behavior of no rules when the first rule does not match, set the default as on_success
rules:
- if: $RUN == "run3"
when: never # don't run the job in this case
- when: on_success # run per usual in all other cases
Alex, maybe you can try:
if: $RUN != undefined && $RUN == "run3"
when: never

Resources