Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GitLab CI: Export variable in before_script build job

I try to implement a conditional versioning depending on if the CI script runs for a tagged branch or not. However the version var is not resolved. Instead it is printed as a string.

The relevant jobs of the GitLab CI script:

# build template
.build_base_template: &build_base_template
  image: registry.gitlab.com/xxxxxxx/npm:latest
  tags:
    - docker
  stage: LintBuildTest
  script:
    - export CUR_VERSION='$(cat ./version.txt)$BUILD_VERSION_SUFFIX'
    - npm ci
    - npm run build
  artifacts:
    expire_in: 1 week
    paths:
      - dist/

# default build job
build:
  before_script:
    - export BUILD_VERSION_SUFFIX='-$CI_COMMIT_REF_SLUG-SNAPSHOT-$CI_COMMIT_SHORT_SHA'
  <<: *build_base_template
  except:
    refs:
      - tags
  only:
    variables:
      - $FEATURE_NAME == null

# specific build job for tagged versions
build_tag:
  before_script:
    - export BUILD_VERSION_SUFFIX=''
  <<: *build_base_template
  only:
    refs:
      - tags
like image 551
Simon Thiel Avatar asked Feb 04 '26 20:02

Simon Thiel


1 Answers

Variables which are exported within before_script are visible within script.

before:
  before_script:
    - export HELLOWELT="hi martin"
  script:
    - echo $HELLOWELT  # prints "hi martin"
like image 60
mr.wolle Avatar answered Feb 09 '26 03:02

mr.wolle



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!