Search code examples
gitlabgitlab-cipipeline

Gitlab CI Child pipeline


I have a problem with gitlab ci child pipelines. Need to trigger ci pipeline automatically after each commit in repo that have more than one app. Need to configure to detect which folder/files were modified in order to know which app pipeline to trigger

Example of structure

Main/
---- applicationsA/
-------- appA1/
-------- appA2/
-------- appA3/
---- applicationsB/
-------- appB1/
-------- appB2/
-------- appB3/

Main ".gitlab-ci.yml" is:


workflow:
  rules:
    - if: ‘$CI_PIPELINE_SOURE == “web”’

variables:
  APPNAME: $APPNAME

stages:
  - child-pipelines

appA1:
  stage: child-pipelines
  trigger:
    include:
      - local: applicationA/appA1/gitlab-ci.yml
    strategy: depend
  rules:
    - if: $APPNAME == “appA1” && $CI_PIPELINE_SOURE == “web”

appA2:
  stage: child-pipelines
  trigger:
    include:
      - local: applicationA/appA2/gitlab-ci.yml
    strategy: depend
  rules:
    - if: $APPNAME == “appA1” && $CI_PIPELINE_SOURE == “web”
...

appA1 ".gitlab-ci.yml" is:


stages:
  - build
  - test

build-appA1:
  stage: build
  script:
    - echo "Execute appA1 build!"

publish-appA1:
  stage: build
  script:
    - echo "Execute appA1 publish!"

appA2 ".gitlab-ci.yml" is:


stages:
  - build
  - test

build-appA2:
  stage: build
  script:
    - echo "Execute appA1 build!"

publish-appA2:
  stage: build
  script:
    - echo "Execute appA1 publish!"

The purpose of this configuration is that , for example, when i change a file inside app**, the pipeline detects the changes and build the app**.


Solution

  • You can use rules:changes with a glob pattern and only run a certain job if anything changes in the specific app folder:

    appA1:
      stage: child-pipelines
      trigger:
        include:
          - local: applicationA/appA1/gitlab-ci.yml
        strategy: depend
      rules:
        - if: '$APPNAME == "appA1" && $CI_PIPELINE_SOURE == "web"'
          changes:
            - Main/applicationsA/appA1/**/*