How can I implement a retry option using Jenkins pipeline plugin - jenkins

Currently in build flow plugin we use following approach. This code will retry twice.
programs_create_servers_retry_count=2
retry(programs_create_servers_retry_count) {
build( "create_virtual_servers",j_SL_data_center_local: programs_create_servers_dc_1,j_random_id_local: random_id)
}
How can do the same Jenkins Pipeline plugin?

Check retry method in Jenkins DSL : https://jenkins.io/doc/pipeline/steps/workflow-basic-steps/#retry-retry-the-body-up-to-n-times
Example piece of code will look like that:
stage('stageName') {
try {
...
} catch(error) {
retry(3) {
do smth
}
}
}
Where number 3 is number of attempts to retry.

Related

Can the Jenkins JaCoCo plugin produce multiple reports?

I'm building a JaCoCo report in my Jenkins pipeline:
pipeline {
stages {
stage("One") {
// build something...
jacoco()
}
}
}
This works well with just the one report. But when I add a second one:
pipeline {
stages {
stage("One") {
// build something...
jacoco()
}
stage("Two") {
// build something else...
jacoco()
}
}
}
... then, Jenkins will give me two JaCoCo report buttons in the sidebar, but they will both link to one of the reports. Looks like the second one gets generated, but overwrites the first one.
Can I configure something so both reports are available?

Jenkins text finder

Im currently working with this Jenkins Plugin
https://www.jenkins.io/doc/pipeline/steps/text-finder/
https://plugins.jenkins.io/text-finder/#documentation
I`ve tried to make a pipline and do the following:
pipeline {
agent any
stages {
stage('Hello') {
steps {
findText(textFinders: [textFinder(regexp: '<Started by>', alsoCheckConsoleOutput: true, buildResult: 'UNSTABLE')])
}
}
}
}
This code succeeds, finds the desired text, but buildResult/unstableIfFound parameters seem to never work for me: the job finishes with SUCCESS status. Can anybody help me with that? Or recommend any other option to parse Jenkins console logs and mark the build unstable in some cases ?
Thanks in advance!
Yes you can do it by using manager.log feature parse the jenkins console log output. Please install postbuild plugin and then try
try {
something
} catch (e) {
if ( manager.logContains('.*Add the text message which you want find and mark build unstable.*') {
catchError(buildResult: 'UNSTABLE', stageResult: 'FAILURE') {
error('The build is failed because above test.')
}
}

Jenkins Job DSL get current build result in postBuildScripts shell

I need to get result of current build execution in postBuildScripts shell call in my Jenkins DSL script. Like ${currentBuild.currentResult} in Jenkins pipelines with values: SUCCESS, UNSTABLE, or FAILURE
I've searched through DSL doc but havent found any solution for that.
My code is something like this:
postBuildScripts {
steps {
shell("""echo \$CURRENT_BUILD_STATUS""")
}
}
So how to get this $CURRENT_BUILD_STATUS in easiest way?
Unfortunately the documentation of the PostBuildScript plugin is missing some interesting parts...
job('example') {
publishers {
postBuildScripts {
steps {
shell('echo $BUILD_RESULT')
}
}
}
}

Jenkinsfile declarative syntax for conditional post-build step

I have a Jenkinsfile for a multibranch pipeline like this:
pipeline {
agent any
stages {
// ...
}
post {
failure {
mail to: 'team#example.com',
subject: "Failed Pipeline: ${currentBuild.fullDisplayName}",
body: "Something is wrong with ${env.BUILD_URL}"
}
}
}
I want to only send email for failures on the master branch. Is there a way to make the mail step conditional? Based on the documentation a when directive may only be used inside a stage.
https://jenkins.io/doc/book/pipeline/syntax/#when
https://jenkins.io/doc/pipeline/tour/post/
like you've noted when only works inside a stage. And only valid steps can be used inside the post conditions.
You can still use scripted syntax inside of a script block, and script blocks are a valid step. So you should be able to use if inside a script block to get the desired behavior.
...
post {
failure {
script {
if (env.BRANCH_NAME == 'master') {
... # your code here
}
}
}
}
}
see JENKINS-52689

How to add "single conditional steps" under build section using dsl script

I'm currently trying to develop a DSL script that can create a jenkins job with all required plugins and options.
I think I've almost completed all the section. But, I stuck up under build section where I've to include "conditional steps (single)" under Build.
Actually what I wanted is this
But, what I get is this
Here's the code that I used,
job('Sample_dev') {
steps {
conditionalSteps {
condition {
alwaysRun()
}
}
maven {
goals('install')
}
}
}
You have done few mistakes there:
Using multi-step DSL for achieving single step.
Pushed maven outside context like individual step.
Wrong DSL for Maven Step declaration.
Try following
job('Sample_dev')
{
steps{
singleConditionalBuilder{
condition{
alwaysRun()
}
buildStep {
maven{
targets('install')
name('')
pom('')
properties('')
jvmOptions('')
usePrivateRepository(false)
settings {
standard()
}
globalSettings {
standard()
}
injectBuildVariables(false)
}
}
runner {
fail()
}
}
}
}
The creator has deployed most on this url https://jenkinsci.github.io/job-dsl-plugin. But I would suggest you install in you local instance and access it via http://<your-jenkins-host>:<port> /plugin/job-dsl/api-viewer/index.html as Job DSL support auto generation so there is bright chance that plugin not listed above still has DSL support.

Resources