ahmedwaleedmalik
ahmedwaleedmalik

Reputation: 93

GitLab pass variable from one pipeline to another

We have a master pipeline, which is responsible for triggering pipelines from multiple projects and performing some steps. I want to pass a file from first pipelines output to the second one but i am unable to do so.

image:
  name: some-image
  entrypoint: [""]

variables:

stages:
  - create_file
  - print_file
  - consume_file

create_file:
  stage: create_file
  variables:
  trigger:
    project: user-name/project-name
    strategy: depend


print_file:
  stage: print_file
  script:
    - sleep 5
    - cat output_file.txt
  dependencies:
    - create_file

consume_file:
  stage: consume_file
  variables:
  trigger:
    project: user-name/project-name-alternate
    strategy: depend

I don't want to resort to scripts instead of trigger. Have tried artifacts etc but i couldn't find a way to pass them on to the next pipelines.

P.s. this is just a sample set out of the pipelines, there are multiple pipelines that are dependent on the output from first pipeline.

Upvotes: 6

Views: 8571

Answers (3)

VonC
VonC

Reputation: 1323463

See if GitLab 14.10 (April 2022) can help:

Improved pipeline variables inheritance

Previously, it was possible to pass some CI/CD variables to a downstream pipeline through a trigger job, but variables added in manual pipeline runs or by using the API could not be forwarded.

In this release we’ve added a new trigger:forward keyword to control what things you forward to downstream parent-child pipelines or multi-project pipelines, which provides a flexible way to handle variable inheritance in downstream pipelines.

See Documentation and Issue.

Note that, on self-managed GitLab, by default this feature is not available.
To make it available, ask an administrator to enable the feature flag named ci_trigger_forward_variables. The feature is not (yet) ready for production use (in Apr. 2022).

Example of trigger:forward:

Run this pipeline manually, with the CI/CD variable MYVAR = my value:

variables: # default variables for each job
  VAR: value

# Default behavior:
# - VAR is passed to the child
# - MYVAR is not passed to the child
child1:
  trigger:
    include: .child-pipeline.yml

# Forward pipeline variables:
# - VAR is passed to the child
# - MYVAR is passed to the child
child2:
  trigger:
    include: .child-pipeline.yml
    forward:
      pipeline_variables: true

# Do not forward YAML variables:
# - VAR is not passed to the child
# - MYVAR is not passed to the child
child3:
  trigger:
    include: .child-pipeline.yml
    forward:
      yaml_variables: false

Upvotes: 3

Lost Lont
Lost Lont

Reputation: 51

Passing artifacts from downstream pipelines to upstream ones may be implemented later according to this issue: https://gitlab.com/gitlab-org/gitlab/-/issues/285100

Upvotes: 0

Edmund Wang
Edmund Wang

Reputation: 139

Since artifacts can be passed between stages, you can try writing the variables into a file such as JSON, and parse it in another job.

Upvotes: 0

Related Questions