Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get latest git tags on Gitlab CI/CD pipeline

I want to set up a CI/CD pipelines in Gitlab that can read the latest tag and get that last tag to increment my next version application. I came with this configuration:

stages:
  - version

calculate_version:
  image:
    name: alpine/git:latest
    entrypoint: [""]
  stage: version
  script:
    - VERSION=$(git tag);test -z "$VERSION" && echo "no version tag found" && exit 1
    - CMDLINE="$VERSION";
    - echo $VERSION
    - echo $CMDLINE > cmdline
  artifacts:
    paths:
      - cmdline

But i get no tags listed there on $VERSION. Its look like Gitlab not passing the tags on the repository. However if I create and push a new tag, it shows just that new tag, not all tag list I expected.

Is this the behaviour of the GitLab ci/cd? If yes, how can I get all of the tags in my repo inside the pipeline?

like image 981
dennbagas Avatar asked Jan 28 '20 11:01

dennbagas


People also ask

How do I get the latest tag from github?

In order to find the latest Git tag available on your repository, you have to use the “git describe” command with the “–tags” option. This way, you will be presented with the tag that is associated with the latest commit of your current checked out branch.

What are tags in GitLab CI?

In Git, within your repository, tags are used to mark a specific commit. It is often used to tag a version. The two concepts can be mixed up when you use tags (in Git) to start your pipeline in GitLab CI. In your . gitlab-ci.

What are tags used for in GitLab?

Tags are ref's that point to specific points in Git history. Tagging is generally used to capture a point in history that is used for a marked version release (i.e. v1. 0.1). A tag is like a branch that doesn't change.

Where are artifacts in GitLab?

The artifacts are stored by default in /home/git/gitlab/shared/artifacts . Save the file and restart GitLab for the changes to take effect.

How do I run a Git pipeline in GitLab?

In the Run for branch name or tag field, select the branch or tag to run the pipeline for. Enter any environment variables required for the pipeline to run. You can set specific variables to have their values prefilled in the form . Select Run pipeline . The pipeline now executes the jobs as configured. Introduced in GitLab 13.7.

Is the GitLab CI/CD pipeline free to use?

The GitLab CI/CD pipeline is free to use for a limited amount of minutes/month. The project is a pretty simple web application. It does not use server-side logic or database layer, just pure frontend code.

How do I push code from CICD pipeline to GitLab?

This process involves some shuffling with sensitive files so be mindful where you store these. We are going to generate a private SSH key (+public key), upload that pair into gitlab and use it from within our pipeline to have a trust between the cicd pipeline to be able to push code.

How to tag and release versions on GitLab CI using release-it?

Using release-it to tag and release versions on GitLab CI is quite straightforward when using the detached HEAD approach but if you want to do GitLab release for the version you need to set the release step separately and have a changelog generation set up in package.json. Like below:


Video Answer


3 Answers

You can obtain tags using Gitlab API

By default, results are ordered by the last updated tags, so if you want to get the last one, you can modify your script block like this:

script:
  - VERSION=$(curl -Ss --request GET --header "PRIVATE-TOKEN: <REPLACE_BY_A_VARIABLE>" "https://gitlab.com/api/v4/projects/${CI_PROJECT_ID}/repository/tags" | jq -r '.[0] | .name')
  - test -z "$VERSION" && echo "no version tag found" && exit 1
like image 56
Nicolas Pepinster Avatar answered Oct 17 '22 07:10

Nicolas Pepinster


You can do this very easily without long script code.

script:
  - git fetch --tags
  - git tag --list | sort -V | tail -n1

assign in variable ${VER} with your version:

script:
  - VER=$(git tag --list | sort -V | tail -n1)
like image 43
Mohammad Raoufi Avatar answered Oct 17 '22 07:10

Mohammad Raoufi


Without GitLab API, you can just use:

script: 
  git describe --abbrev=0 2>/dev/null || echo ''

assign in variable to compare with your version:

script: 
  last_v=$(git describe --abbrev=0 2>/dev/null || echo '')
  

Return last tag or empty string if no tag found (using 2>/dev/null to hide error code)

⚠️ Warning:

After test, leaving Tag message blank creates a lightweight tag, and command above will not work.

like image 2
GuilleW Avatar answered Oct 17 '22 08:10

GuilleW