What is the standard way of version bumping from gitlab CI? A few questions

In our workflow we need to:

  1. bump version of an npm package whenever it’s a merge into master/develop.
  2. deploy it whenever tag is created.

I believe this workflow is quite regular but the best solution we’ve found so far looks really ugly.
I wonder is there a better way to do the same?

So, the bump version job:

bump_version:
  stage: version
  image: gitlab.company.com:5000/tsanalyzer/frontend/node-ssh-agent:latest
  tags:
    - docker
    - kvm
  before_script:
    - eval $(ssh-agent -s)
    - echo "$SEMVER_PRIVATE_KEY" | tr -d '\r' | ssh-add -
    - mkdir -p ~/.ssh
    - chmod 700 ~/.ssh
    - echo -e "Host *\n\tStrictHostKeyChecking no\n\n" >> ~/.ssh/config
  script:
    - echo "CI_REPOSITORY_URL=$CI_REPOSITORY_URL"
    - 'REPOSITORY_PUSH_URL=$(echo "$CI_REPOSITORY_URL" | sed -r "s#.+(@[^/]+)/#git\1:#")'
    - echo "REPOSITORY_PUSH_URL=$REPOSITORY_PUSH_URL"
    - git remote set-url origin $REPOSITORY_PUSH_URL
    - git checkout $CI_COMMIT_BRANCH
    - git config --global user.name "${GITLAB_USER_NAME}"
    - git config --global user.email "${GITLAB_USER_EMAIL}"
    - git pull
    - CURRENT_HEAD_TAG=$(git describe --tags --exact-match $(git rev-parse HEAD) || true)
    - 'echo "Pipeline source: $CI_PIPELINE_SOURCE"'
    - >
      if [ -n "$CURRENT_HEAD_TAG" ]; then
        exit 0;
      fi
    - >
      if [ "$CI_PIPELINE_SOURCE" = "push" -a -n "$CURRENT_HEAD_TAG" ]; then
          echo "Current $CI_COMMIT_BRANCH branch already tagged with \"$CURRENT_HEAD_TAG\", skipping";
          exit 0;
      fi
    - >
      case "$SL_RELEASE_STRATEGY" in
        alternative)
          case "$CI_COMMIT_BRANCH" in
            develop) npm run release ;;
                  *) npm run release -- --prerelease "$CI_COMMIT_REF_SLUG" ;;
          esac
          ;;
        standard | *)
          case "$CI_COMMIT_BRANCH" in
            master) npm run release ;;
            develop) npm run release -- prerelease dev;;
                  *) echo "Incorrect branch" ;;
          esac
          ;;
      esac
    - git push --follow-tags origin "$CI_COMMIT_BRANCH"
  variables:
    GIT_STRATEGY: clone
  only:
    - master
    - develop
  except:
    - tags

As you see, it’s bloody huge, verbose, depends on some custom docker image (node+ssh), requires a fake user account, and what is worse - it needs to check isn’t it a push/mr with the same tag:

- >
      if [ -n "$CURRENT_HEAD_TAG" ]; then
        exit 0;
      fi

and that despite we said:

  except:
    - tags

Do you folks have any ideas about making all this a little bit less ugly or it is the best of one can achieve with GitLab CI for such a workflow?

We bump using a combination of the branch release number and the CI_PIPELINE_IID. Instead of deploying from the tag, we both create the tag and deploy from the main/develop/master branch. It simplifies lots of things and reduces the number of builds/deployments by a factor of 2.

Create a tag inside .gitlab-ci.yml:

script:
  - git tag ${CI_BUILD_REF_NAME/release\//}.$CI_PIPELINE_IID
  - git push origin ${CI_BUILD_REF_NAME/release\//}.$CI_PIPELINE_IID

Thanks @wparad ! Could you please provide a code snippet?

I updated the previous comment with an example script to run