简体   繁体   中英

Gitlab CI/CD auto tagging release

I'm trying to have my GitLab pipeline automatically tag the master branch but with no luck.

What I want to do

Since the project is a composer package, what I want to do is get the version number from the composer.json file, store it in a variable, and then use this variable with git to tag the branch.

What I'm doing

Here is the pipeline job part from my .gitlab-ci.yml :

tagging:
  stage: publish
  image: alpine
  only:
    - master
  script:
    - version=$(cat composer.json | grep version | grep -Eo "[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+")
    - git tag "$(version)"
    - git push --tags

The error

I can't actually tell what the problem is since no output is displayed in the GitLab job output as show in the picture below

在此处输入图像描述

Ok, didn't know exactly why this didn't work, but i found out i wrote Version (with the capital V) in the first grep command instead of version : this shouldn't be the cause of the problem since in local the same command pipeline return a 0 but not an error.

I prefer to not install additional cli commands on the pipeline job image as @davide-madrisan suggested since I wanted to keep it as simple as possible.

Tips and tricks

Moreover i found this interesting gitlab repo with exactly what I needed:
https://gitlab.com/guided-explorations/gitlab-ci-yml-tips-tricks-and-hacks/commit-to-repos-during-ci/commit-to-repos-during-ci

The result

So in the end I came up with this pipeline job:

tagging:
  stage: publish
  only:
    - master
  script:
    - git config --global user.name "${GITLAB_USER_NAME}"
    - git config --global user.email "${GITLAB_USER_EMAIL}"
    - tag=$(cat composer.json | grep version | grep -Eo "[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+")
    - git tag "$tag"
    - git push --tags http://root:$ACCESS_TOKEN@$CI_SERVER_HOST/$CI_PROJECT_PATH.git HEAD:master

I just needed to create a personal access token and add tree pipeline variables with the git creadentials to create the tag and push it to the master branch from within the pipeline, but it actually work now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM