skip pipeline for a branch in workflow keyword - gitlab

While developing gitlab ci-cd pipeline i want to run pipeline manually for a particular branch for example branch name is "develop-tool". what i want is anything done on this branch should not trigger pipeline automatically. it should be manually. what i have tried is:
workflow:
rules:
- when: manual # Error: workflow:rules:rule when unknown value: manual
then i tried to do this:
- if: '$CI_PIPELINE_SOURCE == "web" && $CI_COMMIT_REF_NAME !~ /^.*-$develop-tool/'
- when: always
its taking job to some infinite loop and it keeps running until and unless you cancel it and run the job again. does anyone have any other way to achieve this goal.

Have the configuration skip pipelines when the source is not the web for that branch. So the only way it can be triggered on that branch is through the web UI.
workflow:
rules:
- if: '$CI_COMMIT_BRANCH == "develop-tool" && $CI_PIPELINE_SOURCE != "web"'
when: never
- when: always

Related

Gitlab-ci: Getting error when I use rules and downstream pipeline trigger in the same stage

I would like to trigger downstream pipeline but before that I need to eliminate the branches with rule method and I just want to trigger it only with specific branches. But I'm getting this error when I run the pipeline downstream pipeline cannot be created reference not found Did I miss something or can't I use rule and trigger methods in the same stage?
My stage:
test:
stage: test
variables:
branch: $CI_COMMIT_BRANCH
trigger:
project: test/project
strategy: depend
branch: $branch
allow_failure: false
rules:
- if: $CI_COMMIT_BRANCH == 'main' || $CI_COMMIT_BRANCH == 'test'
- when: never
Looks like the dash on the last line is not needed. Probably you meant this:
rules:
- if: $CI_COMMIT_BRANCH == 'main' || $CI_COMMIT_BRANCH == 'test'
when: never
I also recommend to check documentation for $CI_COMMIT_BRANCH:
The commit branch name. Available in branch pipelines, including
pipelines for the default branch. Not available in merge request
pipelines or tag pipelines.
You could try to use $CI_COMMIT_REF_NAME variable instead which is available in merge request or tag pipelines. You could try to debug this adding some echo commands (like - echo $CI_COMMIT_REF_NAME and - echo $branch to be sure that the branch really exists in the downstream project and variable are properly expanded.
But first of all you need to fix the syntax (that extra dash).

".gitlab-ci.yml" rule to do not run a pipeline on merge request creation

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"

Triggering a gitlab pipeline ONLY for merge requests that are actually merged

I'm trying to trigger a gitlab pipeline only after the merge request is actually merged, only for merge requests, and only if it comes from the 'Develpment' branch.
For that I'm using this rule:
rules:
- if: '$CI_COMMIT_REF_NAME == "Production"'
when: manual
- if: '$CI_MERGE_REQUEST_EVENT_TYPE == "merged_result" && $CI_COMMIT_REF_NAME == "Development"'
when: on_success
However, it seems to be running on any commit to the branch Development...

Stop detach pipelines from getting created

I had a gitlab job defined as follows.
test-1:
stage: test
only:
variables:
- $RUN_TEST
except:
- tags
and then I changed that job to run based on gitlab rules.
test-1:
stage: test
rules:
- if: '$CI_PIPELINE_SOURCE != "schedule" && $CI_COMMIT_TAG == null'
- if: '$RUN_TESTS == "true" && $CI_COMMIT_TAG == null'
After this change, Whenever I commit some change to my branch, I am getting two pipeline runs in my gitlab project. One is for the latest commit I have made and second as detached.
How can I get rid of detached pipeline run?
Another interesting thing is, when I cancel the latest pipeline the detach pipeline also got cancelled.
For latest pipeline, value of CI_PIPELINE_SOURCE = push and for detach pipeline, value of CI_PIPELINE_SOURCE = merge_request_event.
The detached pipelines you are seeing are merge request pipelines. You can use workflow:rules to control when a pipeline is created. To prevent merge request pipelines:
workflow:
rules:
- if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
when: never
- when: always

Gitlab-ci: if MR exist just trigger merge_request detach pipeline, if not trigger source branch pipeline. Those 2 pipelines shouldn't run in same time

I want to see sonar results in the MR(merge request) command section when I create a MR.
My main expectations:
if there is an existing MR for the source branch, trigger detached pipeline (do not trigger feature pipeline. I need only that one for reviewing sonar results in MR commands)
if there isn't an existing MR for the source branch, just trigger the normal feature(source) branch pipeline
I tried to do it with the below example stage. But when I pushed the commit to the source pipeline, while MR is exist for source branch. I still getting double pipeline. Detach and source pipelines are running and I don't want to see both in same time, plus except not working with rules configuration. How can I integrate except section with rules part.
This is my gitlab-ci stage:
deploy:
stage: deployment
when: manual
rules:
- if: '$CI_PIPELINE_SOURCE == "merge_request_event" && $CUSTOM_VARIABLE == "true" || $CUSTOM_VARIABLE == "true"'
script:
- ....
- ....
except:
- tags
- main
I also tried below rules, if one of them fit my condition don't run the other one. But it still trigger both pipelines.
deploy:
stage: deployment
when: manual
rules:
- if: '$CI_PIPELINE_SOURCE == "merge_request_event" && $CUSTOM_VARIABLE == "true"'
when: on_success
- if: '$CI_PIPELINE_SOURCE == "push" && $CUSTOM_VARIABLE == "true"'
when: on_success
script:
- ....
- ....
except:
- tags
- main
Covered in workflow:rules templates, In this case, you can use the CI_OPEN_MERGE_REQUESTS variable to determine whether to run the pipeline for merge request or just the feature branch.
If you use both [pipelines for merge requests and branch pipelines], duplicate pipelines might run at the same time. To prevent duplicate pipelines, use the CI_OPEN_MERGE_REQUESTS variable.
Using workflow:rules you can do this for the entire pipeline, but the same principle can also be applied to individual jobs.
workflow:
rules:
- if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
- if: '$CI_COMMIT_BRANCH && $CI_OPEN_MERGE_REQUESTS'
when: never
- if: '$CI_COMMIT_BRANCH'
This means your pipeline will run:
for merge requests
for branch pipelines UNLESS there is an open merge requests

Resources