I'm trying to have my GitLab pipeline automatically tag the master
branch but with no luck.
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.
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
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.
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
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.