Skip to content

Expose merge request pipeline variables

Shinya Maeda requested to merge expose-mr-pipeline-variables into master

What does this MR do?

New predefined variables for MR pipelines

Variable name Description Sample value
CI_MERGE_REQUEST_ID The ID of the merge request 34
CI_MERGE_REQUEST_IID The IID of the merge request (per project) 1
CI_MERGE_REQUEST_REF_PATH The ref path to the merge request refs/merge-requests/1/head
CI_MERGE_REQUEST_PROJECT_ID The project id of the merge request 1
CI_MERGE_REQUEST_PROJECT_PATH The project path of the merge request namespace/awesome-project
CI_MERGE_REQUEST_PROJECT_URL The project URL of the merge request http://192.168.10.15:3000/namespace/awesome-project
CI_MERGE_REQUEST_TARGET_BRANCH_NAME The target branch name of the merge request master
CI_MERGE_REQUEST_SOURCE_PROJECT_ID The source project id of the merge request 10
CI_MERGE_REQUEST_SOURCE_PROJECT_PATH The source project path of the source project (with namespace) namespace/awesome-forked-project
CI_MERGE_REQUEST_SOURCE_PROJECT_URL The source project URL of the merge request http://192.168.10.15:3000/namespace/awesome-forked-project
CI_MERGE_REQUEST_SOURCE_BRANCH_NAME The source branch name of the merge request patch-1

Example of export

declare -x CI_MERGE_REQUEST_ID="36"
declare -x CI_MERGE_REQUEST_IID="3"
declare -x CI_MERGE_REQUEST_PROJECT_ID="51"
declare -x CI_MERGE_REQUEST_PROJECT_PATH="root/mr-variables"
declare -x CI_MERGE_REQUEST_PROJECT_URL="http://192.168.10.15:3000/root/mr-variables"
declare -x CI_MERGE_REQUEST_REF_PATH="refs/merge-requests/3/head"
declare -x CI_MERGE_REQUEST_SOURCE_BRANCH_NAME="patch-3"
declare -x CI_MERGE_REQUEST_SOURCE_PROJECT_ID="51"
declare -x CI_MERGE_REQUEST_SOURCE_PROJECT_PATH="root/mr-variables"
declare -x CI_MERGE_REQUEST_SOURCE_PROJECT_URL="http://192.168.10.15:3000/root/mr-variables"
declare -x CI_MERGE_REQUEST_TARGET_BRANCH_NAME="master"

Usage: Sample script to create a merge commit within the job

job:
  script:
  - git fetch "$CI_MERGE_REQUEST_PROJECT_URL" "$CI_MERGE_REQUEST_TARGET_BRANCH_NAME"
  - git merge FETCH_HEAD
  - # Test on the merge commit
  only: [merge_requests]

Usage: Access API

#!/bin/bash
curl --header "Private-Token: yyM1RF_aWpf_RZNHAhMa" "http://192.168.10.15:3000/api/v4/projects/${CI_MERGE_REQUEST_PROJECT_ID}/pipelines"
curl --header "Private-Token: yyM1RF_aWpf_RZNHAhMa" "http://192.168.10.15:3000/api/v4/projects/${CI_MERGE_REQUEST_SOURCE_PROJECT_ID}/pipelines"

What are the relevant issue numbers?

Related: https://gitlab.com/gitlab-org/gitlab-ce/issues/15310

Does this MR meet the acceptance criteria?

Edited by Shinya Maeda

Merge request reports