Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get latest tag in .gitlab-ci.yml for Docker build

I want to add a tag when building a Docker image, I'm doing this so far but I do not know how to get the latest tag on the repository being deployed.

docker build -t company/app .

My goal docker build -t company/app:$LATEST_TAG_IN_REPO? .

like image 424
Otto Avatar asked Jun 13 '19 16:06

Otto


People also ask

What is the use of tags in GitLab CI Yml?

gitlab-ci. yml , you can specify some jobs with the tag testing . If a runner with this tag associated is available, it will pickup the job. In Git, within your repository, tags are used to mark a specific commit.

What is Ci_commit_tag?

CI_COMMIT_TAG - The commit tag name. Present only when building tags. Therefore in the variables section CI_COMMIT_TAG is not defined, hence equals to "". So if you want to use CI_COMMIT_TAG use in job where tags are defined. See https://docs.gitlab.com/ee/ci/yaml/README.html#tags.

Where can I find GitLab CI Yml file?

From version 7.12, GitLab CI uses a YAML file ( . gitlab-ci. yml ) for the project configuration. It is placed in the root of your repository and contains definitions of how your project should be built.

What is Docker DIND service?

Ultimately Codefresh is a DevOps platform designed for containers and Kubernetes. The backbone of all of that is the Codefresh pipelines which provide Docker in Docker as a service.


2 Answers

Since you're looking for the "latest" git tag which is an ancestor of the currently building commit you probably want to use

git describe --tags --abbrev=0

to get it and use it like:

docker build -t company/app:$(git describe --tags --abbrev=0) .

Read here for the finer points on git describe

like image 161
renefritze Avatar answered Sep 17 '22 15:09

renefritze


You can try using $CI_COMMIT_TAG or $CI_COMMIT_REF_NAME, this is part of the predefined variables accessible during builds.

If you want to see what are all the available environment variables during build step this should work as one of your jobs:

script:
    - env
like image 27
aknosis Avatar answered Sep 20 '22 15:09

aknosis