Jenkins downstream error not propagated to upstream - jenkins

I have a pipeline to build which will trigger another pipeline for running the (long) tests. My idea is to have the downstream test job run without waiting (to allow frequent build jobs). However, when I add wait: false to the build job command a failure will not change the status of the build job:
JenkinsfileBuild
node {
def app
stage('Clone repository') {
}
stage('Merge main') {
}
stage('Build image') {
}
stage('Upload image') {
sh "echo would push image here"
}
}
stage('Test Image') {
build job: "../${env.JOB_NAME.split("/")[0]}Test/${env.BRANCH_NAME}", wait: false
}
JenkinsfileTest
node {
stage('Run Test') {
sh "exit -1";
}
}
I was expecting that the failing test job would mark the build job as failed as it does, when wait: true is set.
What am I missing?

Add propagate: true option to the build step. And you need to wait for the Job in order to get the result.
build job: "../${env.JOB_NAME.split("/")[0]}Test/${env.BRANCH_NAME}", wait: true, propagate: true
If you don't want to wait for the Job you can execute things in parallel.
pipeline {
agent any
stages {
stage("Your Stages") {
parallel {
stage("Buld Job") {
build job: "../${env.JOB_NAME.split("/")[0]}Test/${env.BRANCH_NAME}", wait: true, propagate: true
}
stage("Other Stuff") {
sh(‘cd /path/to/proj-2 && make && make publish’)
}
}
}
}
}

Related

Is it possible to get build number even if build is unstable but not failed?

When building a job in a scripted pipeline, I would like to keep the external build number even if that build is unstable but not failed.
pipeline {
agent any
stages {
stage('Job1') {
steps {
script {
Job1 = build job: 'Job1'
}
}
}
stage('Job2') {
steps {
build job: 'Job2',
parameters: [
string(
name: 'Job1_ID'
value: "${Job1.number}"
)
]
}
}
}
}
I have tried with a catchError() around the job1 build, but still have that problem if the build is unstable.
I have also tried with propagate:false parameter, but I can never see the actual status of the build visually, plus, I don't want the second build to be triggered if the first is failed.
Is there any solution for that ?
What you can do is set propagate: false and then conditionally execute your second Job. Please see the pipeline below.
pipeline {
agent any
stages {
stage('Job1') {
steps {
script {
Job1 = build job: 'Job1', propagate: false
}
}
}
stage('Job2') {
when { expression { return Job1.resultIsBetterOrEqualTo("SUCCESS")}}
steps {
build job: 'Job2',
parameters: [
string(name: 'Job1_ID',value: "${Job1.number}")
]
}
}
}
}

Jenkins: Trigger another job with branch name

I am using Jenkins Pipeline via declarative and I would like to trigger another job with branch name.
For instance, I have two different pipeline(PipelineA -PipelineB) with stages JobA and JobB.
One of the stage for JobA should trigger the JobB via paramater using env.GIT_BRANCH. What I mean, if we trigger the JobA via origin/develop, then it should trigger the 'JobB' and run the stages where it has origin/develop condition.
Meanwhile, we also making some separate changes on JobB and it also has its own GIT_BRANCH expression.Thus I could not able to find a way to manage this separately without affecting JobA. To be clarify, when JobA trigger JobB with origin/stage parameter, due to latest changes on JobB is origin/development whereas GIT_BRANCH is origin/development, I can not able to run the stages which has stage condition.
Here is my script.
stage ('Job A') {
steps {
script {
echo "Triggering job for branch ${env.GIT_BRANCH}"
ret = build(job: "selenium_tests",
parameters: [
string(name: "projectName", value: "Project1"),
string(name: "branchName", value: "env.GIT_BRANCH")
],
propagate: true,
wait: true)
echo ret.result
currentBuild.result = ret.result
}
}
}
parameters {
string(defaultValue: "project1", description: 'Which project do you want to test?', name: 'projectName')
string(defaultValue: "origin/development", description: 'Environment for selenium tests', name:'branchName')
}
stage ('Job B') {
when {
beforeAgent true
expression { params.projectName == 'Project1' }
expression { params.branchName == "origin/stage"}
expression{ return env.GIT_BRANCH == "origin/stage"}
}
steps {
script {
//Do something
}
}
}
Pass down one more param for branch when trigger Job B
stage('Trigger Job A') {}
stage('Trigger Job B') {
when {
allOf {
beforeAgent true
expression { params.projectName == 'Project1' }
expression{ return env.GIT_BRANCH == "origin/stage"}
}
}
steps {
build(job: "selenium_tests/Job B",
parameters: [
string(name: "projectName", value: "Project1")
strint(name: "branchName", value: "${env.GIT_BRANCH}")
],
propagate: true,
wait: true)
}
}
In Job B' Jenkinsfile add one stage as the first stage to switch to desired branch
pipeline {
parameters {
string(name: 'branchName', defaultValue: 'develop')
}
stages {
stage('Switch branch') {
steps {
sh "git checkout ${params.branchName}"
}
}
// other stages
}
}

