Search code examples
gitlabgitlab-ciparent-childpipeline

Gitlab pass values from child pipeline to parent pipeline


I have a monorepo which each package should be build as a docker. and when all changed package dockerized I want to deploy then using helmfile

I created a trigger job for each package that trigger a child pipeline.

I am looking for a way to get values that generated in child pipeline like docker tag or chart version in the parent pipeline.

I need those values to the deploy phase that is happened after all child pipeline finish.

.gitlab-ci.yml

---
trigger-package-a:
  stage: build
  trigger:
    include: .gitlab/ci/packages/package-gitlab-ci.yml
    strategy: depend
  rules:
   - changes:
      - "packages/package-a/**/*"
  variables:
    PACKAGE: package-a

trigger-package-b:
  stage: build
  trigger:
    include: .gitlab/ci/packages/package-gitlab-ci.yml
    strategy: depend
  rules:
    - changes:
      - "packages/package-b/**/*"
  variables:
    PACKAGE: package-b

done_job:
  stage: deploy
  script:
    - "echo DONE"
    - "cat config.json"
stages:
  - build
  - deploy

package-gitlab-ci.yml

stages:
  - bootstrap
  - validate

cache:
  key: "${PACKAGE}_${CI_COMMIT_REF_SLUG}"
  paths:
    - packages/${PACKAGE}/node_modules/
  policy: pull

install-package:
  stage: bootstrap
  script:
    - echo ${PACKAGE}}
    - echo '{"package":${PACKAGE}}' > config.json
    - "cd packages/${PACKAGE}/"
    - yarn install --frozen-lockfile
  artifacts:
    paths:
      - config.json
  cache:
    key: "${PACKAGE}_${CI_COMMIT_REF_SLUG}"
    paths:
     - packages/${PACKAGE}/node_modules/
    policy: pull-push

lint-package:
  script:
   - yarn lint
  stage: validate
  needs: [install-package]
  before_script:
   - "cd packages/${PACKAGE}/"

Solution

  • I get values and files only by get Artifacts via GitLab CI API

    previous - create token for your User Gitlab and create var GITLAB_USER_TOKEN

    enter image description here

    done_job:
      image: nexus/docker-images/ansible:2.9.24-9
      stage: done_job
      script:
        - yum install -y unzip 
        - >
            export CI_CHILD_PIPELINE_ID=$(curl --header "PRIVATE-TOKEN: $GITLAB_USER_TOKEN" "$CI_API_V4_URL/projects/$CI_PROJECT_ID/pipelines/$CI_PIPELINE_ID/bridges" | jq ".[].downstream_pipeline.id")
        - echo $CI_CHILD_PIPELINE_ID
        - >
            export CI_CHILD_JOB_ID=$(curl --header "PRIVATE-TOKEN: $GITLAB_USER_TOKEN" "$CI_API_V4_URL/projects/$CI_PROJECT_ID/pipelines/$CI_CHILD_PIPELINE_ID/jobs" | jq '.[].id')
        - echo $CI_CHILD_JOB_ID 
        - 'curl --output artifacts.zip --header "PRIVATE-TOKEN: $GITLAB_USER_TOKEN" "$CI_API_V4_URL/projects/$CI_PROJECT_ID/jobs/$CI_CHILD_JOB_ID/artifacts"'
        - unzip artifacts.zip
        - "echo DONE"
        - "cat config.json"
        - rm -rf artifacts.tar