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?
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.
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.
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.
The artifacts are stored by default in /home/git/gitlab/shared/artifacts . Save the file and restart GitLab for the changes to take effect.
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.
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.
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.
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:
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
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)
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)
After test, leaving Tag message blank creates a lightweight tag, and command above will not work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With