I'm trying to use changes block inside rules block in the downstream pipeline:
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
changes:
- "projects/testing/**"
When doing it like this the job is not created. If i remove the changes part and just go with:
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
It works just fine. I dont know if i'm missing something here.
Edit. I managed to get it working in the MR pipelines with this code:
rules:
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
changes:
- "projects/demo-app/**"
- "*.tf"
when: on_success
- if: $CI_MERGE_REQUEST_ID
changes:
- "projects/demo-app/**"
- "*.tf"
when: on_success
- when: never
But when change is merged it doesn't trigger the pipeline anymore.
Related
My aim: add "run pipeline" button to merge request -> pipelines page
Initially I had following .gitlab-ci.yaml file:
include:
- project: <erased>
ref: latest
file:
- /pipelines/my_chart.yaml
...
...
my_chart.yaml:
workflow:
rules:
- if: $CI_COMMIT_BRANCH
- if: $CI_COMMIT_TAG
..
I started to google and faced following article:
https://docs.gitlab.com/ee/ci/pipelines/merge_request_pipelines.html#:~:text=Run%20when%20you%3A,tab%20in%20a%20merge%20request
so I've modified(added the single line) my_chart.yaml like this:
workflow:
rules:
- if: $CI_COMMIT_BRANCH
- if: $CI_COMMIT_TAG
- if: $CI_PIPELINE_SOURCE == 'merge_request_event'
...
This changed added "run pipeline" button to merge request pipeline page but if I have a MR and make additional commit to the branch - 2 pipelines are started simultaneously. It is redundant - only 1 pipeline should be started.
How can I achieve it ?
P.S.
this one doesn't work
workflow:
rules:
- if: $CI_PIPELINE_SOURCE == 'merge_request_event'
- if: $CI_COMMIT_BRANCH && $CI_PIPELINE_SOURCE != 'merge_request_event'
- if: $CI_COMMIT_TAG
Important note before going further:
The question GitLab do not run CI/CD pipeline when creating new merge request is not a duplicate:
I am asking about ".gitlab-ci.yml" rules, but that question has no answers about this.
Current GitLab (default) behavior
On an issue page I click "Create Merge Request" --> A new pipeline is started automatically.
Required behavior
Do not run a pipeline on merge request creation
My current ".gitlab-ci.yml"
.default_rules:
rules:
- if: '$CI_PIPELINE_SOURCE == "web"'
when: manual
- if: $CI_PIPELINE_SOURCE == "merge_request_event"
changes:
- "**/*.{py,c,cpp}"
- .gitlab-ci.yml
- poetry.lock
- if: $CI_COMMIT_BRANCH && $CI_OPEN_MERGE_REQUESTS
when: never
- if: $CI_COMMIT_BRANCH
changes:
- "**/*.{py,c,cpp}"
- .gitlab-ci.yml
- poetry.lock
- if: $PIPELINE_TYPE == "multi-project-pipeline"
SOLUTION
Do not run a job on Merge Request creation:
# .gitlab-ci.yml
your_job_name:
rules:
- if: $CI_COMMIT_BEFORE_SHA == "0000000000000000000000000000000000000000"
when: never
A LITTLE USEFUL BONUS
Full rules to avoid duplicate pipelines (just in case if someone needs this):
rules:
# Prevent creating duplicate pipelines because pipelines are already created for PUSH events.
- if: $CI_PIPELINE_SOURCE == "merge_request_event"
when: never
# Do not run after Merge Request closing.
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
when: never
# Do not run on Merge Request creation.
- if: $CI_COMMIT_BEFORE_SHA == "0000000000000000000000000000000000000000"
when: never
# When to run:
- if: $CI_PIPELINE_SOURCE == "push"
changes:
# If the code has changed
- "**/*.py"
I have a parent pipeline as follows:
workflow:
rules:
- if: $CI_PIPELINE_SOURCE == "web"
- if: $CI_MERGE_REQUEST_IID
- if: $CI_COMMIT_BRANCH && $CI_OPEN_MERGE_REQUESTS
when: never
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
- if: $CI_COMMIT_BRANCH == "master"
stages:
- triggers
deidentify:
stage: triggers
trigger:
include: microservice_a/.gitlab-ci.a.yml
strategy: depend
only:
changes:
- microservice_a/*
ebridge_export:
stage: triggers
trigger:
include: microservice_b/.gitlab-ci.b.yml
strategy: depend
only:
changes:
- microservice_b/*
If I push any changes under microservice_a/ e.g. microservice_a/.gitlab-ci.a.yml it NEVER starts any pipeline, but if I change
only:
changes:
- microservice_a/*
To
rules:
- changes:
- microservice_a/*
On both, it will always run both regardless of whether the change was actually under microservice_a or microservice_b.
Now, the child pipelines both have a small test job that looks like this:
stages:
- test
test:
stage: test
rules:
- when: always
If I remove the rules section on both (while using rules in the parent pipeline), both will fail because it couldn't find any jobs to add to the pipeline
What am I doing wrong?
workflow:
rules:
- if : '$CI_COMMIT_BRANCH == "Sprint-Release-Branch"'
when: never
- if : '$CI_PIPELINE_SOURCE == "merge_request_event" && $CI_PIPELINE_SOURCE == "push"'
when: never
- when: always
stages:
- Stage1
- Stage2
- Stage3
Task1:
stage: Stage1
script:
- echo "Stage1"
rules:
- if: '$CI_COMMIT_TAG =~ /^\d+\.\d+\.DEV\d+/'
tags:
- Runner
Task2:
stage: Stage1
script:
- echo "Checking code standard as per "Coding Standards""
rules:
- if: '$CI_COMMIT_TAG =~ /^\d+\.\d+\.DEV\d+/'
allow_failure: true
tags:
- Runner
Task3:
stage: Stage2
script:
- echo "Stage2"
when: manual
tags:
- Runner
Task4:
stage: Stage3
script:
- echo "Stage3"
when: manual
tags:
- Runner
Above is my Gitlab CI file, where i am trying
pipeline should not add jobs when there is merge & push requests happened on "Sprint-Release-Branch"
but whenever any merge request done "feature branches" onto "Sprint-Release-Branch" jobs which are defined as "when: manual" get added in pipeline.
So in my situation, Dev team is creating different feature branch for different user-stories, and then merging those features branches onto Sprint-Release-Branch having above yml file. So multiple jobs are getting added in pipeline continuously for every merge_request which are defined with "manual" trigger
How can i make optimized my yml so that jobs having manual trigger should not get added in pipeline.
which are defined as "when: manual" get added in pipeline.
You have to repeat the whole logic when you overwrite it.
Task3:
...
rules:
- if : '$CI_COMMIT_BRANCH == "Sprint-Release-Branch"'
when: never
- if : '$CI_PIPELINE_SOURCE == "merge_request_event" && $CI_PIPELINE_SOURCE == "push"'
when: never
- when: manual
Also, it's better to use when: on_success, not always.
Do something like the following with yaml anchors:
.myrules: &myrules
if: $CI_COMMIT_BRANCH == "Sprint-Release-Branch" || ($CI_PIPELINE_SOURCE == "merge_request_event" && $CI_PIPELINE_SOURCE == "push")
when: never
workflow:
rules:
- *myrules
- when: on_success
Task3:
...
rules:
- *myrules
- when: manual
I'm using rule method in my stage. But with rule method, I don't want to trigger CI run for tags.
I need to find a way to avoid tags like except: - tags. Unfortunately except not working with rules. In shortly how can I avoid it?
I use this snippet in my stage but it did not work, it still triggers the pipeline after the creation of the tag.
- if: '$CI_COMMIT_TAG'
when: never
This is my main deployment stage:
deploy:
stage: deployment
rules:
- if: '$CI_PIPELINE_SOURCE == "merge_request_event" && $sample == "one"'
when: manual
- if: '$CI_COMMIT_BRANCH && $CI_OPEN_MERGE_REQUESTS'
when: never
- if: '$CI_COMMIT_TAG'
when: never
- if: '$CI_COMMIT_BRANCH && $sample == "one"'
when: manual
script:
- ...
- ...
I resolved the problem. Just I added - when: never at the end of the rules. As a separate rule, it will block all different trigger cases.
deploy:
stage: deployment
rules:
- if: '$CI_PIPELINE_SOURCE == "merge_request_event" && $sync == "one"'
when: manual
- if: '$CI_COMMIT_BRANCH && $CI_OPEN_MERGE_REQUESTS'
when: never
- if: '$CI_COMMIT_TAG'
when: never
- if: '$CI_COMMIT_BRANCH && $sync == "false"'
when: manual
- when: never
script:
- ...
- ...