Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a merge request at the end of a successful pipeline in Gitlab?

I'm very new to gitlab and gitlab CI and I have put a pipeline in place that is successfully completing. My master and development branches are protected so a merge request is required so that another dev in the group can review the code and comment before merging. I was wondering if it is possible to generate this merge request at the end of this pipeline. Is there a setting for this in the gitlab repository or do I have to create a script to achieve this?
Side note :
Just before posting this I came across this section of the gitlab docs
I'm using gitlab-runner 11.0.0 on ubuntu 18.04

like image 792
Dark Star1 Avatar asked Jun 29 '18 15:06

Dark Star1


People also ask

What is merge when pipeline succeeds?

If the pipeline succeeds, the merge request is merged. If the pipeline fails, the author can either retry any failed jobs, or push new commits to fix the failure: If a retried job succeeds on the second try, the merge request is merged.

How do I create a merge request pipeline?

Create a new merge request from a source branch with one or more commits. Push a new commit to the source branch for a merge request. Select Run pipeline from the Pipelines tab in a merge request.

How do you create merge request for specific commit in GitLab?

You can, however, create a new branch from your master branch, cherry-pick the single commit, and create a merge request for that branch, containing only the one commit. If you do not need the other commits any more, you can also consider an interactive rebase, to remove the unwanted commits from the branch.


1 Answers

In order to achieve my simple needs, I simply added a final stage to my pipeline which essentially executes a bash script adapted from this post.

EDIT: As requested by @Yuva

# Create a pull request on pipeline success
create_merge_request:
  stage: createMR
  tags:
    - autoMR
  script:
    - 'echo Merge request opened by $GITLAB_USER_NAME '
    - ~/commit.sh

and in commit.sh

#!/bin/bash
# This script was adapted from:
# https://about.gitlab.com/2017/09/05/how-to-automatically-create-a-new-mr-on-gitlab-with-gitlab-ci/

# TODO determine URL from git repository URL
[[ $HOST =~ ^https?://[^/]+ ]] && HOST="${BASH_REMATCH[0]}/api/v4/projects/"

# The branch which we wish to merge into
TARGET_BRANCH=develop;

# The user's token name so that we can open the merge request as the user
TOKEN_NAME=`echo ${GITLAB_USER_LOGIN}_COMMIT_TOKEN | tr "[a-z]" "[A-Z]"`

# See: http://www.tldp.org/LDP/abs/html/parameter-substitution.html search ${!varprefix*}, ${!varprefix@} section
PRIVATE_TOKEN=`echo ${!TOKEN_NAME}`

# The description of our new MR, we want to remove the branch after the MR has
# been closed
BODY="{
\"project_id\": ${CI_PROJECT_ID},
\"source_branch\": \"${CI_COMMIT_REF_NAME}\",
\"target_branch\": \"${TARGET_BRANCH}\",
\"remove_source_branch\": false,
\"force_remove_source_branch\": false,
\"allow_collaboration\": true,
\"subscribed\" : true,
\"title\": \"${GITLAB_USER_NAME} merge request for: ${CI_COMMIT_REF_SLUG}\"
}";

# Require a list of all the merge request and take a look if there is already
# one with the same source branch
 LISTMR=`curl --silent "${HOST}${CI_PROJECT_ID}/merge_requests?state=opened" --header "PRIVATE-TOKEN:${PRIVATE_TOKEN}"`;
 COUNTBRANCHES=`echo ${LISTMR} | grep -o "\"source_branch\":\"${CI_COMMIT_REF_NAME}\"" | wc -l`;

# No MR found, let's create a new one
if [ ${COUNTBRANCHES} -eq "0" ]; then
    curl -X POST "${HOST}${CI_PROJECT_ID}/merge_requests" \
    --header "PRIVATE-TOKEN:${PRIVATE_TOKEN}" \
    --header "Content-Type: application/json" \
    --data "${BODY}";

    echo "Opened a new merge request: WIP: ${CI_COMMIT_REF_SLUG} for user ${GITLAB_USER_LOGIN}";
    exit;
fi
    echo "No new merge request opened"

like image 60
Dark Star1 Avatar answered Oct 16 '22 04:10

Dark Star1