harsh
harsh

Reputation: 43

Tagging a Docker image with auto incremented global version in Gitlab in yaml

I'm currently digging in Gitlab CI. I would like to add a way in my YAML files to tag my docker images with a version number composed in the following fashion : MajorVersion.Minorversion.AutoincrementedGlobalversionNumber

I would like to auto-increment the globally defined variable "AutoincrementedGlobalversionNumber" each time I deploy.

I have used CI_PIPELINE_IID however it keeps incrementing for each pipeline request, I need something to keep a version where I can keep track of and it should increment only when I pack and deploy.

   variables:
      CI_VERSION: "1.0.${CI_PIPELINE_IID}"

    build-master:
      stage: build
      script:
        - docker build --pull -t "$CI_REGISTRY_IMAGE" -t "$CI_REGISTRY_IMAGE:$CI_VERSION"   ./postfix
        - docker push "$CI_REGISTRY_IMAGE"
      only:
        - master

Upvotes: 1

Views: 4494

Answers (1)

Rekovni
Rekovni

Reputation: 7374

You probably can't do this with the default GitLab CI variables, but there could be a workaround along the lines of (untested):

  1. Get the registry ID with something like:

    $ registry_id=$(curl -s -XGET --header "PRIVATE-TOKEN: $TOKEN" "https://gitlab.com/$PROJECT_PATH/container_registry.json" | jq '.[].id')

  2. Query said registry to get the name:

    curl -s -XGET --header "PRIVATE-TOKEN: $TOKEN" "https://gitlab.com/$PROJECT_PATH/registry/repository/$registry_id/tags?format=json" | jq

    eg returns the following and you can grep the name for GlobalVersionNumber:

    [
      {
        "name": "latest",
        "location": "registry.gitlab.com/mwasilewski/helm:latest",
        "revision": "85a403337a56e9e6409dfb8185bf9aa5c2135f9a437bd75da82d27471c71feb4",
        "short_revision": "85a403337",
        "total_size": 152246865,
        "created_at": "2016-12-11T08:31:30.126+00:00",
        "destroy_path": "/mwasilewski/helm/registry/repository/31074/tags/latest"
      }
    ]
    
  3. Continue with your Docker build and push, after incrementing the GlobalVersionNumber you get back.

NB: this assumes you are using GitLab's Container Registry

Resources:

Upvotes: 1

Related Questions