Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use conditions in .gitlab-ci.yml variables?

I want to know, if it's possible to set custom Gitlab CI variable from if-else condition statement.

In my .gitlab-ci.yml file I have the following:

variables:
    PROJECT_VERSION: (if [ "${CI_COMMIT_TAG}" == "" ]; then "${CI_COMMIT_REF_NAME}-${CI_PIPELINE_ID}"; else ${CI_COMMIT_TAG}; fi);

Trying to set project version:
    image: php:7.1-cli
    stage: test
    script:
        # this echoes correct string (eg. "master-2794")
        - (if [ "${CI_COMMIT_TAG}" == "" ]; then echo "${CI_COMMIT_REF_NAME}-${CI_PIPELINE_ID}"; else echo ${CI_COMMIT_TAG}; fi);
        # this echoes something like "(if [ "" == "" ]; then "master-2794"; else ; fi);"
        - echo $PROJECT_VERSION

Can this be done? If so, what have I missed? Thanks

like image 519
WellBloud Avatar asked Nov 09 '17 14:11

WellBloud


1 Answers

It is possible:

Add your logic into the variable section:

variables:
  VERSION_LOGIC: '(if [ "$${CI_COMMIT_TAG}" == "" ]; then echo "1.3.5.$$CI_PIPELINE_IID"; else echo "$${CI_COMMIT_TAG}"; fi);'

Now you are able to use this logic in a script secion of a job:

version:
  stage: versioning
  script:
    - VERSION=$(eval $VERSION_LOGIC)
    - echo "The current version is set to ${VERSION}."
like image 182
Marcel Melzig Avatar answered Sep 28 '22 03:09

Marcel Melzig