How to display values from variables

I’m trying to do a release stage using the releas-cli in a shell executer. for some reason my description and tag name are always empty.
my ref on the other hand is filled in. it is kind a strange as all three come from variables, and I have checked they are filled in.

Any idea what I’m doing wrong?

yaml (part):

script:
- echo feature deploy
- release_ver=$(cat version.txt)
- echo $release_ver
- echo $CI_COMMIT_TAG
release:
tag_name: ‘$release_ver’
description: ‘$release_ver’
ref: ‘$CI_COMMIT_SHA’

I have checked the documentation regarding release and gitlab ci/cd variables, but I don’t see my problem.

EDIT:

feature-deploy:
stage: deploy
tags:
- dev
variables:
release_ver: ‘’
script:
- echo feature deploy
- git fetch --tags
- VER=$(git tag --list | sort -V | tail -n1)
- git remote set-url origin http://tim:$GITLAB_TOKEN@192.168.0.68/tim/ci-cd-example.git
- dotnet /home/Tools/Semantic/Semantic.dll -m $CI_COMMIT_SHA -v $VER
- release_ver=$(cat version.txt)
- echo $release_ver
- echo $CI_COMMIT_TAG
release:
tag_name: ‘$release_ver’
description: ‘$release_ver’
ref: ‘$CI_COMMIT_SHA’
rules:
- if: $CI_COMMIT_TITLE =~ “/(Merge branch 'Feature).+/” && $CI_COMMIT_BRANCH == “main”

I think the variables are expanded before the script part is run. That’s why they are empty, and $release_ver is only set within the context of the script.

I have tried both local variable and a job variable but both didn’t work. currently release_ver is a job variable, so I created the variable empty and in the script it should set the tag, due to the echo i can see the variable is set with the correct value.

@tschoesi is correct.

The pipeline definition is generated BEFORE you define any value for release_ver

script:
- echo feature deploy
- release_ver=$(cat version.txt)   # this makes the variable set ONLY in `script`, nowhere else.
- echo $release_ver
- echo $CI_COMMIT_TAG
release:
tag_name: ‘’   # empty because at the time of pipeline definition generation such variable is not defined
description: ‘' # same as above
ref: ‘$CI_COMMIT_SHA’

If you want to use variables defined in script you need to use dotenv, there is an example here

see my edit, I have created the variable before the script, and I only set the value inside de script. does this still keep it just in the context of the script?

Yes. And exactly the value is empty. Pipeline config is generated before anything from script is executed. So GitLab doesn’t know you set some value later on.

ah oke thank you, let me check the example and see what I can try.

You could release via an API call, then you can first read in the text file… (edit the URL with your own Gitlab instance, if you don’t use the official one)

release_job:
  stage: release
  script:
    - release_ver=$(cat version.txt)
    - |
      curl --request POST \
        --url "https://gitlab.example.com/api/v4/projects/$CI_PROJECT_ID/releases" \
        --header "Private-Token: $GITLAB_TOKEN" \
        --form "name=Release $release_ver" \
        --form "tag_name=$release_ver" \
        --form "description=$release_ver" \
        --form "ref=$CI_COMMIT_SHA"
1 Like

Yeah, I should mention that you can also call release-cli directly from script. You do not have to use the release: keyword. GitLab actually calls the command itself and passes the arguments to it, you just define it more nicely using release:.

script:
- echo feature deploy
- release_ver=$(cat version.txt)   # this makes the variable set ONLY in `script`, nowhere else.
- echo $release_ver
- echo $CI_COMMIT_TAG
- release-cli --server-url https://gitlab.com --job-token=$CI_JOB_TOKEN --project-id $CI_PROJECT_ID create ...

you can find the usage here docs/index.md · master · GitLab.org / release-cli · GitLab

1 Like

That’s too much information at one hand lol. the example is a little bit different as the dotenv works trough multiple jobs trough artifacts, which is logic, I’m indeed using the release-cli with the shell executer, but as you mention in the answer, there are indeed a lot of possiblities, I alread use a project access token to push the newly created tag.

the release-cli in the script worked, thanks alot, very usefull information.