Gitlab CI Rules - Run only on push to origin/master - gitlab

I'm looking for a bit of an unusual use case, where I only run certain jobs after a push to origin/master. In practice, this only happens when a merge goes in so my first attempt at this was the following rule:
if: '$CI_PIPELINE_SOURCE == "merge_request_event" && $CI_COMMIT_BRANCH == "master"'
This didn't work though, presumably because the actual merge commit pushed to origin/master is not considered a merge_request_event.
Any way to achieve this?
Note: I don't simply use if: $CI_COMMIT_BRANCH == "master"' to avoid running on a push to a non-origin fork's master branch.

I found a way to do this using the uniquely assigned PROJECT_ID gitlab variable (it's different for each fork). So the following works:
if: '$CI_PROJECT_ID == "<origin fork's project_id>" && $CI_COMMIT_BRANCH == "master"'
The project ID can be found in Settings on gitlab.

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).

How can I only allow merge request from one specific branch to another specific branch in GitLab?

In GitLab, I just wan to the merge requests from sit -> uat -> master.
How can I implement it by pipeline?
You should be albe to use a combination of:
rules:if, as illustrated in workflow:rules
Predefined variables reference, like
CI_PIPELINE_SOURCE: How the pipeline was triggered
CI_MERGE_REQUEST_TARGET_BRANCH_NAME: The target branch name of the merge request.
CI_MERGE_REQUEST_SOURCE_BRANCH_NAME: The source branch name of the merge request.
Something like:
sit-to-uat:
stage: build
image: yourImage
script:
- # do something
rules:
- if: '$CI_PIPELINE_SOURCE == "merge_request_event" &&
$CI_MERGE_REQUEST_TARGET_BRANCH_NAME =~ /^sit$/' &&
$CI_MERGE_REQUEST_TARGET_BRANCH_NAME =~ /^uat$/'
when: always

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...

skip pipeline for a branch in workflow keyword

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

How to run Gitlab-CI pipelines only branch and tag?

I just want to run pipelines when tagged from main branch. I tried using workflow but it doesn't work.
This is my .gitlab-ci.yml file.
workflow:
rules:
- if: '$CI_COMMIT_BRANCH == "develop"'
variables:
CHART_GIT_URL: $CHART_DEV_URL
CHART_VALUES_FILE: "values-dev.yaml"
DOCKER_IMAGE_TAG: "dev-$CI_COMMIT_SHORT_SHA"
- if: $CI_COMMIT_TAG && $CI_COMMIT_BRANCH == "main"
variables:
CHART_GIT_URL: $CHART_PROD_URL
CHART_VALUES_FILE: "values-prod.yaml"
DOCKER_IMAGE_TAG: "v$CI_COMMIT_TAG"
stages:
- build and push
- deploy
package Docker image:
stage: build and push
before_script:
- docker login $DOCKER_REGISTRY -u $DOCKER_REGISTRY_USER -p $DOCKER_REGISTRY_PASSWD
script:
- docker build -t $DOCKER_IMAGE_NAME:$DOCKER_IMAGE_TAG .
- docker push $DOCKER_IMAGE_NAME:$DOCKER_IMAGE_TAG
rules:
- if: '$CI_COMMIT_BRANCH == "develop"'
- if: $CI_COMMIT_TAG && $CI_COMMIT_BRANCH == "main"
Thanks for the help!
tagged from main branch
Unfortunately, this is not possible. Git tags are only associated with commits, not branches. Therefore, you cannot create a condition for a tag to be created "from" a branch because that's not how tags work. Also consider that a tagged ref can exist on many branches, or even no branch at all.
This is also the reason why the predefined variables CI_COMMIT_TAG and CI_COMMIT_BRANCH will never be present together. If a pipeline is associated with a tag, it cannot be associated with a branch and vice versa.
The best you might be able to do is to run only on tags, then check if the tagged ref exists in main in the job itself. Unfortunately this is not possible to do with rules:.

Resources