Jenkins text finder - jenkins

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.')
}
}

Related

"Ambiguous expression could be either a parameterless closure expression or an isolated open code block in jenkins parallel execution throws error

following code is throwing below error.
if(!SkipLanguageComponentTests){
^
WorkflowScript: : Groovy compilation error(s) in script. Error(s): "Ambiguous expression could be either a parameterless closure expression or an isolated open code block;
solution: Add an explicit closure parameter list,
script {
2 errors
`
def SkipLanguageComponentTests = false;
pipeline {
parameters {
booleanParam(name: 'SkipLanguageComponentTests', defaultValue: false, description: 'XYZ')
}
stages {
stage('Checkout Source') {
steps {
checkout scm
}
}
stage("Component & Language Tests"){
steps{
parallel (
"componentTestsTask":{
//component test start
dir("docker"){
sh script: "docker-compose -f blah blah\""
}
// some xyz step here
//component test ends here
},
"integrationTestTasks":{
// language test script starts
if(!SkipLanguageComponentTests){
//run lang test and publish report
} else {
echo "Skip Language Component Tests"
}
// language test script ends
}
)
}
}
}
`
I have tried as per the documentation https://www.jenkins.io/blog/2017/09/25/declarative-1/
I have tried this based on the answer mentioned in : Running stages in parallel with Jenkins workflow / pipeline
stage("Parallel") { steps { parallel ( "firstTask" : { //do some stuff }, "secondTask" : { // Do some other stuff in parallel } ) } }
Can someone help me to resolve this ?
Ok, here is the working version of your pipeline - with proper IF inside:
parameters {
booleanParam(name: 'SkipLanguageComponentTests', defaultValue: false, description: '')
}
agent { label 'master' }
stages {
stage("Component & Language Tests") {
parallel {
stage("componentTestsTask") {
steps {
//component test start
echo "docker"
// some xyz step here
//component test ends here
}
}
stage("integrationTestTasks") {
steps {
script {
// language test script starts
if (!params.SkipLanguageComponentTests) {
echo "not skipped"
//run lang test and publish report
} else {
echo "Skip Language Component Tests"
}
}
}
// language test script ends
}
}
}
}
}
This pipeline is not optimal, use below information to improve it.
Notes:
you are using declarative pipeline so I think it is better to stay with parallel section expressed in declarative way
help is here: Jenkins doc about parallel
there is scripted pipeline as well Jenkins doc about scripted pipeline
as I stated in original answer, you have to use params to refer to input parameter properly
if you are using code you have to enclose it in script section - this is like putting scripted pipeline inside declarative one
IF statement can also be done declaratively: Jenkins doc about WHEN
I recommend not to mix these 2 styles: so if you are using both for some good reason they should be separated from one another as much as possible for example by using function or library inside declarative pipeline - the goal is to have pipeline as clear and readable as possible
You are using input variable, so try to refer as it should be done for input in Jenkins:
if(!params.SkipLanguageComponentTests)

Jenkins User Acceptance code issue in Jenkins pipeline. How to implement multiple acceptance code in a single pipeline

I am trying to create a pipeline in which after my deployment, I will perform functional test and on the basis of that I want to conclude that whether I want to proceed further or not. I used Jenkins "input" feature. I am getting the message to proceed further, but when I click OK the nothing happened, It is stucked there only. And also after first Approval I have send approval step below, after that only I have to release the result.
I am not able to understand how to achieve as I am new to this. The pipeline code is mentioned below:
pipeline {
agent any
tools {
// Install the Maven version configured as "M3" and add it to the path.
maven "mvn"
jdk "jdk8"
}
stages {
stage('SCM Checkout') {
steps {
println "============= SCM Checkout =============="
}
}
stage('Code Inspection'){
steps {
println "============== SonarQube Scanning ======================="
}
}
stage('Build, Package & JUnit'){
steps {
println "============== Build, Package & JUnit ================"
}
}
stage('Deploy'){
steps {
println "============== Deploy and Split Traffic=================="
}
}
stage('Functional & Performance Test'){
steps {
println "=========== Functional and Performance Test ==============="
}
}
stage('A/B Testing'){
input {
message "Functional & Performance Test done. Should we continue?"
ok "OK"
}
steps {
println "=========== A/B Testing ==============="
}
}
stage('Release'){
input {
message "A/B Testing done. Should we continue?"
ok "OK"
}
steps {
println "========= Final Release =================="
}
}
}}
Is there any other way to achieve this? or who can I improve this code to achieve the desired result.
Use the input feature like this:
stage('Release'){
steps {
input message: "A/B Testing done. Should we continue?"
println "========= Final Release =================="
}
}
Make sure you also have the Pipeline: Input Step Plugin (a component of Pipeline Plugin) installed and activated:

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')
}
}
}
}

Jenkins pipeline dockerFingerprintFrom example

I have some troubles about dockerFingerprintFrom on jenkins pipeline.
The code is as follows:
node {
stage("First stage") {
dockerFingerprintFrom([dockerfile: "."])
}
}
It does not work.
I don't even know if this correct.
I would like ask if anyone has a practical example to show me ?

How can I implement a retry option using Jenkins pipeline plugin

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.

Resources