0

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 ?
3
  • Does this answer your question? How to list all tags pointing to a specific commit in git Commented Sep 12, 2022 at 21:26
  • Hi thanks for the reply. Actually i get the one tag from predefined variable, as i show on my script: CI_COMMIT_TAG` and i miss the version tag. I manage to get the version tag if i run the above command on my terminal like so: git log --decorate -n 1 --oneline | cut -d: -f2 | cut -d, -f1 , so far so good. Now the issue is that this git command returns nothing, inside the gitlab-ci.yml. To answer your question, those answers are not as i need because i need to get just the version tag and not all the tags of the commit. Commented Sep 13, 2022 at 9:24
  • ok i found the issue for ignoring git. we need to install git, in the gitlab-ci.yml - apk add --no-cache curl jq python3 py-pip git - pip install awscli botocore==1.26.1 All good now. :) Commented Sep 13, 2022 at 10:33

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.