7

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

enter image description here

4
  • You can cat composer.json before for debugging. I think the grep command fails because the file or the patters is not found. Commented Oct 9, 2021 at 17:11
  • And maybe adding a set -xv command before may help debugging. Commented Oct 9, 2021 at 17:15
  • An finally what about using jq instead of grep for parsing JSON files? Commented Oct 9, 2021 at 17:20
  • I think the main reason is that you need to authenticate yourself before you can do anything with git, which is clarified in the answer :)
    – Kerem
    Commented Feb 1, 2023 at 16:11

1 Answer 1

10

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.

4
  • This created a master branch on my repo. I removed the HEAD:master and it worked as I expected. Commented Jun 22, 2022 at 21:57
  • 4
    You could have done this with a deploy token with write privileges.
    – Torrien
    Commented Mar 17, 2023 at 22:23
  • 1
    To expand on @Torrien 's answer, specifically the CI_JOB_TOKEN variable: docs.gitlab.com/ee/ci/variables/predefined_variables.html
    – mars64
    Commented Apr 8 at 21:38
  • It can be useful to specify that "root" in yor "git push" command is a token name. Took me awhile to realise that lol Commented May 18 at 9:22

Not the answer you're looking for? Browse other questions tagged or ask your own question.