Push to release branch based on tag created on master branch

Push to release branch based on tag created on master branch

Here is what I am trying to achieve

  • My repo is currently connected to Vercel for automated deployments
  • I have two branches master and release
  • The master branch is the current connected to a staging URL on vercel like staging-example.vercel.app
  • The release branch is connected to the production URL like example.vercel.app
  • Whenever I create a new release(via the GitLab release page) on the master branch with a tag, the pipeline pushes that code the release branch which in turn is picked up and deployed on the production instance.

The current pipeline as shown below is able to achieve this but I am not entirely sure if this is the correct or the better way to do. Any advise is highly welcome and most appreciated.

image: node:latest

before_script:
  - curl -o- -L https://yarnpkg.com/install.sh | bash
  - export PATH="$HOME/.yarn/bin:$HOME/.config/yarn/global/node_modules/.bin:$PATH"

stages:
  - build
  - test
  - release

cache:
  paths:
    - node_modules/
    - .yarn

build:
  stage: build
  script:
    - yarn install
  artifacts:
    expire_in: 1 day
    paths:
      - node_modules/

test:
  stage: test
  artifacts:
    expire_in: 1 day
  script: yarn test:ci

release:
  stage: release
  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 -
    - mkdir -p ~/.ssh
    - chmod 700 ~/.ssh
    - ssh-keyscan gitlab.com >> ~/.ssh/known_hosts
    - chmod 644 ~/.ssh/known_hosts
  artifacts:
    expire_in: 1 day
  rules:
    - if: '$CI_COMMIT_TAG =~ /^v((([0-9]+)\.([0-9]+)\.([0-9]+)(?:-([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?)(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?)$/'
  script:
    - git config user.email "myemail@gmail.com"
    - git config user.name "Soham Dasgupta"
    - git fetch origin
    - git checkout -B release origin/release
    - git merge origin/master
    - git remote set-url origin git@gitlab.com:mygroup/myrepo.git
    - git push origin HEAD