Artifacts are not pulled in a child pipeline

Viewed 1908

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
3 Answers

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

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

Related