I want to have a pipeline that needs to run at each merge request. I would also like to have the ability to run that pipeline manually so that one can anticipate merge issues.
However, if I specify my jobs as
only: - merge_requestswhen trying to run the pipeline manually, I am greeted with a
Pipeline cannot be run. No stages / jobs for this pipeline.
If I use workflows instead,
include: - template: 'Workflows/MergeRequest-Pipelines.gitlab-ci.yml'I am greeted with a different but equivalent
Pipeline cannot be run. Pipeline filtered out by workflow rules.
So how can I set up a pipeline that is required for merge requests but that could also be run manually by users on their feature branch?
3 Answers
You should use rules instead of only/except as the later will be deprecated in the future.
But instead of configuring when every job should run you can use workflow and define for the whole pipeline when it should be created.
In your example the following workflow will create a pipeline if it is a merge-request and if the pipeline was manually triggered without a push.
For further reference you can take a look at the possible values for CI_PIPELINE_SOURCE if you want to change that in the future.
workflow: rules: - if: '$CI_PIPELINE_SOURCE == "merge_request_event"' - if: '$CI_PIPELINE_SOURCE == "web"' 1 Usually, I'd recommend using the newer rules statement instead of only, that way if you want to run it only manually in merge requests, I'd do something like the following:
The CI_PIPELINE_SOURCE is a default variable from Gitlab ci should be useful for your use case.
Danielnelz's answer is correct. But note that if you're using merge request pipelines, something like this:
workflow: rules: - if: '$CI_PIPELINE_SOURCE == "merge_request_event"' - if: "$CI_COMMIT_BRANCH && $CI_OPEN_MERGE_REQUESTS" when: never - if: "$CI_COMMIT_BRANCH" - if: "$CI_COMMIT_TAG" - if: '$CI_PIPELINE_SOURCE == "web"'Won't work. You will have to move the '$CI_PIPELINE_SOURCE == "web"' line to above the "$CI_COMMIT_BRANCH && $CI_OPEN_MERGE_REQUESTS" line. Like this:
workflow: rules: - if: '$CI_PIPELINE_SOURCE == "web"' - if: '$CI_PIPELINE_SOURCE == "merge_request_event"' - if: "$CI_COMMIT_BRANCH && $CI_OPEN_MERGE_REQUESTS" when: never - if: "$CI_COMMIT_BRANCH" - if: "$CI_COMMIT_TAG" Why? Because if this condition is true, $CI_COMMIT_BRANCH && $CI_OPEN_MERGE_REQUESTS (you have an open merge request), gitlab CI won't continue reading the following if conditions, so it will never reach the $CI_PIPELINE_SOURCE == "web" line. In summary, if you don't make this change you won't be able to run a pipeline from the website having an open MR.