jenkins pipeline readYaml how to use variable to specify key - jenkins

Suppose the yaml file is like this:
#test.yaml
0.6.5.1.0:
module:
- mysql
- zk
0.7.1.0.0:
module:
- java
Now I want to get the module list of specified version, and the version is a variable, I try to write the jenkins pipeline like this:
yamlFile = readYaml file: test.yaml
version = '0.7.1.0.0'
moduleList = yamlFile.get("${version}").get(module)
but this can't work, yamlFile.get("${version}") is a null object, how can I achieve this?

This works for me:
pipeline {
agent any
stages {
stage ('read') {
steps {
script {
def data = readYaml text: """
0.6.5.1.0:
module:
- mysql
- zk
0.7.1.0.0:
module:
- java
"""
version = '0.7.1.0.0'
println data.get(version).get('module')
}
}
}
}
}
The output:
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on server in /home/user/workspace/task
[Pipeline] {
[Pipeline] stage
[Pipeline] { (read)
[Pipeline] script
[Pipeline] {
[Pipeline] readYaml
[Pipeline] echo
[java]
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

Related

Failed to assign sh output to a variable when sh output contains opening bracket ( | Jenkins Declarative Pipeline

I am trying in one of the steps to write some data in file, and in step after that to read that data using and assign it to a variable. This is my declarative Jenkins pipeline:
pipeline {
agent {label 'build-slave-aws'}
stages {
stage('Notify about start') {
steps {
sh 'echo "Some fatct with brackets ()" > /tmp/facts.issues'
}
}
stage('Gather the facts') {
steps {
script {
factsIssues = sh( script: "cat /tmp/facts.issues", returnStdout: true )
}
sh "echo these are facts: ${factsIssues}"
}
}
}
}
The output of this run is following:
Started by user 123
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on i-000df827977fd5175 in /workspace/workspace/test
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Notify about start)
[Pipeline] sh
+ echo 'Some fatct with brackets ()'
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Gather the facts)
[Pipeline] script
[Pipeline] {
[Pipeline] sh
+ cat /tmp/facts.issues
[Pipeline] }
[Pipeline] // script
[Pipeline] sh
/workspace/workspace/test#tmp/durable-2a1f8cdf/script.sh: line 1: syntax error near unexpected token `('
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: script returned exit code 2
Finished: FAILURE
Any other text that doesn't contain ( simply work just fine. Do you have any suggestion on how I can write some data in file with ( and reading that back to a variable?
You are missing quotes around the argument of the echo command within the last sh script.
Fix:
sh "echo 'these are facts: ${factsIssues}'"

How to use jenkins use environment variables in parameters in a Pipeline

I'm trying to use "JOB_BASE_NAME" jenkins environmental variable in a parameter's path in a pipeline script that gets set will building the project.
example: string(defaultValue: "/abc/test/workspace/test_${JOB_BASE_NAME}/sample", description: 'test', name: 'HOME')
but while executing the ${JOB_BASE_NAME} is not getting replaced by the value(jenkins job name). I'm unsure if I'm setting the jenkins environmental variable in the path of the parameter correctly.
thank you!
I have replicated your use case and it works for me. This is the section of code
node {
stage ('test') {
sh "echo ${HOME}"
}
}
and this is the output - (my Job name was stackoverflow)
[Pipeline] { (hide)
[Pipeline] stage
[Pipeline] { (test)
[Pipeline] sh
+ echo /abc/test/workspace/test_stackoverflow/sample
/abc/test/workspace/test_stackoverflow/sample
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
Check the picture of how I set the String parameter.

Aborted jenkins pipeline job continues running later stages

I'm on the latest version of Jenkins and the pipeline plugins, and I have a jenkins declarative pipeline job configured as follows:
pipeline {
agent {
label 'default'
}
stages {
stage('Prepare') {
steps {
dir('foo') {
git ...
}
}
}
stage('Temp1') {
steps {
sh script: 'dotnet run ...'
echo 'Temp1'
}
}
stage('Temp2') {
steps {
echo 'Temp2'
}
}
}
}
If I abort the build during the sh script in the Temp1 stage, my expectation is that neither the rest of the Temp1 stage nor the Temp2 stage would run. However, when I abort it, it stops the sh script, then runs the rest of Temp1 stage and continues on to run the Temp2 stage as well!
Here is the log:
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] node
Running on ...
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Prepare)
[Pipeline] dir
Running in ...
[Pipeline] {
[Pipeline] git
Fetching changes from the remote Git repository
...
[Pipeline] }
[Pipeline] // dir
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Temp1)
[Pipeline] sh
+ dotnet run ...
Aborted by ...
Sending interrupt signal to process
[Pipeline] echo
Temp1
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Temp2)
[Pipeline] echo
Temp2
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: ABORTED
How do I get the build to actually abort?
This ended up being a dotnet issue, where it returns 0 as the exit code when killed by SIGTERM. https://github.com/dotnet/coreclr/issues/21243 was resolved several days ago but has not been released yet.
As a workaround, you can do something like this:
public static int Main(string[] args)
{
Environment.ExitCode = 1;
// Do stuff
return 0;
}
If Main completes successfully, it will return an exit code of 0. If it receives a SIGTERM, it will return the value in Environment.ExitCode.

