DEV Community

loading...
Cover image for Gitlab Ancestor Check Branches

Gitlab Ancestor Check Branches

Vu Dao
Awesome Devops || AWS SA || CloudOpz
・1 min read

How to ensure the child branches are always up-to-date with their parent one

Alt Text

  • From the example above, when feature branch is merged to develop branch then dev branch need to be updated to get latest code from develop so it will get no conflict or perform test with integration code when it's merged back to develop. Same behavior for integrate branch

  • Here is the .pre job in gitlab pipeline to ensure this

stages:
  - build
  - deploy

ancestor_develop:
  stage: .pre
  script:
    - git merge-base --is-ancestor origin/develop HEAD || ( echo "Branch is not an ancestor of develop"; /bin/false )
  only:
    refs:
      - dev
      - feature
      - integrate
  tags:
    - ancestor-check

compile_code:
  stage: build
  script:
    - echo build image
  only:
    refs:
      - dev
      - feature
      - integrate
  tags:
    - runner-build

deploy_test:
  stage: deploy
  script:
    - echo Deploy to test env
  only:
    refs:
      - dev
      - feature
      - integrate
  tags:
    - runner-deploy
Enter fullscreen mode Exit fullscreen mode
  • .pre is highest priority stage in gitlab pipeline, it must be passed in order to continue other stages (build and deploy)
  • Ref: git-merge-base(1) Manual Page

--is-ancestor

Check if the first is an ancestor of the second , and exit with status 0 if true, or with status 1 if not. Errors are signaled by a non-zero status that is not 1.

Discussion (0)