HiveBrain v1.2.0
Get Started
← Back to all entries
snippetMinor

How to use script in a Gitlab bridge job?

Submitted by: @import:stackexchange-devops··
0
Viewed 0 times
scriptgitlabbridgehowusejob

Problem

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?

Solution

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

Code Snippets

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

Context

StackExchange DevOps Q#13956, answer score: 1

Revisions (0)

No revisions yet.