Getting Jenkinsfile error - command not found

What is wrong with this Jenkins file? I am new to it but I don't get what am I doing wrong
pipeline {
agent any
stages {
stage('Test') {
steps {
dir ('/var/lib/jenkins/workspace/pipleline_2') {
}
}
}
}
post {
always {
sh 'hello2.sh'
}
failure {
mail(from: "heenashree2010#gmail.com",
to: "qshoretechnologies#gmail.com",
subject: "That build passed.",
body: "Nothing to see here")
}
}
}
I am getting below error. hello2.sh exists in the directory which I have specified but I am not able to execute it. I also tried sh('hello2.sh') but it didn't work for me. What am I doing wrong?
Started by user qshore
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] node
Running on Jenkins in /var/lib/jenkins/workspace/pipleline_2
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Test)
[Pipeline] dir
Running in /var/lib/jenkins/workspace/pipleline_2
[Pipeline] {
[Pipeline] }
[Pipeline] // dir
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Declarative: Post Actions)
[Pipeline] sh
[pipleline_2] Running shell script
+ hello2.sh
/var/lib/jenkins/workspace/pipleline_2#tmp/durable-dbcba8b2/script.sh: line 2: hello2.sh: command not found
[Pipeline] mail
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: script returned exit code 127
Finished: FAILURE
The script called hello2.sh is not found. Make sure that it is included in the repo that you're checking out.

trigger parameterized build doesnt find buildpath in jenkins pipeline job

My code dynamically creates a .groovy file which triggers parameterized build inside a parallel step:
def executeParallelBuilds(){
try {
parallel(
build1BUILD: {
def build1BUILD = build job: 'TA/test1', parameters: [string(name: "CPNUM_PARAM", value: 1.141)]
},
build2BUILD: {
def build2BUILD = build job: 'TA/test2', parameters: [string(name: "CPNUM_PARAM", value: 1.141)]
},
failFast: false
)
} catch (e) {
echo "An error ocurred while building"
currentBuild.result="UNSTABLE"
}
}
return this;
Now, I load and execute the groovy file with:
node('master'){
def executeGroovyFile = load buildFilePath
executeGroovyFile.executeParallelBuilds()
}
But it seems that my pipeline cant find the Buildjobs by path.
[Pipeline] }
[Pipeline] // node
[Pipeline] node
Running on master in C:\DevApps\Jenkins\workspace\TA\pipeline_1.0_TEMPLATE
[Pipeline] {
[Pipeline] load
[Pipeline] { (D:\BuildResults_tmp\TA\MBE3\\buildString.groovy)
[Pipeline] }
[Pipeline] // load
[Pipeline] parallel
[Pipeline] [build1BUILD] { (Branch: build1BUILD)
[Pipeline] [build2BUILD] { (Branch: build2BUILD)
[Pipeline] [build1BUILD] build
[Pipeline] [build1BUILD] }
[build1BUILD] Failed in branch build1BUILD
[Pipeline] [build2BUILD] build
[Pipeline] [build2BUILD] }
[build2BUILD] Failed in branch build2BUILD
[Pipeline] // parallel
[Pipeline] echo
An error ocurred while building
[Pipeline] }
What am I doing wrong? I load and execute the .groovy file on my master so that the pipeline should be able to find the other jobs. (Without node declaration I am not able to load and execute)
EDIT: What confuses me is, that I don't get the following error:
No parameterized job named some-downtream-job-name found
There was a problem with the build call.
I saved the whole code as String to a .groovy file. This gave me some struggling with the right notation. (quotes and double qoutes)
After calling my script as:
def build1BUILD = build job: BuildJobNameList[i], parameters: [string(name: "CPNUM_PARAM", value: 1.141)]
everything works fine

Resources