Passing variables to 'needs' attribute

Hello!
I’m new to GitLab CI/CD.

I wish to have a job inside PROJECT_2 access artifacts from another job on a different project - PROJECT_1. I have added a snippet below.

PROJECT_1:
.gitlab-ci.yml

---
job1:
  ---
  artifacts:
    paths:
      - dir1

PROJECT_2:
.gitlab-ci.yml

---
job2:
  --
  needs:
    - project: PROJECT_1
      job: job1
      ref: master
      artifacts: true

This code above works fine. I would like to know if we can have the entries under needs attribute like project, job and ref to be dynamic? Can I pass an env var, say PROJ_DEP and set it to PROJECT_1 when I kick off a pipeline and have the needs attribute expand them correctly?
I have tried passing a variable for components inside needs attribute like shown below.

job2:
  --
  needs:
    - project: $PROJ_DEP                 # linux runner
      job: $JOB_DEP
      ref: $REF_DEP
      artifacts: true

But, I encounter the following error.

This job depends on other jobs with expired/erased artifacts:
Please refer to CI/CD YAML syntax reference | GitLab

I have confirmed that the job does have artifacts available and as I mentioned above, it works fine if I don’t use the variables.

I’m using GitLab v13.3.6-ee and Runner v13.3.1

Thanks.

1 Like

Did you find any related solution after a year?

Sadly variables cannot be used for needs:… according to Where variables can be used

As I’m facing the same problem that I want to pass artifacts from multiple upstream projects to one single downstream project. What currently investigate is to

I have not yet checked this myself and maybe the permission model prevents the download, but as triggering the downstream pipeline requires the upstream job to have permissions to do that, hopefully the reverse is also true and the downstream job will be able to access the artifacts from the upstream job.

Bumping this as I have a similar use-case. I’m using parallel:matrix to automate a lot of build jobs in a mono-repo, however, for jobs which depend on these dynamic jobs, I’m unable to use variables within the needs block, and therefore have to manually configure jobs for each parameter within the parallel:matrix.