jenkins pipeline when is evaluating to false though file exits - jenkins

any hint why WHEN expression is evaluating to FALSE ? I tried this solution but no luck
stage('Code Coverage Tests') {
steps {
container('main') {
catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
sh '''
pwd
echo testinfo > /coverage-data/_coverage_report.dat
ls -ltr /coverage-data/
'''.stripIndent()
}
}
}
}
stage('Code Coverage Scan') {
when { expression { return fileExists ('/coverage-data/_coverage_report.dat') } }
steps {
output shows file exits.
+ ls -ltr /coverage-data/
total 4
-rw-r--r-- 1 jenkins jenkins 9 Dec 18 18:13 _coverage_report.dat
+ pwd
[Pipeline] stage
[Pipeline] { (Code Coverage Scan)
[Pipeline] fileExists
Stage "Code Coverage Scan" skipped due to when conditional

Related

jenkins pipeline setting up a parallel build and stopping tasks

Need help setting up a pipeline on jenkins.
It is necessary to run tests and collect logs in parallel, it worked out, but now there is another problem, the collection of logs is not completed. Maybe there is some method how to stop a task after another task is completed?
stage('Smoke Run') {
steps {
parallel(
first: {
catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
sh '$PYTHON -m pytest --testit android_tv/tests/smoke_run/ --clean-alluredir --alluredir=/Users/jenkins/allure-report/android-tv'
}
},
second: {
sh "$ADB logcat -c"
sh "$ADB logcat -> ~/jenkins/workspace/Android_TV_Smoke_Run/android_tv/tests/smoke_run/logs_tv/log.log"
}
)
}
}
found a solution
steps {
script {
stop = false
parallel(
first: {
catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
sh '''
set +e
$PYTHON -m pytest --testit android_tv/tests/smoke_run/ --clean-alluredir --alluredir=/Users/jenkins/allure-report/android-tv
set -e
'''.stripIndent()
stop = true
}
},
second: {
while (!stop){
sleep 10
}
sh '''pgrep adb logcat | xargs kill'''
sh '''echo "Finish writing logs"'''
}
)
}
}
}```

Running multiple steps in sequenze in one parallel block In Jenkinsfile

I'm trying to optimize my pipeline. I'm using the pipeline to generate and deploy some docs. At the end I clear my document root and write the newly generated docs into the document root. I'm doing this for several stages in parallel.
o-----o-----o--+--o--+---+--o--+-----o
| | | |
+--o--+ +--o--+
| | | |
+--o--+ +--o--+
this is the pipeline exerpt for the parallel stages
stage("clear nfs directory") {
steps {
parallel(
test: {
sh "rm -rf /mnt/nfs/test/docs/$pipelineParams.groupname"
sh "mkdir /mnt/nfs/test/docs/$pipelineParams.groupname"
},
rele: {
sh "rm -rf /mnt/nfs/rele/docs/$pipelineParams.groupname"
sh "mkdir /mnt/nfs/rele/docs/$pipelineParams.groupname"
},
prod: {
sh "rm -rf /mnt/nfs/prod/docs/$pipelineParams.groupname"
sh "mkdir /mnt/nfs/prod/docs/$pipelineParams.groupname"
}
)
}
}
stage("copy generated docs to nfs directory") {
steps {
parallel(
test: {
dir("target/public") {
sh "cp -r * /mnt/nfs/test/docs/$pipelineParams.groupname"
}
},
rele: {
dir("target/public") {
sh "cp -r * /mnt/nfs/rele/docs/$pipelineParams.groupname"
}
},
prod: {
dir("target/public") {
sh "cp -r * /mnt/nfs/prod/docs/$pipelineParams.groupname"
}
}
)
}
}
Since clear and write should depend on each other I would like to refactor the pipeline into a more sequential design (running multiple steps in sequence in less parallel steps)
o-----o-----o--+--o---o--+-----o
| |
+--o---o--+
| |
+--o---o--+
I'm not sure how to run multiple steps in the same parallel block ... can anyone give me a hint? Thanks guys
Please see below reference which will allow you to run multiple steps in same parallel block.
You would need to use sequential stages which will give below output :
o-----o-----o--+--o---o--+-----o
| |
+--o---o--+
| |
+--o---o--+
pipeline {
agent { label 'master' }
stages {
stage('Build and Test') {
parallel {
stage("Build and Test Linux") {
stages {
stage("Build (Linux)") {
agent any
steps {
echo "Inside for loop 1"
}
}
stage("Test (Linux)") {
agent any
steps {
echo "Inside for loop 2"
}
}
}
}
stage("Build and Test Windows") {
stages {
stage("Build (Windows)") {
agent any
steps {
echo "Inside for loop 3"
}
}
stage("Test (Windows)") {
agent any
steps {
echo "Inside for loop 4"
}
}
}
}
}
}
}
}
For more info see:-
https://www.jenkins.io/blog/2018/07/02/whats-new-declarative-piepline-13x-sequential-stages/
Below link gives reference example:
https://issues.jenkins.io/browse/JENKINS-55438

jenkinsfile not passing env to sh

I am trying to set environment variable in Jenkinsfile following way,
pipeline {
agent { label 'slave1' }
stages {
stage ('Build') {
steps {
script {
BUILD_VERSION = sh (
script: 'python get_firmware_version.py',
returnStdout: true
).trim()
}
echo "${BUILD_VERSION}"
withCredentials([file(credentialsId: 'image-sign', variable: 'IMAGE_SIGN')]) {
dir('firmware/') {
echo "${BUILD_VERSION}"
sh '''
echo "Building"
echo "${BUILD_VERSION}"
echo "${env.BUILD_VERSION}"
'''
}
}
}
}
}
post {
failure {
script {
echo "Pipeline Failed"
}
}
}
}
But its failing with following error Bad substitution
[Pipeline] echo
0_2_0
[Pipeline] sh
+ echo Building
Building
/home/jenkins/jenkins_slave/workspace/Firmware/Branch/firmware#tmp/durable-54e04481/script.sh: 3: /home/jenkins/jenkins_slave/workspace/Firmware/Branch/firmware#tmp/durable-54e04481/script.sh: Bad substitution
Why I can't set ENV Var and use it in sh step ?
This is Jenkins thing I think. When you use the sh block with '; it will not have access to things like environment variables. Try using the " instead. That should work
sh """
echo "Building"
echo "${env.BUILD_VERSION}"
echo "${env}"
"""
Jenkins should recognise the shell block and escape the " within the """ automatically.
pipeline {
agent { label 'slave1' }
stages {
stage ('Build') {
steps {
script {
BUILD_VERSION = sh (
script: 'python get_firmware_version.py',
returnStdout: true
).trim()
}
echo "${BUILD_VERSION}"
withCredentials([file(credentialsId: 'image-sign', variable: 'IMAGE_SIGN')]) {
dir('firmware/') {
echo "${BUILD_VERSION}"
sh """
echo "Building"
echo "${BUILD_VERSION}"
echo "${env.BUILD_VERSION}"
"""
}
}
}
}
}
post {
failure {
script {
echo "Pipeline Failed"
}
}
}
}
My test case
pipeline {
agent {
node {
label 'devops-jenkins-slave'
}
}
options {
timestamps()
}
stages {
stage('Setup'){
steps {
dir("${WORKSPACE}/"){
script {
BUILD_VERSION = "1"
}
sh """
echo "${BUILD_VERSION}"
"""
}
}
}
}
post {
always {
dir("${WORKSPACE}/"){
deleteDir()
}
}
}
}
Result
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] node
Running on devops-jenkins-slave in /home/jenkins/workspace/Samples/Test
[Pipeline] {
[Pipeline] timestamps
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Setup)
[Pipeline] dir
22:09:55 Running in /home/jenkins/workspace/Samples/Test
[Pipeline] {
[Pipeline] script
[Pipeline] {
[Pipeline] }
[Pipeline] // script
[Pipeline] sh
22:09:56 [Test] Running shell script
22:09:56 + echo 1
22:09:56 1
[Pipeline] }
[Pipeline] // dir
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Declarative: Post Actions)
[Pipeline] dir
22:09:56 Running in /home/jenkins/workspace/Samples/Test
[Pipeline] {
[Pipeline] deleteDir
[Pipeline] }
[Pipeline] // dir
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // timestamps
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

JenkinsFile pipeline loop

Does anyone have a pipeline{} example with a loop?
I'm trying to "condense" a long and repetitive JenkinsFile by using loops to run the same logic on multiple hosts.
... but ... I'm not getting loops right in my piepline{} JenkinsFile.
Specifically, I'm doing this ...
pipeline {
agent { label 'buildhost' }
stages {
stage("checks") {
parallel {
// ['mac-amd64', 'linux-arm', 'win-amd64', 'linux-x86', 'linux-armv8', 'linux-amd64' ].each {
['mac-amd64', 'linux-armv8', 'linux-amd64'].each {
host ->
stage("checks / ${host}") {
agent { label "buildhost-${host}" }
steps {
dir ('peterlavalle.sbt/') {
// combining these seemed to launch the SBT server which makes Jenkins hang
sh 'java -Dsbt.log.noformat=true -jar ./sbt-launch.jar clean'
sh 'java -Dsbt.log.noformat=true -jar ./sbt-launch.jar test'
sh 'java -Dsbt.log.noformat=true -jar ./sbt-launch.jar publish'
}
['cgc3.gradle/', 'kanobi.sbt/'].each {
gradle ->
dir (path) {
sh 'chmod +=rwx ./gradlew'
sh './gradlew --stacktrace --console=plain clean'
sh './gradlew --stacktrace --console=plain check'
sh './gradlew --stacktrace --console=plain publish'
}
}
dir ("coffeeskript.cgc/") {
sh 'chmod +=rwx ./gradlew'
sh './gradlew --stacktrace --console=plain check'
}
}
}
}
}
}
}
}
}
... and Jenkins says "only stages here!" or somesuch.
...
...
...
Running in Durability level: MAX_SURVIVABILITY
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 47: Expected a stage # line 47, column 11.
['mac-amd64', 'linux-armv8', 'linux-amd64'].each {
^
WorkflowScript: 44: Expected one of "steps", "stages", or "parallel" for stage "checks" # line 44, column 4.
stage("checks") {
^
...
...
...

JenkinsFile for Sonar Quality Gates doesn't work

I have read the documentation of Jenkins and Sonar to write and configure my pipeline and everything works except the Sonar Quality Gate which remains for the response from the Sonar without success. This is my code :
pipeline {
agent master
tools {
jdk 'JAVA'
maven 'mvn_3_5_2'
}
stages {
stage('test Java & Maven installation') {
steps {
sh 'java -version'
sh 'which java'
sh 'mvn -version'
sh 'which mvn'
}
}
stage('Clean stage') {
steps {
sh 'mvn -f project/pom.xml clean'
}
}
stage("build & SonarQube analysis") {
node {
withSonarQubeEnv('Sonar') {
sh 'mvn -f project/pom.xml org.sonarsource.scanner.maven:sonar-maven-plugin:3.2:sonar'
}
}
}
stage("Quality Gate"){
timeout(time: 2, unit: 'MINUTES') {
def qg = waitForQualityGate()
if (qg.status != 'OK') {
error "Pipeline aborted due to quality gate failure: ${qg.status}"
}
}
}
stage('Test stage') {
steps {
sh 'mvn -f project/pom.xml test'
}
}
stage('Build stage') {
steps {
sh 'mvn -f project/pom.xml install'
}
}
stage('Deploy stage') {
steps {
echo 'Deployment...'
// sh 'mvn -f project/pom.xml appengine:deploy -Dapp.deploy.project=acn-shelf-check -Dapp.deploy.version=v1'
}
}
}
}
In the stage "Quality Gate" I never received the result. I have configured the webhook on the sonar with correct URL of Jenkins.
Do I miss any step?

Resources