3

This is my gitlab ci config. In the build stage the application is build in the distfolder, which is stored as an artifact. The next deploy stage is a trigger to run a child pipeline for the deployment.

But the dist artifacts aren't pulled for the child pipeline, so the child job fails.

  1. How do I get the dist artifacts in the child job?
  2. Even if the child deploy job fails, the parent pipeline shows a complete pass. I would expect to see, that at least one child has failed.

.gitlab-ci.yml

stages:
  - build
  - deployment

build:
  stage: build
  cache:
    key: ${CI_COMMIT_REF_SLUG}
    policy: pull
    paths:
      - node_modules/
  artifacts:
    when: always
    paths:
      - deployment.yml
      - dist/
  script:
    - node ./build-app.js # creates dist folder
    - node ./generate-pipeline.js # generates deployment.yml

trigger-deploy:
  stage: deployment
  trigger:
    include:
      - artifact: deployment.yml
        job: build

deployment.yml

stages:
  - deploy

app1-deploy:
  stage: deploy
  script:
    - ls dist # fails, as dist folder is not existing
user3142695
  • 15,844
  • 47
  • 176
  • 332

3 Answers3

8

We can define in child pipeline jobs to read the artefact from parent pipeline job.

First change needed is to define the PARENT_PIPELINE_ID while triggering child pipeline

  variables:
    PARENT_PIPELINE_ID: $CI_PIPELINE_ID

So, your code will change to

trigger-deploy:
  stage: deployment
  trigger:
    include:
      - artifact: deployment.yml
        job: build
  variables:
    PARENT_PIPELINE_ID: $CI_PIPELINE_ID

Now in the child pipeline, you have to use the parent pipeline id and the job which generates the artefact you need.

This can be done by adding, below to your job in child pipeline

  needs:
    - pipeline: $PARENT_PIPELINE_ID
      job: build

So, your code changes to

stages:
  - deploy

app1-deploy:
  stage: deploy
  script:
    - ls dist # fails, as dist folder is not existing
  needs:
    - pipeline: $PARENT_PIPELINE_ID
      job: build

Refer GitLab Documentation: https://docs.gitlab.com/ee/ci/yaml/#needspipelinejob

Sanjay Bharwani
  • 3,317
  • 34
  • 31
2

needs:pipeline:job

A child pipeline can download artifacts from a job in its parent pipeline or another child pipeline in the same parent-child pipeline hierarchy.

Documented here: https://docs.gitlab.com/ee/ci/yaml/#needspipelinejob

szucsz
  • 29
  • 3
0

The 'trigger-deploy' job needs a dependency of the 'build' job, to allow the artifacts to be providable.

To get the parent to fail when the child pipeline fails you'll need to add a strategy to the include strategy: depend. see last example

Master Noob
  • 659
  • 3
  • 9
  • 19
  • Thanks. `The 'trigger-deploy' job needs a dependency of the 'build' job` Could you please post a code example? You mean, there is a `need: []` missing? – user3142695 Jan 11 '22 at 13:21
  • `need: []` if your using DAG, `dependencies:` if your not. Added a link to the answer – Master Noob Jan 11 '22 at 13:54
  • 1
    @user3142695 I also come across the same problem. Are you able to access parent artifacts from the child stage? – Kaushal Apr 21 '22 at 18:59