4

Is it possible to add and push a commit tag using the GitLab CI/CD configuration file?

In my use case I have a release stage that uploads a python package and it's associated documentation, and then only after those succeed I would like to tag the commit with the version number.

SLesslyTall
  • 261
  • 1
  • 3
  • 8
  • inlcuding the version tag with your code does not scale. Moreover, modifying a repo by pushing a tag from within a pipeline is a sideeffect that should be avoided. is not how to create releases in gitlab. Rather see https://stackoverflow.com/questions/29520905/how-to-create-releases-in-gitlab – Felix K. Feb 10 '20 at 11:19

2 Answers2

4

Just add a job using your release stage in your .gitlab-ci.yml with these lines :

tag_commit:   
  stage: release   
  script:
    - git tag -a v1.0 <COMMIT_ID> -m "Message here"
    - git push origin v1.0
Nicolas Pepinster
  • 5,413
  • 2
  • 30
  • 48
  • 1
    tried that but got $ git push --force-with-lease origin "$RELEASE_VERSION" "HEAD:$CI_COMMIT_BRANCH" remote: You are not allowed to upload code. fatal: unable to access 'https://gitlab.xxx.de/xxx/xxx.git/': The requested URL returned error: 403 – wutzebaer May 02 '23 at 21:37
2

GitLab exposes REST API for tagging. Example:

tag_it:
  stage: release
  script:
    - curl --request POST --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/projects/5/repository/tags?tag_name=test&ref=master"

See more info in https://docs.gitlab.com/ee/api/tags.html#create-a-new-tag.

Kristijonas
  • 151
  • 1
  • 10