Get gitlab parent project details in child project

2.7k views Asked by At

I am using below two gitlab repository

  1. Parent Gitlab repo - Application code, for example - Angular application
  2. Child Gitlab repo - For Gitlab Pipeline, has only gitlab-ci.yml file which contain script to run pipeline

I am calling pipeline/child-project gitlab-ci.yml file form parent using below steps

Parent Gitlab repo - gitlab-ci.yml file

  include:
  - project: 'my-group/child-project'
    ref: master
    file: '/templates/.gitlab-ci-template.yml'

Child-project - gitlab-ci.yml file

stages:
  - test
  - build

before_script:
- export PARENT_PROJECT_NAME = ?
- export PARENT_PROJECT_PIPELINE_ID = ?
- export PARENT_PROJECT_BRANCH_NAME = ?


job 1:
  stage: test
  script: 
    - echo "Runnig test for project ${PARENT_PROJECT_NAME}"
    - node_modules/.bin/ng test


release_job:
  stage: build
  script: node_modules/.bin/ng build --prod
  artifacts:
    name: "project-$CI_COMMIT_REF_NAME"
    paths:
      - dist/
  only:
    - tags

How can I get the parent-repo details like parent-project name, pipeline-id & branch name in child-project which is running the pipeline?

One way is to define the variables in parent-project and use in child project, but is there any other way where we can directly access the parent-project detail in the child-project?

2

There are 2 answers

0
Adam Marshall On

Since you’re including the child project’s configuration instead of triggering it, the two pipeline definition files are merged and become one before the pipeline starts, so there’s be no practical difference between this method and having the content of the child project’s definition in the parent’s.

Because of this, all of the predefined variables will be based on the parent project if the pipeline runs there. For example, variables like $CI_COMMIT_REF_NAME, $CI_PROJECT_NAME will point to the parent project and the parent project's branches.

0
AJ Arena On

In your example, include is not the same as trigger. Include just merges all the files together into one giant pipeline so you should be able to access any variables you want from the included files, as long as the variable's scope is correct.


If you are actually looking to pass details to a child pipeline from a parent pipeline you could add a job that exports the variables and details you want to the dotenv, then have the child pipeline access that dotenv. This would allow the code to be dynamic inside of hard coding in the variables and directly passing them to the child pipelines

export-parent-details:
    script:
        - echo "PARENT_PROJECT_NAME=?" >> build.env
        - echo "PARENT_PROJECT_PIPELINE_ID=?" >> build.env
        - echo "PARENT_PROJECT_BRANCH_NAME=?" >> build.env
    artifacts:
      reports:
        dotenv: build.env

trigger-child:
    stage: docker_hub
    trigger:
        include:
            - project: 'my-group/child-project'
              ref: master
              file: '/templates/.gitlab-ci-template.yml'
    # use this variable in child pipeline to download artifacts from parent pipeline
    variables:
        PARENT_PIPELINE_ID: $CI_PIPELINE_ID

Then inside the child jobs, you should be able to access the parent artifacts from the parent

child-job:
    needs:
        - pipeline: $PARENT_PIPELINE_ID
          job: export-parent-details
    script:  
        - echo $PARENT_PROJECT_NAME    

See


Another option could be to make an API call to the get the parent project's details since the runners have a read-only token under $CI_JOB_TOKEN, this method is dependent on repo access privileges and the details you want

curl -H "JOB_TOKEN: $CI_JOB_TOKEN" "https://gitlab.com/api/v4/{whatever the api call is}"