I am trying to trigger gitlab CI/CD pipeline of ProjectA from ProjectB
gitlab-ci.yml of projectB
stages:
- deploy
staging:
stage: deploy
trigger:
project: projectA
branch: main
And my gitlab-ci.yml of projectA is
image: docker:19.03.13
stages:
- build
- staging
fromprojectB: <--- when I trigger pipeline from B, I want only this job to run in build stage
stage: build
script:
..............
fromprojectC:
stage: build
fromprojectD:
stage: build
script:
.......
deploy-to-stage:
stage: staging
script:
............
I want only the fromprojectB job to run among the build stage jobs when the pipeline is triggers from projectB
How can I do this
You can pass environment variables from projectB and then use them in rules in projectA.
Example based on your input:
projectA:
stages:
- build
fromprojectB:
stage: build
rules:
- if: '$RUN_JOB_B'
when: always
- when: never
allow_failure: true
script:
- echo "Compiling the code from project B"
- echo "Compile complete."
fromprojectC:
stage: build
rules:
- if: '$RUN_JOB_C'
when: always
- when: never
allow_failure: true
script:
- echo "Compiling the code from project C"
- echo "Compile complete."
and projectB:
stages:
- build
build-job:
stage: build
variables:
RUN_JOB_B: "true"
trigger:
project: itersive/internal/rd/projecta
branch: main
Pipeline in projectB:
Pipeline in projectA:
In this setup you cannot run pipeline in projectA - all jobs require environment variables - see rules section.
Related
I have a gitlab pipeline which consiste of multiple stages[checkout->build->test->Deploy->cleanup->validation->terminate ec2].I have 3 requirement.
if any stage before the deploy stage failed, the pipeline stopped immediately.
if the deploy stage succeeds then the cleanup stage must skip and continue with another stage.
when only the deploy stage failed then only and must clean up stage run and exit the pipeline.
Here is my example pipeline.
stages:
- checkout
- build
- test
- Deploy
- cleanup
- reset
- Jenkins Jobs
- validation
checkout:
stage: checkout
tags:
- publishing
script:
- echo checkout
build:
stage: build
tags:
- automated-content-publishing
script:
- echo build
test:
stage: test
tags:
- publishing
script:
- echo test
Deploy:
stage: Deploy
tags:
- publishing
script:
- echo deploy
Cleanup:
stage: Cleanup
tags:
- publishing
script:
- echo clean-up
when: on_failure
reset:
stage: Nexus Insert
tags:
- publishing
script:
- echo reset
Jenksin Jobs:
stage: Jenkins Jobs
tags:
- publishing
script:
- echo jenkins jobs
vilidation:
stage: Vilidation
tags:
- publishing
script:
- echo validation
this pipeline works perfectly in 2 and 3 requirements. pipeline not working for 1 requirement. if any stage failed before the deploy stage failed cleanup stage call.
I want to call the cleanup stage only and only when the deploy stage failed.
Please guide me to build this case.
You could use something like this
Deploy:
stage: Deploy
tags:
- publishing
script:
- echo deploy
...
Cleanup:
stage: Cleanup
tags:
- publishing
script:
- echo clean-up
needs:
- job: Deploy
when: on_failure
If deploy stage has many jobs just add them to needs section. For more info look at reference.
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**.
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/**/*
i have a problem with gitlab (community edition, version 14.1.2) CI with complex pipeline on my monorepo.
My structure is client/server:
root/
---- server/
-------- lib/
----------- libA/
----------- libB/
----------- libC/
-------- applications/
----------- appA/
----------- appB/
----------- appC/
---- client/
-------- applications/
------------- appA/
------------- appB/
...
Every folder (root, server, lib, libA, libB, libC etc...) have his own ".gitlab-ci.yml"
Root ".gitlab-ci.yml" is:
stages:
- build
- test
build-server:
stage: build
trigger:
include:
- local: 'server/.gitlab-ci.yml'
rules:
- changes:
- server/**/*
build-client:
stage: build
trigger:
include:
- local: 'client/.gitlab-ci.yml'
rules:
- changes:
- client/**/*
Server ".gitlab-ci.yml" is:
stages:
- build
- test
build-lib:
stage: build
trigger:
include:
- local: 'lib/.gitlab-ci.yml'
rules:
- changes:
- lib/**/*
build-applications:
stage: build
trigger:
include:
- local: 'applications/.gitlab-ci.yml'
rules:
- changes:
- applications/**/*
lib ".gitlab-ci.yml" is:
stages:
- build
- test
build-libA:
stage: build
script:
- echo "Execute libA build!"
rules:
- changes:
- libA/**/*
build-libB:
stage: build
script:
- echo "Execute libB build!"
rules:
- changes:
- libB/**/*
If i change a file inside libA only the ".gitlab-ci.yml" of root folder is triggered, other subfolders not detect file changes and not trigger the build.
The purpose of this configuration is that , for example, when i change a file inside libA, the pipeline detects the changes and build the libA.
Somone can help me to resolve? I hope the structure and the problem is clear. Thanks.
UPDATE
I'm using gitlab 14.1.0
Thanks to DavidC for the answer but with your solution I have not solved my problem, especially with the trigger $CI_PROJECT_PATH seems not to work.
After some time I finally got a solution (which can be evolved with variables)
Root ".gitlab-ci.yml" is:
stages:
- build
- test
build-server:
stage: build
trigger:
include:
- local: '/server/.gitlab-ci.yml'
rules:
- changes:
- server/**/*
build-client:
stage: build
trigger:
include:
- local: '/client/.gitlab-ci.yml'
rules:
- changes:
- client/**/*
Server ".gitlab-ci.yml" is:
stages:
- build
- test
build-lib:
stage: build
trigger:
include:
- local: '/server/lib/.gitlab-ci.yml'
rules:
- changes:
- server/lib/**/*
build-applications:
stage: build
trigger:
include:
- local: '/server/applications/.gitlab-ci.yml'
rules:
- changes:
- server/applications/**/*
lib ".gitlab-ci.yml" is:
stages:
- build
- test
build-libA:
stage: build
script:
- echo "Execute libA build!"
rules:
- changes:
- server/lib/libA/**/*
build-libB:
stage: build
script:
- echo "Execute libB build!"
rules:
- changes:
- server/lib/libB/**/*
Pay attention to this line from the gitlab documentation: "Parent and child pipelines were introduced with a maximum depth of one child pipeline level, which was subsequently increased to two. A parent pipeline can activate many child pipelines and these child pipelines can activate their own child pipelines. It is not possible to activate another level of child pipeline. " refer to: https://docs.gitlab.com/ee/ci/pipelines/parent_child_pipelines.html#nested-child-pipelines
Thanks for help!
It seems like GitLab child-pipeline context execution path is the same as the root directory of your repository, and is not relative to the path of the child-pipeline gitlab-ci.yml file.
The only solution so far seems to either give the path to the directory you want as a variable to your child-pipeline or to directly define it in the .gitlab-ci.yml.
Example :
Root ".gitlab-ci.yml" is:
stages:
- build
- test
build-server:
stage: build
variables:
CI_ROOT_DIR: server # you don't need to provide it, if you define it in the server/.gitlab-ci.yml file
trigger:
include:
- local: '$CI_ROOT_DIR/.gitlab-ci.yml'
rules:
- changes:
- $CI_ROOT_DIR/**/*
build-client:
stage: build
variables:
CI_ROOT_DIR: client
trigger:
include:
- local: '$CI_ROOT_DIR/.gitlab-ci.yml'
rules:
- changes:
- $CI_ROOT_DIR/**/*
Server ".gitlab-ci.yml" is:
stages:
- build
- test
variables:
CI_ROOT_DIR: $CI_PROJECT_PATH/server # default
build-lib:
stage: build
variables:
CI_ROOT_DIR: $CI_ROOT_DIR/lib # you don't need to provide it, if you define it in the server/lib/.gitlab-ci.yml file
trigger:
include:
- local: '$CI_ROOT_DIR/.gitlab-ci.yml'
rules:
- changes:
- $CI_ROOT_DIR/**/*
build-applications:
stage: build
variables:
CI_ROOT_DIR: $CI_ROOT_DIR/applications # you don't need to provide it, if you define it in the server/applications/.gitlab-ci.yml file
trigger:
include:
- local: '$CI_ROOT_DIR/.gitlab-ci.yml'
rules:
- changes:
- $CI_ROOT_DIR/**/*
lib ".gitlab-ci.yml" is:
stages:
- build
- test
variables:
CI_ROOT_DIR: $CI_PROJECT_PATH/server/lib # default
build-libA:
stage: build
script:
- echo "Execute libA build!"
rules:
- changes:
- $CI_ROOT_DIR/libA/**/*
build-libB:
stage: build
script:
- echo "Execute libB build!"
rules:
- changes:
- $CI_ROOT_DIR/libB/**/*
It would be better tho if it was possible to choose the context of the execution of the pipeline when triggered by the parent-pipeline or to have a CI_CHILD_PIPELINE_DIR variable available by Gitlab predefined environment variables
I have four build stages, which are operated manually. What I'd like is to execute the deploy stage, after one build stage is finished. Right now my implementation only works when all four build stages are finished.
Use case: Dev klicks on the environment he wants to build. After build is done, it is deployed to the systems. Deploy should start automatically after build is finished
Q: Is there a way to execute the deploy stage after only one build is done?
My implementation of the build pipelines (simplified):
production:
stage: env
script:
- echo build one
when: manual
allow_failure: false
production2:
stage: env
script:
- echo build two
when: manual
allow_failure: false
staging:
stage: env
script:
- echo build three
when: manual
allow_failure: false
staging2:
stage: env
script:
- echo build four
when: manual
allow_failure: false
This is my deploy stage
build:
stage: build
needs: [production, production2, staging, staging2]
when: on_success
script:
- echo do deploy stuff
Many thanks and I wish you a nice day
Maybe something like this help?
build:
stage: build
needs:
- job: production
optional: true
- job: production2
optional: true
- job: staging
optional: true
- job: staging2
optional: true
when: on_success
script:
- echo do deploy stuff
So far I've only found a solution that works.
stages:
- env
- connections
- build
production:
stage: env
script:
- echo build one
when: manual
allow_failure: false
production2:
stage: env
script:
- echo build two
when: manual
allow_failure: false
staging:
stage: env
script:
- echo build three
when: manual
allow_failure: false
staging2:
stage: env
script:
- echo build four
when: manual
allow_failure: false
# Connections
run:build:production:
extends: .build
stage: connections
needs:
- job: production
run:build:production2:
extends: .build
stage: connections
needs:
- job: production2
run:build:staging:
extends: .build
stage: connections
needs:
- job: staging
run:build:staging2:
extends: .build
stage: connections
needs:
- job: staging2
.build:
stage: build
script:
- echo do deploy stuff
I need to specify the stage instead of triggering the whole thing. This is how it's currently configured.
stage: bridge
trigger:
project: user/project2
branch: master
strategy: depend
You can specify variables in the parent pipeline which you can check in the child pipeline and specify the job creation via rules. In the below example only job1 is run as only the variable $FOO is set in the parent pipeline.
You parent pipeline:
test:
stage: test
variables:
FOO: bar
trigger:
project: user/project2
branch: master
strategy: depend
Your child pipeline can look like this:
stages:
- job1
- job2
job1:
stage: job1
script:
- echo "job1"
rules:
- if: '$FOO'
job2:
stage: job2
script:
- echo "job2"
rules:
- if: '$BAR'