简体   繁体   中英

Gitlab CI automatically rebase master branch when review branch merged

We use a peer-review process where reviewers are asked to merge in to a Review branch when done.

Thing is, after review, we wish that our master branch gets automatically rebased with what Review branch has. This would reflect our current manual process where repo maintainer manually rebases review branch in to master to make a deploy.

How can we achieve this automatization?

This can be achieved through a gitlab-ci pipeline task. Essentially you will need to merge to master and push. Unfortunately there is no direct way for a gitlab runner to push to remote.

The below is a workaround

  • Setup ssh key in a before_script
  • Make the git updates locally
  • Push to remote

Sample code below

merge_to_master:
  before_script:
  - which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )
  - eval `ssh-agent -s`
  - echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add - > /dev/null
  - mkdir -p ~/.ssh
  - chmod 700 ~/.ssh  
  - ssh-keyscan -H <Your gitlab server> >> ~/.ssh/known_hosts
  - ssh -vT git@<Your gitlab server>
  variables:
   VERSION: "$CI_PIPELINE_ID"
   VERSIONNAME: "$CI_COMMIT_REF_SLUG"
  only:
   - <Review* or similar>
  script:    
    - export LC_CTYPE=en_US.UTF-8
    - git config --global user.email "Some user email - Typically the machine user"
    - git config --global user.name "Some name"
    - git checkout master
    - git merge $CI_COMMIT_REF_NAME
    - git push ssh://git@<Your gitlab server>/<Repo> HEAD:master
  stage: dev_deploy

$SSH_PRIVATE_KEY - Environment variable containing the private SSH key for git Refer https://help.github.com/en/github/authenticating-to-github/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent

[ci skip] - Add only if you want to skip the build after the push

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM