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
}
}
}
}
}
Related
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’)
}
}
}
}
}
I have a pipeline with some information detailed behind
pipeline {
parameters {
booleanParam(name: 'RERUN', defaultValue: false, description: 'Run Failed Tests')
}
stage('Run tests ') {
steps {
runTest()
}
}
post {
always {
reRun()
}
}
}
def reRun() {
if ("SUCCESS".equals(currentBuild.result)) {
echo "LAST BUILD WAS SUCCESS"
} else if ("UNSTABLE".equals(currentBuild.result)) {
echo "LAST BUILD WAS UNSTABLE"
}
}
but I want that after the stage "Run tests" execute, if some tests fail I want to re-run the pipeline with parameters RERUN true instead of false. How can I replay via script instead of using plugins ?
I wasn't able to find how to re-run using parameters on my search, if someone could help me I will be grateful.
First of you can use the post step to determine if the job was unstable:
post{
unstable{
echo "..."
}
}
Then you could just trigger the same job with the new parameter like this:
build job: 'your-project-name', parameters: [[$class: 'BooleanParameterValue', name: 'RERUN', value: Boolean.valueOf("true")]]
I have a Jenkins pipeline which, among multiple steps should have a final step that should be executed regardless of the status of previous steps. For that to happen, I've tried using post section which looks like this:
pipeline {
agent {
label 'master'
}
stages {
stage('Stage 1') {
steps {
build job: 'stage 1 job', parameters: [
...
]
}
}
stage('Stage 2') {
steps {
build job: 'stage 2 job', parameters: [
...
]
}
}
}
post {
always {
build job: "cleanup", parameters: [
...
]
}
}
}
However, I'm getting following error when trying to execute something like this:
No such DSL method '$' found among steps
Question: Is it even possible to use build job inside post action? If not, what would be good alternative to achieve that "cleanup" job is always executed at the end (regardless of the status of stages above)
Yes, it is possible to use build a job inside post action. Here is the pipeline script:
pipeline {
agent any
stages {
stage('1') {
steps {
script {
echo "Hello"
}
}
}
}
post {
always {
build job: 'schedule-job', parameters: [string(name: 'PLATFORM', value: 'Windows')]
}
}
}
In the above example, I have schedule-job which accepts parameters PLATFORM and it will Always run, regardless of build status
Here is the output:
I am trying to create a pipeline in Jenkins which triggers same job multiple times in different node(agents).
I have "Create_Invoice" job Jenkins, configured : (Execute Concurrent builds if necessary)
If I click on Build 10 times it will run 10 times in different (available) agents/nodes.
Instead of me clicking 10 times, I want to create a parallel pipeline.
I created something like below - it triggers the job but only once.
What Am I missing or is it even possible to trigger same test more than once at the same time from pipeline?
Thank you in advance
node {
def notifyBuild = { String buildStatus ->
// build status of null means successful
buildStatus = buildStatus ?: 'SUCCESSFUL'
// Default values
def tasks = [:]
try {
tasks["Test-1"] = {
stage ("Test-1") {
b = build(job: "Create_Invoice", propagate: false).result
}
}
tasks["Test-2"] = {
stage ("Test-2") {
b = build(job: "Create_Invoice", propagate: false).result
}
}
parallel tasks
} catch (e) {
// If there was an exception thrown, the build failed
currentBuild.result = "FAILED"
throw e
}
finally {
notifyBuild(currentBuild.result)
}
}
}
I had the same problem and solved it by passing different parameters to the same job. You should add parameters to your build steps, although you obviously don't need them. For example, I added a string parameter.
tasks["Test-1"] = {
stage ("Test-1") {
b = build(job: "Create_Invoice", parameters: [string(name: "PARAM", value: "1")], propagate: false).result
}
}
tasks["Test-2"] = {
stage ("Test-2") {
b = build(job: "Create_Invoice", parameters: [string(name: "PARAM", value: "2")], propagate: false).result
}
}
As long as the same parameters or no parameters are passed to the same job, the job is only tirggered once.
See also this Jenkins issue, it describes the same problem:
https://issues.jenkins.io/browse/JENKINS-55748
I think you have to switch to Declarative pipeline instead of Scripted pipeline.
Declarative pipeline has parallel stages support which is your goal:
https://www.jenkins.io/blog/2017/09/25/declarative-1/
This example will grab the available agent from the Jenkins and iterate and run the pipeline in all the active agents.
with this approach, you no need to invoke this job from an upstream job many time to build on a different agent. This Job itself will manage everything and run all the stages define in all the online node.
jenkins.model.Jenkins.instance.computers.each { c ->
if(c.node.toComputer().online) {
node(c.node.labelString) {
stage('steps-one') {
echo "Hello from Steps One"
}
stage('stage-two') {
echo "Hello from Steps Two"
}
}
} else {
println "SKIP ${c.node.labelString} Because the status is : ${c.node.toComputer().online} "
}
}
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'
}
}