I want to create a parent pipeline job with stages that call trigger other jobs, which are also pipeline jobs.
Can I achieve this?
Here is a skeleton of what I want:
Parent job's script:
pipeline {
parallel{
stage("A") {
build 'name of job 1 which is a pipeline job again and has a parallel block with stages in it'
}
stage("B") {
build 'name of job 2 which is a pipeline job again and has a parallel block with stages in it'
}
stage("C") {
build 'name of job 3 which is a pipeline job again and has a parallel block with stages in it'
}
}
}
Does it work this way? Is there any way to achieve this
Sure does,
This is what we are using, we promote between environments by kicking off the same job from the current execution and don't wait for the result.
build(job: "org/${jobName}/${BRANCH_NAME}",
parameters: [
new StringParameterValue('ENV', env),
new StringParameterValue('ENV_NO', env_no),
new StringParameterValue('ARTIFACT_NAME', params.ARTIFACT_NAME)
],
propagate: false,
wait: false,
)
Refer to the reference for all options
https://jenkins.io/doc/pipeline/steps/pipeline-build-step/
Related
I am trying to have a pipeline that executes multiple sequential jobs.
The problem is that if I have the "propagate false" flag, the jobs are executed but the pipeline build always returns 'Success' regardless the sub-jobs status.
If I want the pipeline reflects the 'Fail' status when a sub-job fails, and remove the propagate flag, the sequence is broken at that failure point, and no more jobs are executed.
Can you help me getting the best way to achieve this?
I hope I was clear. Thank you very much.
pipeline{
stages{
stage('Tests'){
steps{
parallel(
'TestSet':{
build wait: true, job: 'Test A'
build wait: true, job: 'Test B'
build wait: true, job: 'Test C'
}
)
}
}
}
}
When you run the build step it actually returns an RunWrapper object (see Java Docs).
The RunWrapper has a getResult() function which enables you to get the result of the build that was executed, along side many other proproteins of the executed build, like the build number.
You can then run your jobs with the propagate false option, save the results, examine them after all builds are finished and then run your required logic.
For example:
pipeline{
stages{
stage('Tests'){
steps{
script {
parallel(
'TestSet': {
// Collect all results of build execution into a map
def results = [:]
results['Test A'] = build(wait: true, job: 'Test A').getResult()
results['Test B'] = build(wait: true, job: 'Test B').getResult()
results['Test C'] = build(wait: true, job: 'Test C').getResult()
// Analyze the result
failedJobs = results.findAll(it.value != 'SUCCESS') // you can also use Result.SUCCESS instead of the string
if (failedJobs){
error "The following jobs have failed: ${failedJobs.collect{it.key}.join(',')}"
}
}
)
}
}
}
}
}
I have a jenkinfile like this
node any {
def global variables.
stage {
//build job 1
build job 'job1'
}
stage{
//build job 2
build job 'job2'
}
What's happening when i run this is , job 1 gets successfully built on jenkins, but anything written after the first 'build' statement doesn't get executed. I have tried moving the stages around the first always work but second doesn't as the control never reaches to second stage.
What am i doing wrong here?
Did you tried naming your stages ? As shown here : https://jenkins.io/doc/book/pipeline/#scripted-pipeline-fundamentals
node any {
def global variables.
stage('Build 1') {
//build job 1
build job: 'job1'
}
stage('Build 2') {
//build job 2
build job: 'job2'
}
}
We Have a Jenkins pipeline with several stages (powershell).
each stage has one or more jobs to be executed.
I need to use the results of the first job and pass them to the next job at the next stage.
except the pipeline all jobs are powershell.
example:
node('one'){
stage ('Get Info from DB'){
build job: 'test1'
}
stage ('Do Something'){
build job: 'test2' parameters: [string(name: 'PARAM1', value: $PARAM_FROM_test1 )]
}
}
Assuming that you want to retrieve the outputs of the first job test1 and use these outputs in a second job, you can simply in the second job use the copyArtifact class :
step([$class: 'CopyArtifact', projectName: 'test1'])
This is achieved, assuming that in test1 job, you use the ArtifactArchiver class like (for instance):
step([$class: "ArtifactArchiver", artifacts: "*.tgz", fingerprint: true])
I have a jenkins setup with a bunch of pipelines.
I wrote a new pipeline which can start all pipelines at once.
I would like to build other stages, even if one of them fails.
The script currently looks like this
stage 'CentOS6'
build 'centos6.testing'
stage 'CentOS7'
build 'centos7.testing'
stage 'Debian7'
build 'debian7-x64.testing'
stage 'Debian8'
build 'debian8-x64.testing'
The build scripts itself contain the node they should run on.
How can the script continue with the following stages even if one of them fails.
Cheers
If they should be run in a sequence you can do something like this:
def buildResult= 'success'
try{
build 'centos6.testing'
}catch(e){
buildResult = 'failure'
}
currentBuild.result = buildResult
If they should be run in parallell you just run them:
https://www.cloudbees.com/blog/parallelism-and-distributed-builds-jenkins
If you use the parallel step, this should work as you expect by default, as the failFast option, which aborts the job if any of the parallel branches fail, defaults to false.
For example:
parallel(
centos6: { build 'centos6.testing' },
centos7: { build 'centos7.testing' },
debian7: { build 'debian7-x64.testing' },
debian8: { build 'debian8-x64.testing' }
)
What worked for me:
'Task' : {
build( job : "DemoJob-2", wait: false )
build( job : "DemoJob-3", wait: false )
}
How can I trigger build of another job from inside the Jenkinsfile?
I assume that this job is another repository under the same github organization, one that already has its own Jenkins file.
I also want to do this only if the branch name is master, as it doesn't make sense to trigger downstream builds of any local branches.
Update:
stage 'test-downstream'
node {
def job = build job: 'some-downtream-job-name'
}
Still, when executed I get an error
No parameterized job named some-downtream-job-name found
I am sure that this job exists in jenkins and is under the same organization folder as the current one. It is another job that has its own Jenkinsfile.
Please note that this question is specific to the GitHub Organization Plugin which auto-creates and maintains jobs for each repository and branch from your GitHub Organization.
In addition to the above mentioned answers: I wanted to start a job with a simple parameter passed to a second pipeline and found the answer on http://web.archive.org/web/20160209062101/https://dzone.com/refcardz/continuous-delivery-with-jenkins-workflow
So i used:
stage ('Starting ART job') {
build job: 'RunArtInTest', parameters: [[$class: 'StringParameterValue', name: 'systemname', value: systemname]]
}
First of all, it is a waste of an executor slot to wrap the build step in node. Your upstream executor will just be sitting idle for no reason.
Second, from a multibranch project, you can use the environment variable BRANCH_NAME to make logic conditional on the current branch.
Third, the job parameter takes an absolute or relative job name. If you give a name without any path qualification, that would refer to another job in the same folder, which in the case of a multibranch project would mean another branch of the same repository.
Thus what you meant to write is probably
if (env.BRANCH_NAME == 'master') {
build '../other-repo/master'
}
You can use the build job step from Jenkins Pipeline (Minimum Jenkins requirement: 2.130).
Here's the full API for the build step: https://jenkins.io/doc/pipeline/steps/pipeline-build-step/
How to use build:
job: Name of a downstream job to build. May be another Pipeline job, but more commonly a freestyle or other project.
Use a simple name if the job is in the same folder as this upstream Pipeline job;
You can instead use relative paths like ../sister-folder/downstream
Or you can use absolute paths like /top-level-folder/nested-folder/downstream
Trigger another job using a branch as a param
At my company many of our branches include "/". You must replace any instances of "/" with "%2F" (as it appears in the URL of the job).
In this example we're using relative paths
stage('Trigger Branch Build') {
steps {
script {
echo "Triggering job for branch ${env.BRANCH_NAME}"
BRANCH_TO_TAG=env.BRANCH_NAME.replace("/","%2F")
build job: "../my-relative-job/${BRANCH_TO_TAG}", wait: false
}
}
}
Trigger another job using build number as a param
build job: 'your-job-name',
parameters: [
string(name: 'passed_build_number_param', value: String.valueOf(BUILD_NUMBER)),
string(name: 'complex_param', value: 'prefix-' + String.valueOf(BUILD_NUMBER))
]
Trigger many jobs in parallel
Source: https://jenkins.io/blog/2017/01/19/converting-conditional-to-pipeline/
More info on Parallel here: https://jenkins.io/doc/book/pipeline/syntax/#parallel
stage ('Trigger Builds In Parallel') {
steps {
// Freestyle build trigger calls a list of jobs
// Pipeline build() step only calls one job
// To run all three jobs in parallel, we use "parallel" step
// https://jenkins.io/doc/pipeline/examples/#jobs-in-parallel
parallel (
linux: {
build job: 'full-build-linux', parameters: [string(name: 'GIT_BRANCH_NAME', value: env.BRANCH_NAME)]
},
mac: {
build job: 'full-build-mac', parameters: [string(name: 'GIT_BRANCH_NAME', value: env.BRANCH_NAME)]
},
windows: {
build job: 'full-build-windows', parameters: [string(name: 'GIT_BRANCH_NAME', value: env.BRANCH_NAME)]
},
failFast: false)
}
}
Or alternatively:
stage('Build A and B') {
failFast true
parallel {
stage('Build A') {
steps {
build job: "/project/A/${env.BRANCH}", wait: true
}
}
stage('Build B') {
steps {
build job: "/project/B/${env.BRANCH}", wait: true
}
}
}
}
The command build in pipeline is there to trigger other jobs in jenkins.
Example on github
The job must exist in Jenkins and can be parametrized.
As for the branch, I guess you can read it from git
Use build job plugin for that task in order to trigger other jobs from jenkins file.
You can add variety of logic to your execution such as parallel ,node and agents options and steps for triggering external jobs. I gave some easy-to-read cookbook example for that.
1.example for triggering external job from jenkins file with conditional example:
if (env.BRANCH_NAME == 'master') {
build job:'exactJobName' , parameters:[
string(name: 'keyNameOfParam1',value: 'valueOfParam1')
booleanParam(name: 'keyNameOfParam2',value:'valueOfParam2')
]
}
2.example triggering multiple jobs from jenkins file with conditionals example:
def jobs =[
'job1Title'{
if (env.BRANCH_NAME == 'master') {
build job:'exactJobName' , parameters:[
string(name: 'keyNameOfParam1',value: 'valueNameOfParam1')
booleanParam(name: 'keyNameOfParam2',value:'valueNameOfParam2')
]
}
},
'job2Title'{
if (env.GIT_COMMIT == 'someCommitHashToPerformAdditionalTest') {
build job:'exactJobName' , parameters:[
string(name: 'keyNameOfParam3',value: 'valueOfParam3')
booleanParam(name: 'keyNameOfParam4',value:'valueNameOfParam4')
booleanParam(name: 'keyNameOfParam5',value:'valueNameOfParam5')
]
}
}