Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 641
Remotec Avatar asked Oct 18 '17 13:10

Remotec


2 Answers

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

like image 153
Stefan van Gastel Avatar answered Oct 11 '22 03:10

Stefan van Gastel


Regarding timestamp, another approach is to use existing variables associated to your current pipeline.

See GitLab 13.10 (March 2021)

Predefined CI/CD variables for job start and pipeline created timestamps

Previously, if you wanted to reference the exact date and time when a job started or a pipeline was created, you needed to retrieve these timestamps in your scripts. Now they are readily available as predefined CI/CD variables by using CI_JOB_STARTED_AT and CI_PIPELINE_CREATED_AT, provided in the ISO 8601 format and UTC time zone.

Thanks to @Winkies for this contribution!

See Documentation and Issue.

Unfortunately this variable can't be used directly as an image tag. As seen in the referenced implementation issue, the output of this variable is similar to 2021-03-18T04:45:29Z. Used directly, your image would look something like myimage:2021-03-18T04:45:29Z which is invalid.

like image 44
VonC Avatar answered Oct 11 '22 02:10

VonC