In my pipeline, I'd like to have a job run only if the Merge Requests target branch is a certain branch, say master or release.
Is this possible?
I've read through https://docs.gitlab.com/ee/ci/variables/ and unless I missed something, I'm not seeing anything that can help.
Go to Settings > CI/CD. Click Expand in the Variables section. Select the State and Masked values you want for your variable.
A merge request is simply a request from a user to merge their code from one branch to another, typically to the master branch.
By default, external contributors who work in forks can't create pipelines in the parent project. When a merge request that comes from a fork triggers a pipeline: The pipeline is created and runs in the fork (source) project, not the parent (target) project.
Update: 2019-03-21
GitLab has variables for merge request info since version 11.6 (https://docs.gitlab.com/ce/ci/variables/ see the variables start with CI_MERGE_REQUEST_
). But, these variables are only available in merge request pipelines
.(https://docs.gitlab.com/ce/ci/merge_request_pipelines/index.html)
To configure a CI job for merge requests, we have to set:
only:
- merge_requests
And then we can use CI_MERGE_REQUEST_*
variables in those jobs.
The biggest pitfall here is only: merge_request
has complete different behavior from normal only/except
parameters.
usual only/except
parameters:
(https://docs.gitlab.com/ce/ci/yaml/README.html#onlyexcept-basic)
only
defines the names of branches and tags for which the job will run.except
defines the names of branches and tags for which the job will not run.
only: merge_request
: (https://docs.gitlab.com/ce/ci/merge_request_pipelines/index.html#excluding-certain-jobs)
The behavior of the
only: merge_requests
parameter is such that only jobs with that parameter are run in the context of a merge request; no other jobs will be run.
I felt hard to reorganize jobs to make them work like before with only: merge_request
exists on any job. Thus I'm still using the one-liner in my original answer to get MR info in a CI job.
Original answer:
No.
But GitLab have a plan for this feature in 2019 Q2: https://gitlab.com/gitlab-org/gitlab-ce/issues/23902#final-assumptions
Currently, we can use a workaround to achieve this. The method is as Rekovni's answer described, and it actually works.
There's a simple one-liner, get the target branch of an MR from the current branch:
script: # in any script section of gitlab-ci.yml
- 'CI_TARGET_BRANCH_NAME=$(curl -LsS -H "PRIVATE-TOKEN: $AWESOME_GITLAB_API_TOKEN" "https://my.gitlab-instance.com/api/v4/projects/$CI_PROJECT_ID/merge_requests?source_branch=$CI_COMMIT_REF_NAME" | jq --raw-output ".[0].target_branch")'
Explanation:
CI_TARGET_BRANCH_NAME
is a newly defined variable which stores resolved target branch name. Defining a variable is not necessary for various usage.
AWESOME_GITLAB_API_TOKEN
is the variable configured in repository's CI/CD variable config. It is a GitLab personal access token(created in User Settings) with api
scope.
About curl
options: -L
makes curl aware of HTTP redirections. -sS
makes curl silent(-s
) but show(-S
) errors. -H
specifies authority info accessing GitLab API.
The used API could be founded in https://docs.gitlab.com/ce/api/merge_requests.html#list-project-merge-requests. We use the source_branch
attribute to figure out which MR current pipeline is running on. Thus, if a source branch has multiple MR to different target branch, you may want to change the part after |
and do your own logic.
About jq
(https://stedolan.github.io/jq/), it's a simple CLI util to deal with JSON stuff(what GitLab API returns). You could use node -p
or any method you want.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With