Gitlab CI Tag Version

Art Krisada
2 min readNov 29, 2023

Note on tag version after build completed on Gitlab CI.

First, Generate your token. Open Preference > Access Tokens

Then, Add new token.

Fill in Token name, Expiration date as you like and Select scope. then Create personal access tokens. Record your token to use it later.

Next, Go to your project to set variable.

From Setting > CI/CD Expand Variables

Add variable.

Fill in Flags, Key and Value. Value is your token that yyou just created.

Back to your project repository, edit .gitlab-ci.yml

image: docker:latest
stages:
- build
build:
stage: build
services:
- name: docker:24-dind
only:
- main
before_script:
- project_url=$(echo $CI_PROJECT_URL | sed 's/https:\/\///')
- apk add git
- git remote set-url origin https://oauth2:$GITLAB_TOKEN@$project_url
script:
- tag=v0.0.$CI_PIPELINE_IID
- echo "Start build..."$tag
- echo "${CI_REGISTRY_PASSWORD}" | docker login ${CI_REGISTRY} -u ${CI_REGISTRY_USER} --password-stdin
- docker build --build-arg MODE=prod -t ${CI_REGISTRY_IMAGE}:${tag} .
- docker push ${CI_REGISTRY_IMAGE}:${tag}
- echo "End Push docker."
- git tag $tag
- git push origin $tag
- echo "End Git tag."
after_script:
- 'echo "End Build."'

In before_script step, set login to git with GITLAB_TOKEN.

You can tag and push tag after that.

--

--

Art Krisada

Never stop learning, because life never stop teaching.