How to get the commit TAGS, via git and not from predefined vars, when gitlab CI/CD triggers

Viewed 61

Update:

I solved that and i added my solution, on the bottom, for reference.

Update:

After searching and digging i found a git script that gives the version tag:
git log --decorate -n 1 --oneline | cut -d: -f2 | cut -d, -f.
So that command, local on my the terminal, gives just the version tag : v1.0.7

But when this git is executed inside the gitlab-ci.yml file, it returns always nothing/empty.

gitlab-ci.yml:

      ...
      export USER_TAG=$(git rev-parse HEAD)
      export STAGE_TAG=$(git log --decorate -n 1 --oneline $USER_TAG | cut -d: -f2 | cut -d, -f1 | xargs)
      echo "*ver tag: $STAGE_TAG"

It is like the git command is ignored, i dont know why this is happening, i need to search again.


I m creating my gitlab-ci.yml file so that the Ci/CD pipeline starts when i push tags.

I need to make the pipeline start on specific combination of 2 TAGS, a version e.g. v1.0.0 and DEV_TEST and not on just one TAG.

My commit that triggers the pipeline is like so (<DEV_TEST><v1.0.7>): enter image description here

Into my gitlab-ci.yml i have a script that takes the $CI_COMMIT_TAG, but it just takes the DEV_TEST tag and ignores the version tag:

build:dev:
  stage: build:dev
  only:
    refs:
      - /^DEV_TEST/
  script: |
    set -e
    echo
    echo
    echo "################################################################################"
    echo "# Building Docker Base Image"
    echo "################################################################################"
    echo
    echo

    # ---- build image from branch ---- #
    if [ "$CI_COMMIT_TAG" != "" ]; 
    # ---- Build image from tag ---- # 
      then echo "building from tag branch $CI_COMMIT_TAG"
      docker build -t $CONTAINER_TEST_IMAGE -f Dockerfile .
      docker tag $CI_REGISTRY_IMAGE $CI_REGISTRY_IMAGE:$CI_COMMIT_TAG
      docker push $CI_REGISTRY_IMAGE:$CI_COMMIT_TAG

      export TAG_VERSION=$(echo $CI_COMMIT_TAG | cut -d '-' -f1)

    fi 

What i do is:

  • push a version tag first, e.g. v1.0.0
  • push the tag DEV_TEST, and based on the rule, the pipeline starts
  • but this line: then echo "building from tag branch $CI_COMMIT_TAG" gives the DEV_TEST tag only. So i m wondering is there a way to get both tags ?
1 Answers

I managed to get the version tag from the commit, like so:

in gitlab-ci.yml :

export USER_TAG=$(git rev-parse HEAD)
export VERSION_TAG=$(git log --decorate -n 1 --oneline $USER_TAG | cut -d: -f2 | cut -d, -f1 | xargs)

So, in a commit with a version tag e.g. v1.0.10 and another tag e.g. DEV_TEST, it will return the v1.0.10 tag.

One challenge that exists here, is that the git should be installed into yml file, in order to run the commands.

So if it is not, and the commands cannot executed, we install git like so:

A good place to put it is on the before script.

before_script:
  - docker login -u gitlab-ci-token -p $CI_JOB_TOKEN registry.gitlab.com
  - apk add --no-cache curl jq python3 py-pip git
  - pip install awscli botocore==1.26.1

Now all are in place, and the git log ... will be executed and return correct values.

I leave it here for reference, in case someone else wants to get the same values. (tag version from commit).

Related