Gitlab CI auto-tagging after merge

Viewed 1467

I want to add a stage in the gitlab ci that auto tags the latest merge as "latest", deleting/overwriting the tag if it already exists.. but I'm kind of stuck

I tried to just write it in the gitlab-ci.yml file but got a permission error so I moved it to a script file but I'm not sure how to implement the stage..

tag.sh:

SCRIPT_DIR=$(cd $(dirname $0); pwd)
GIT_MERGE_AUTOEDIT=no

# include common utilities
test -f $SCRIPT_DIR/functions && . $SCRIPT_DIR/functions

# 1. setup git
git_init $CI_PROJECT_PATH $GITLAB_USER_EMAIL

# 2. tag as "latest"
(git tag -d latest && git push origin --delete latest) || git tag latest
git push origin --tags

(git_init function):

git_init () {
  local project_path=$1
  local user_email=$2

  git remote set-url --push origin ssh://git@<cdn>/$project_path.git
  git config user.email "$user_email"
  git remote -v
}

gitlab-ci.yml:

stages:
  - lint
  - sa
  - test
  - tag
  - build
  - deploy
  - release
...
tag:
  stage: tag
  script:
    - scripts/tag.sh

The pipeline succeeds but I don't see a new tag.. and it's also running when the merge request is testing. Is there a way to set a stage to only run after the MR is merged?

I've seen the docs for when: merge_request but that doesn't seem to solve only on merged MR's..

The functions I've mostly copied from our team's other projects and the inner workings of the ci is somewhat confusing to me.

0 Answers
Related