Jenkins trigger another job - jenkins

i have a pipeline that triggers the same job if it fails. but when the second job is triggered, the first pipeline still stay opened till the second one success or fails, i would like to know if i can close the pipeline after the trigger was made for the second one.
pipeline {
agent any
stages {
stage('test') {
steps {
script {
input message: 'Proceed?', ok: 'Yes', submitter: 'admin'
}
echo "helloworld"
}
post {
aborted{
script{
retry(1) {
input "Retry the job ?"
build(job: 'pipelines/testCS')
}
}
}
success {
script{
sh 'echo "continue"'
}
}
}
}
stage('deploy'){
steps{
sh 'echo "deploy"'
}
}
}
post {
aborted {
echo "pipeline has been aborted"
}
}
}

Simply pass wait: false for the build step:
build(job: 'pipelines/testCS', wait: false)
See documentation for all parameters.

Related

Execute code block if stage fails, but proceed with other stages

We would like to send an email if a stage fails. The stage should be marked as unstable, but overall build result should not be affected by the result of this stage. This is the code snippet that we are using:
stage("Stage 1")
{
catchError(buildResult: 'SUCCESS', stageResult: 'UNSTABLE')
{
sh 'scriptThatCanExitWithStatus1.sh'
}
}
It works fine, but we are not able to define the code that should be executed if shell script fails. How can we execute custom error handling block of code if scriptThatCanExitWithStatus1.sh fails (ex. send an email to sys admin)?
This is how I solved the problem:
stage("Stage 1")
{
success = false
catchError(buildResult: 'SUCCESS', stageResult: 'UNSTABLE')
{
sh 'scriptThatCanExitWithStatus1.sh'
success = true
}
if (!success) {
// send mail
}
}
Another way is to use try catch in a script block and re-throw the error after performing the error handling. See example below:
pipeline {
agent any
stages {
stage('1') {
steps {
sh 'exit 0'
}
}
stage('2') {
steps {
catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
script {
try {
sh "exit 1"
} catch (e) {
echo 'send email'
throw e
}
}
}
}
}
stage('3') {
steps {
sh 'exit 0'
}
}
}
}

Display Jenkins pipeline stage as failed without failing the whole job

Could you please tell me what I am doing wrong. I would like to failure a Stage1. Stage2 should be green and that case, and I would like to have whole build Success. What I am doing wrong?
pipeline{
agent none
stages{
stage ("Stage1") {
steps {
script{
catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
stage ("stege1") {
def seekAndDestroy = build job: 'SeekAndDestroy' // it is going FAILURE
}
}
stage ("Stege2") {
sh "exit 0"
}
}
}
}
}
}
catchError is something similar to try/catch block, so you need to catch error of the code you are executing. For details - see documentation: https://www.jenkins.io/doc/pipeline/steps/workflow-basic-steps/#catcherror-catch-error-and-set-build-result-to-failure
This works:
pipeline{
agent { label 'jdk11' }
stages {
stage ("stage 1") {
steps {
catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
sh "exit 1"
}
}
}
stage ("stage 2") {
steps {
sh "exit 0"
}
}
}
}
And the result is:

Jenkins - Mark all stages as aborted

How to mark all stages as aborted instead of error when stopping early? I'm using error to stop all stages if the commit message doesn't match. But I want all the stages to be aborted instead of failed.
stages {
stage('Stage 1'){
steps{
script {
RESULT_LOG = sh(returnStdout: true, script: 'git log -1 --pretty=%B')
if (!RESULT_LOG.contains("[ci build]")) {
echo "Commit message is: ${RESULT_LOG}"
currentBuild.result = 'ABORTED'
error('Stopping early…')
}
}
}
}
stage('Stage 2'){
steps{
...
}
}
}
You can try this
if (!RESULT_LOG.contains("[ci build]")) {
catchError(buildResult: 'FAILURE', stageResult: 'ABORTED') {
echo "Commit message is: ${RESULT_LOG}"
error('Stopping early…')
}
}

Jenkins pipeline execute job and get status

pipeline {
agent { label 'master' }
stages {
stage('test') {
steps {
script {
def job_exec_details = build job: 'build_job'
if (job_exec_details.status == 'Failed') {
echo "JOB FAILED"
}
}
}
}
}
}
I have a pipeline that executing build job, how can I get Job result in jenkins pipeline ?
It should be getResult() and status should be FAILURE not Failed.
so your whole code should be like this
pipeline {
agent { label 'master' }
stages {
stage('test') {
steps {
script {
def job_exec_details = build job: 'build_job', propagate: false, wait: true // Here wait: true means current running job will wait for build_job to finish.
if (job_exec_details.getResult() == 'FAILURE') {
echo "JOB FAILED"
}
}
}
}
}
}
Where is a second way of getting results:
pipeline {
agent { label 'master' }
stages {
stage('test') {
steps {
build(job: 'build_job', propagate: true, wait: true)
}
}
}
post {
success {
echo 'Job result is success'
}
failure {
echo 'Job result is failure'
}
}
}
}
You can read more about 'build' step here

Jenkins multipe post sections per stage

I have two question for usage of post section in Jenkins pipeline
1.Can we use multiple post section per stage in Jenkins declarative Pipeline?
2.Can we run sh command in post section?
pipeline{
stages{
stage("....") {
steps {
script {
....
}
}
post {
failure{
sh "....."
}
}
stage("Second stage") {
when {
expression { /*condition */ }
}
steps {
script{
....
}
post {
always {
script {
sh "..."
}
}
}
}
}
You can find information in https://jenkins.io/doc/book/pipeline/syntax/#post
pipeline {
agent any
stages {
stage('Example') {
steps {
echo 'Hello World'
}
}
}
post {
success {
sh label: 'success', script: 'ls'
}
failure {
sh label: 'failure', script: 'ls'
}
aborted {
sh label: 'aborted', script: 'ls'
}
}
}
You can use Post steps each Stage, but pipeline will stop on first failure. In example below, if Stage 1 fail Stage 2 will be skipped. Post after all stages will always executed.
pipeline{
stages{
stage("Stage 1") {
steps {
catchError(message: 'catch failure') {
script {
sh "echo stage 1"
}
}
}
post {
always {
sh "echo post stage 1"
}
}
}
stage("Stage 2") {
when {
expression { /*condition */ }
}
steps {
script{
sh "echo stage 2"
}
}
post {
always {
script {
sh "echo post stage 2"
}
}
}
}
}
post {
always {
sh "echo post after all stages"
}
}
}

Resources