6

I'm trying to automate a DefectDojo template for Maven projects. I need to create a Product in DefectDojo instance and store the ID of the created product.

I've written a script for a job that is run ONLY ONE TIME (one pipeline when project is created, then its removed as it's useless).

Therefore, I cannot pass the ID I got to other stages using the .env artifact since the job is not run anymore.

In fact, I would like to dynamically assign PRODUCT_ID as a GitLab variable, using a job that is run once. Is this possible ?

1
  • PS: How and when are you using this variable? In a different pipeline or different project? Commented May 26, 2021 at 18:30

1 Answer 1

15

You can still pass it to other jobs or you can use it as an artifact and pass it for later jobs or pipelines. You can also consider saving it as a project variable if you want. You can pass environment variables from one job to another job in a later stage. These variables cannot be used as CI/CD variables to configure a pipeline, but they can be used in job scripts.

In the job script, save the variable as a .env file. Save the .env file as an artifacts:reports:dotenv artifact. Set a job in a later stage to receive the artifact by using the dependencies or the needs keywords. The later job can then use the variable in scripts. For example, with the dependencies keyword:

build:
  stage: build
  script:
    - echo "BUILD_VERSION=hello" >> build.env
  artifacts:
    reports:
      dotenv: build.env

deploy:
  stage: deploy
  script:
    - echo "$BUILD_VERSION"  # Output is: 'hello'
  dependencies:
    - build

For example, with the needs keyword:

build:
  stage: build
  script:
    - echo "BUILD_VERSION=hello" >> build.env
  artifacts:
    reports:
      dotenv: build.env

deploy:
  stage: deploy
  script:
    - echo "$BUILD_VERSION"  # Output is: 'hello'
  needs:
    - job: build
      artifacts: true
Sign up to request clarification or add additional context in comments.

2 Comments

That's not exactly what I wanted. I know I can pass variables from one job to another, but imagine I have 2 pipelines (for the same project). The first has 4 jobs, while the 2nd only has 3 jobs. I want the 2nd pipeline to have the variable from a job of the first pipeline that doesn't exist in the 2nd one. In other words, I want to save a variable from a pipeline that can be used in other pipelines for the same project.
This sounds like you would want to use a combination of artifacts and cache.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.