tmegos blog

フルリモートで働きたい

GitLab CIで自動マージを実現する

Motivation

  • 作業ブランチが常にmasterを追従していてほしい

.gitlab-ci.yml

stages:
  - merge

.merge: &merge
  image: bravissimolabs/alpine-git
  stage: merge
  before_script:
    - git config --global core.autocrlf false
  script:
    - git checkout ${TARGET_BRANCH}
    - git config user.name ${GIT_USER}
    - git config user.email ${GIT_USER_EMAIL}
    - git remote add gitlab http://gitlab/hoge/fuga.git
    - echo "machine gitlab" > ~/.netrc
    - echo "login ${GIT_USER}" >> ~/.netrc
    - echo "password ${GIT_USER_PASSWORD}" >> ~/.netrc
    - RES=$(git merge --no-commit --no-ff origin/${SOURCE_BRANCH})
    - |-
      if [[ "$?" = "1" ]]; then
        exit 1
      fi
    - |-
      if [[ "${RES}" =~ ^"Already up-to-date". ]]; then
        exit 0
      fi
    - git commit -m "Merge branch ${SOURCE_BRANCH} into ${TARGET_BRANCH}"
    - git push gitlab ${TARGET_BRANCH}
  tags:
    - docker

auto-merge:
  <<: *merge
  only:
    - master
  variables:
    GIT_STRATEGY: clone
    GIT_CHECKOUT: "false"
    SOURCE_BRANCH: master
    TARGET_BRANCH: develop