Jenkins pipeline: prevent a job from failing if upstream jobs do not exist

In my Jenkins I have a Groovy pipeline script which triggers multiple jobs afterward:
stage('Build other pipelines') {
steps {
build job: "customer-1/${URLEncoder.encode(BRANCH_NAME, "UTF-8")}", propagate: true, wait: false
build job: "customer-2/${URLEncoder.encode(BRANCH_NAME, "UTF-8")}", propagate: true, wait: false
build job: "customer-3/${URLEncoder.encode(BRANCH_NAME, "UTF-8")}", propagate: true, wait: false
}
}
Now, I develop on a feature-branch e.g. feature/ISSUE-123 just for customer 2, so the jobs customer-1/ISSUE-123 and customer-3/ISSUE-123 do not exist. How can I tell Jenkins not to fail in this case?
Consider extracting a new method called safeTriggerJob that wraps the build step with the try-catch block that catches exception thus let the pipeline continue running.
pipeline {
agent any
stages {
stage("Test") {
steps {
safeTriggerJob job: "job2", propagate: true, wait: false
}
}
}
}
void safeTriggerJob(Map params) {
try {
build(params)
} catch (Exception e) {
echo "WARNING: ${e.message}"
}
}
Output:
[Pipeline] Start of Pipeline (hide)
[Pipeline] node
Running on Jenkins in /home/wololock/.jenkins/workspace/sandbox-pipeline
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Test)
[Pipeline] build
[Pipeline] echo
WARNING: No item named job2 found
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
Alternatively, instead of extracting a dedicated method you could add try-catch directly inside steps block, but in this case, you would need to wrap it with script, something like:
pipeline {
agent any
stages {
stage("Test") {
steps {
script {
try {
build job: "job2", propagate: true, wait: false
} catch (Exception e) {
echo "WARNING: ${e.message}"
}
// The next build inside its own try-catch here, etc.
}
}
}
}
}

How to build an pipeline jobs in parallel based on choice parameters values?

In Jenkins, right now i am configuring the pipeline job that can run based on choice parameters values, for each choice values there is an certain jobs need to run in parallel. for example here i need to build Job1 parameter then its only need to build Job1's parallel jobs. but i tried it here its building all the jobs, is there an way to build the jobs based on parameter values?
Choice Parameter
Name: Param
Value: Job1
Job2
import jenkins.model.*
import hudson.model.*
node('') {
String
stage ('Parallel-Job1'){
parallel(Job1: {
stage ('Parallel-test1'){
build job: 'test1', propagate: false
def jobname1 = "test1"
}
}, Job1: {
stage ('Parallel-test2'){
build job: 'test2', propagate: false
def jobname2 = "test2"
}
})
stage ('Parallel-Job2'){
parallel(Job2: {
stage ('Parallel-test3'){
build job: 'test3', propagate: false
def jobname1 = "test3"
}
})
}
}
}
if (param == "Job1") {
stage('Parallel-Job1') {steps ..}
PA: in this case you won't see the skipped pipeline stage on the general view
Or:
stage('conditional stage') {
agent label:'my-node'
when {
expression {
return ${Param} != 'Job1';
}
}
steps {
echo 'foo bar'
}
}

How to run trigger another job outside pipeline

I have three jobs which are in pipeline. Whenever anyone fails due to an internal account lock these have to trigger post-build action.In POst build action i mentioned Trigger when build is failed. I wrote a robot test to unlock the account and I wrote a shell script to call this test.
I am calling this template in both jobs in post-build action and building it on the same node.But what i found is this post build action is kept in pending state and jenkins is triggering downstream project. How to make Jenkins to run post build action when the current job fails?
How to achieve that?
You can play with the seed job's propagate property.
Simple example:
Map jobResults = [:]
pipeline {
agent any
stages {
stage('Build seedjob 1') {
steps {
script {
String seedJobName = 'testjob1'
def seedJob = build job: seedJobName, propagate: false
jobResults[seedJobName] = seedJob.result
echo "Result of ${seedJobName}: ${seedJob.result}"
}
}
}
stage('Build seedjob 2') {
steps {
script {
String seedJobName = 'testjob2'
def seedJob = build job: seedJobName, propagate: false
jobResults[seedJobName] = seedJob.result
echo "Result of ${seedJobName}: ${seedJob.result}"
}
}
}
}
post {
success {
script {
if(jobResults['testjob1'] == 'FAILURE') {
echo "Running another job"
build job: 'another-job1', propagate: true
}
if(jobResults['testjob2'] == 'FAILURE') {
echo "Running another job"
build job: 'another-job2', propagate: true
}
}
}
}
}

Resources