Scenario:

You want to add a tag to an older commit in the GitLab repo. This needs to be done using the Gitlab pipeline.

 

Solution:

Pre-requisite-

–> should know the tag to be added

–> should know the commit_sha hash of the commit

–> should know the access token name for the repo

 

Steps:

You can create a GitLab pipeline job as shown below to add a tag to an older commit.

tag_gitlab:
  stage: add_tag_to_old_commit
  image: "registry.example.com/my/image:latest"
  services:
    - docker:dind
  variables:
    TAG: "v1.4.7"
  before_script:
   - git remote remove origin
   - git remote add origin https://<access-token-name>:$GITLAB_TOKEN@gitlab.xe.digital/${CI_PROJECT_PATH}.git
  script:
    - echo "Tag the old commit"
    - echo $TAG
    - git config --global user.email "${GITLAB_USER_EMAIL}"
    - git config --global user.name "${GITLAB_USER_NAME}"
    # CI_COMMIT_SHA is for some old commit
    - git tag -a $TAG $CI_COMMIT_SHA -m"Retroactively tagging version $TAG"
    - git push origin $TAG

 

Note: 

You should replace a value for “access-token-name”, the CICD variable name GIT_TOKEN,  and the CI_COMMIT_SHA value.

– Create the access token for the repo as shown in the screenshot and get the token name-

 

– Create variable in CICD- GIT_TOKEN, and assign the value to it.