2

I have a job which needs to include another job and needs to append a value to a variable, such as:

nightlies:
  variables:
    BUILD_NAME: "nightly"

  script:
    - $BUILD_NAME="${BUILD_NAME}-$(date +"%Y%m%d")"

  only:
    - schedules

  trigger:
    include:
      - local: /.gitlab-ci/build.yml
    strategy: depend

However reading the documentation script and trigger cannot be used together and I'm unsure of how else to create the BUILD_NAME with a date. Is there a way to use both. Otherwise, is there a way I could remove the trigger in order to use script, which would still call that job?

1
  • another option I explored was using before_script, in the root of the file, but it seems that the variable made there cannot be exported and I have no idea how else to make it accessible to the BUILD_NAME variable May 20, 2021 at 16:16

1 Answer 1

1

One option is to inherit your environment variable from another job. You can create two different jobs, one to create the environment variable, and one to trigger the local CI pipeline.

Once all that is done, you should have something like this:

stages:
  - build
  - deploy

nightlies_env:
  stage: build
  variables:
    BUILD_NAME: "nightly"
  script:
    - BUILD_NAME="${BUILD_NAME}-$(date +"%Y%m%d")"
    - echo "BUILD_NAME=$BUILD_NAME" >> build.env
  artifacts:
    reports:
      dotenv: build.env

nightlies:
  stage: deploy
  variables:
    BUILD_NAME: $BUILD_NAME
  trigger:
    include: build.yml
    strategy: depend
2
  • That also doesn't work: Syntax is incorrect. CI configuration validated, including all configuration added with the includes keyword. It's saying that it doesn't recognise the dependencies key. May 20, 2021 at 16:09
  • @user1092809 I've modified the question to not include the dependencies keyword. You're correct, dependencies isn't friendly with the trigger keyword :) The above solution passes the env. variable w/o using dependencies. Technically in this case, dependencies isn't a requirement, as artifacts are by default passed between stages in GitLab. Using dependencies is just a method for explicitly declaring specific stages to inherit artifacts from.
    – Preston Martin
    May 20, 2021 at 19:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.