Passing variable in multi project git pipeline - gitlab

YML of my trigger project:
trigger:
variables:
CODE_SHA: ${SHA}
stage: test
trigger:
project: dummy
branch: dump
strategy: depend
YML of my triggered project:
build:docker3.7:
stage: build
retry: 2
script:
- echo $code_sha
Ideal behaviour: It should print ${SHA} from the trigger project in triggered project.
Present behaviour- no value is getting passed.

Related

Gitlab - passing job id of parent pipeline to child pipeline

Is there any way we can pass job id of parent pipeline to child pipeline as variable
package:
stage: package
script:
- zip -r ./service.zip
deploy:
stage: deploy
variables:
trigger:
include:
- project: '<namespace>/<project>'
ref: '<branch>'
file: '<path to yml file>'
strategy: depend
Yes, it's supported by Gitlab. You need to pass the CI_PIPELINE_ID built-in variable.
.trigger_deploy:
stage: deploy
strategy: depend
trigger:
include:
- project: '<namespace>/<project>'
ref: '<branch>'
file: '<path to yml file>'
variables:
PARENT_PIPELINE_ID: $CI_PIPELINE_ID
PARENT_JOB_ID: $CI_JOB_ID
You can file these and more variables in Gitlab predefined variables documentation

gitlab CI/CD: How to trigger only a particular jobs

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.

Run specific job in Gitlab CI base on a condition

I have a repo QA/tests which I want to run all the jobs when there is a push to this repo.
I used a script to generate the jobs dynamically:
job-generator:
stage: generate
tags:
- kuber
script:
- scripts/generate-job.sh > generated-job.yml
artifacts:
paths:
- generated-job.yml
main:
trigger:
include:
- artifact: generated-job.yml
job: job-generator
strategy: depend
At the next step, I have another repo products/first which I want to run a specific job in QA/tests at every push in the products/first so I tried:
stages:
- test
tests:
stage: test
variables:
TARGET: first
trigger:
project: QA/tests
branch: master
strategy: depend
Then I tried to define a global TARGET: all variable in my main gitlab-ci.yml and override it with the TARGET: first in the above YAML.
generate-job.sh:
#!/bin/bash
PRODUCTS=("first" "second" "third")
for P in "${PRODUCTS[#]}"; do
cat << EOF
$P:
stage: test
tags:
- kuber
script:
- echo -e "Hello from $P"
rules:
- if: '"$TARGET" == "all"'
when: always
- if: '"$TARGET" == $P'
when: always
EOF
done
But no results. the downstream pipeline doesn't have any job at all!
Any idea?
I am not sure if this is now helpful, but this looks like an over complicated approach from the outside. I have to say i have limited knowledge and my answer is based on assumption:
the QA/tests repository contains certain test cases for all repositories
QA/tests has the sole purpose of containing the tests, not an overview over the projects etc.
My Suggestion
As QA/tests is only containing tests which should be executed against each project, i would create a docker image out of it which is contains all the tests and can actually execute them. (lets calls it qa-tests:latest)
Within my projects i would add a step which uses this images, with my source code of the project and executes the tests:
qa-test:
image: qa-tests:latest
script:
- echo "command to execute scripts"
# add rules here accordingly
this would solve the issue with each push into the repositories. For an easier usage, i could create a QA-Tests.gitlab-ci.yml file which can be included by the sub-projects with
include:
- project: QA/tests
file: QA-Tests.gitlab-ci.yml
this way you do not need to do updates with in the repositories if the ci snippet changes.
Finally to trigger the execution on each push, you only need to trigger the pipelines of the subprojects from the QA/tests.
Disclaimer
As i said, i have only a limited few, as the goal is described but not the motivation. With this approach you remove some of the directive calls - mainly the ones triggering from sub projects to QA/tests. And it generates a clear structure, but it might not fit your needs.
I solved it with:
gitlab-ci.yml:
variables:
TARGET: all
job-generator:
stage: generate
tags:
- kuber
script:
- scripts/generate-job.sh > generated-job.yml
artifacts:
paths:
- generated-job.yml
main:
variables:
CHILD_TARGET: $TARGET
trigger:
include:
- artifact: generated-job.yml
job: job-generator
strategy: depend
and use CHILD_TARGET in my generate-job.sh:
#!/bin/bash
PRODUCTS=("first" "second" "third")
for P in "${PRODUCTS[#]}"; do
cat << EOF
$P:
stage: test
tags:
- kuber
script:
- echo -e "Hello from $P"
rules:
- if: '\$CHILD_TARGET == "all"'
when: always
- if: '\$CHILD_TARGET == "$P"'
when: always
EOF
done
So I could call it from other projects like this:
stages:
- test
e2e-tests:
stage: test
variables:
TARGET: first
trigger:
project: QA/tests
branch: master
strategy: depend

Gitlab multi pipeline override variable passed to new pipelines

I am testing the new multi project functionality for Gitlab where I can trigger the next pipeline from my current pipeline:
https://docs.gitlab.com/ee/ci/multi_project_pipelines.html
I only have one problem. I have two pipelines A and B. in both pipelines I define global variables in the yaml file.
Pipeline A:
variables:
Name: 'vw-$CI_PIPELINE_ID'
stages:
- hello
- trigger
hello:
stage: hello
script:
- Write-Host "Hello World"
trigger:
stage: trigger
trigger:
project: my/PipelineB
branch: master
Pipeline B
variables:
Name: 'vw-$CI_PIPELINE_ID'
stages:
- triggered
triggered:
stage: triggered
script:
- write-host $Env:Name
Expectation is that the variables would be overwritten in Pipeline B.
Actual outcome is the variable has the same value as defined in Pipeline A

Azure pipeline will not trigger from a branch with a single azure-pipelines.yml file

I've recently set up an Azure pipeline using the following azure-pipelines.yml file in the master branch:
trigger:
- master
- feature/*
pool:
vmImage: 'ubuntu-latest'
variables:
buildConfiguration: 'Release'
steps:
- script: dotnet build --configuration $(buildConfiguration)
displayName: 'dotnet build $(buildConfiguration)'
I've also tried these two syntax examples for the trigger to no avail as well as removing the trigger completely.
trigger:
branches:
include:
- master
- feature/*
trigger:
branches:
include: ['*']
I've linked this to a GitHub repo where I created a branch called feature/cool_feature.
According to all of the docs I've read, I should be able to place the YAML file in the master branch which should start a build for both my branches. But all I can get this to do is start a build when a commit is made in the master branch.
The only want I could get the build to trigger is if I put another azure-pipelines.yml file inside of that branch.
Is this no longer the case or am I doing something wrong?
Apparently, you must have another copy of the master's azure-pipelines.yml file in the branch for this to work.

Resources