Search code examples
gitlabgitlab-cigitlab-ci-runnersemantic-versioning

Use semver or a timestamp in gitlab piplelines


I'm using GitLab pipelines and have defined my build defintion in the .gitlab-ci.yml file.

I'm using this to build docker containers.

Simple question. Is there a way I can tag my docker containers with either a semver from gitlab or a timestamp.

The build-in variables don't seem to give me much to work with.

On Windows I've been able to use GitVersion before in powershell that gets the semver tag and puts it into a variable you can use in the rest of the build process.

If I can't do that, is it possible to get a timestamp from the OS and use that in the yml file?


Solution

  • You can use the timestamp in your .gitlab-ci.yml like this (taken from our own usage creating <year>.<month> tags and releases:

    job-1:
     script:
       - export VERSION=$(date +%y.%m)
       - docker build -t myregistry/project/image:$VERSION
    

    This results in a image tag like: myregistry/project/image:17.10

    You can use date +%s instead of date +%y.%m for unixtimestamp.

    Depending on your (git)flow you can also use branch-slugs provided by Gitlab CI env vars