Otto
Otto

Reputation: 4190

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? .

Upvotes: 20

Views: 38460

Answers (3)

Michael Lihs
Michael Lihs

Reputation: 8220

The pre-defined variable $CI_COMMIT_TAG is empty if no git tag points to the current commit in the pipeline. If you either want the current tag or the current SHA of the commit (as a fallback), you can use IMAGE_VERSION=${CI_COMMIT_TAG:-"$CI_COMMIT_SHORT_SHA"}

Upvotes: 1

renefritze
renefritze

Reputation: 2223

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

Upvotes: 22

aknosis
aknosis

Reputation: 4308

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

Upvotes: 17

Related Questions