Jenkins Declarative Piplines- Script is unable to pickup parameter as varible - jenkins

I am New to Jenkins. Trying to create one basic Pipeline which is using choicebased parameters. Following is my script.
Code ----
pipeline{
agent {
label 'agent'
}
parameters {
choice choices: ['John', 'Stacy'], description: 'Choose one', name: 'Person'
}
stages {
stage('Print') {
steps {
echo "Hello ${params.Person}"
sh """if (${params.Person} = "John")
then
echo "Person is male."
else
echo "Person is female."
fi"""
}
}
}
}
Now my build complete successfully regardless of whatever option I choose. It always display result "Person is female.
Following is result of one of my build.
Started by user ****
[Pipeline] Start of Pipeline
[Pipeline] node
Running on agent in
/home/temp/jenkins_agent/workspace/ChoiceBased PL
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Print)
[Pipeline] echo
Hello John
[Pipeline] sh
+ John = John
/home/temp/jenkins_agent/workspace/ChoiceBased PL#tmp/durable-
b7e98c46/script.sh: 1: John: not found
+ echo Person is female.
Person is female.
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: Success
Please suggest what I am missing ?

I would change this just to be in Groovy rather than doing the compare in sh
stage('Print') {
steps {
echo "Hello ${params.Person}"
script {
if (params.Person == "John") {
echo "Person is male."
} else {
echo "Person is female."
}
}
}
}
Then when you choose Stacey you will get
[Pipeline] echo
Hello Stacy
[Pipeline] script
[Pipeline] {
[Pipeline] echo
Person is female.
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

Related

How to change the whole workspace of a project in Jenkins

With customWorkspace option in pipeline block, the job will start up at the jenkins default workspcace, logs below
pipeline {
agent {
node {
label ''
customWorkspace '/Users/dabaoji/change_dir_test/test'
}
}
stages {
stage ('pre') {
steps {
sh 'echo $pwd'
}
}
}
}
Started by user admin
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /Users/dabaoji/.jenkins-lts/workspace/test
[Pipeline] {
[Pipeline] ws
Running in /Users/dabaoji/change_dir_test/test
[Pipeline] {
[Pipeline] stage
[Pipeline] { (pre)
[Pipeline] sh
+ echo
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // ws
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
How to make the job run at the custom workspace when starting up?
With freestyle project it is possible. https://youtu.be/elc7oGS7BTg?t=302

Jenkins 2 dimensional array parallel stages

I am trying to implement Jenkins 2D parallel stages.
This is the example that I am testing.
I expect 12 different stages which are A1/B1/C1/A2/B3/C2/A3/B3/C3/A4/B4/C4 and they are executing 3 parallel builds and each build has 4 serial build stages.
build_list = ['B1', 'B2', 'B3']
l1 = ['A1', 'A2', 'A3', 'A4']
l2 = ['B1', 'B2', 'B3', 'B4']
l3 = ['C1', 'C2', 'C3', 'C4']
array_build_list = [ l1, l2, l3 ]
index = 1
index = -index
def parallelStagesMap = build_list.collectEntries {
index++
["${it}" : generateStage(it, index)]
}
def generateStage(job, index) {
return {
stage("${job}") {
echo "This is ${job}"
script {
build_project_list = array_build_list[index]
echo "build_project_list = ${build_project_list}"
for(int i=0; i<build_project_list.size(); i++) {
stage(build_project_list[i]) {
echo "${build_project_list[i]}"
}
}
}
}
}
}
pipeline {
agent any
stages {
stage("start") {
steps {
echo "start"
}
}
stage('parallel stage') {
steps {
script {
echo "Inside script"
parallel parallelStagesMap
}
}
}
}
}
But this is the build result:
Result
The result stages are C1/C1/C1/C2/C2/C2/C3/C3/C3/C4/C4/C4 which means only the last parallel run is repeated 3 times.
Blue ocean Result
Do you have any suggestions or comments?
This is the console output:
[Pipeline] Start of Pipeline
[Pipeline] echo
After value B1 at index 0
[Pipeline] echo
After value B2 at index 1
[Pipeline] echo
After value B3 at index 2
[Pipeline] node
Running on Jenkins in /Users/.jenkins/workspace/2D_array_test
[Pipeline] {
[Pipeline] stage
[Pipeline] { (start)
[Pipeline] echo
start
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (parallel stage)
[Pipeline] script
[Pipeline] {
[Pipeline] echo
Inside script
[Pipeline] parallel
[Pipeline] { (Branch: B1)
[Pipeline] { (Branch: B2)
[Pipeline] { (Branch: B3)
[Pipeline] stage
[Pipeline] { (B1)
[Pipeline] stage
[Pipeline] { (B2)
[Pipeline] stage
[Pipeline] { (B3)
[Pipeline] echo
This is B1
[Pipeline] sh
[Pipeline] echo
This is B2
[Pipeline] sh
[Pipeline] echo
This is B3
[Pipeline] sh
[Pipeline] script
[Pipeline] {
[Pipeline] script
[Pipeline] {
[Pipeline] script
[Pipeline] {
[Pipeline] echo
build_project_list = [A1, A2, A3, A4]
[Pipeline] echo
build_project_list = [B1, B2, B3, B4]
[Pipeline] echo
build_project_list = [C1, C2, C3, C4]
[Pipeline] stage
[Pipeline] { (C1)
[Pipeline] stage
[Pipeline] { (C1)
[Pipeline] stage
[Pipeline] { (C1)
[Pipeline] echo
C1
[Pipeline] }
[Pipeline] echo
C1
[Pipeline] }
[Pipeline] echo
C1
[Pipeline] }
[Pipeline] // stage
[Pipeline] // stage
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (C2)
[Pipeline] stage
[Pipeline] { (C2)
[Pipeline] stage
[Pipeline] { (C2)
[Pipeline] echo
C2
[Pipeline] }
[Pipeline] echo
C2
[Pipeline] }
[Pipeline] echo
C2
[Pipeline] }
[Pipeline] // stage
[Pipeline] // stage
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (C3)
[Pipeline] stage
[Pipeline] { (C3)
[Pipeline] stage
[Pipeline] { (C3)
[Pipeline] echo
C3
[Pipeline] }
[Pipeline] echo
C3
[Pipeline] }
[Pipeline] echo
C3
[Pipeline] }
[Pipeline] // stage
[Pipeline] // stage
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (C4)
[Pipeline] stage
[Pipeline] { (C4)
[Pipeline] stage
[Pipeline] { (C4)
[Pipeline] echo
C4
[Pipeline] }
[Pipeline] echo
C4
[Pipeline] }
[Pipeline] echo
C4
[Pipeline] }
[Pipeline] // stage
[Pipeline] // stage
[Pipeline] // stage
[Pipeline] }
[Pipeline] }
[Pipeline] }
[Pipeline] // script
[Pipeline] // script
[Pipeline] // script
[Pipeline] }
[Pipeline] }
[Pipeline] }
[Pipeline] // stage
[Pipeline] // stage
[Pipeline] // stage
[Pipeline] }
[Pipeline] }
[Pipeline] }
[Pipeline] // parallel
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
Try refactoring your code like shown below.
build_list = ['B1X', 'B2X', 'B3X']
l1 = ['A1', 'A2', 'A3', 'A4']
l2 = ['B1', 'B2', 'B3', 'B4']
l3 = ['C1', 'C2', 'C3', 'C4']
array_build_list = [ l1, l2, l3 ]
index = 1
index = -index
def parallelStagesMap = build_list.collectEntries {
index++
["${it}" : generateStage(it, index)]
}
def generateStage(job, index) {
return {
stage("${job}") {
echo "This is ${job}"
script {
build_project_list = array_build_list[index]
echo "build_project_list = ${build_project_list}"
runStages(build_project_list)
}
}
}
}
def runStages(def build){
for(int i=0; i<build.size(); i++) {
stage(build[i]) {
echo "Build ~~~~~ ${build[i]}"
}
}
}
pipeline {
agent any
stages {
stage("start") {
steps {
echo "start"
}
}
stage('parallel stage') {
steps {
script {
echo "Inside script"
parallel parallelStagesMap
}
}
}
}
}
Explanation
Basically, this has to do with the variable scopes. In your pipeline, the culprit is this line build_project_list = array_build_list[index] Since you don't have def in front of the variable declaration the build_project_list becomes a global variable that binds to the scope of the script. So when things execute in parallel all the stages will update this variable in parallel.
So there are two ways to fix this, method one is the one I added above, by scoping the variables by moving them to a new function. Method two is simply adding def key work to the variable build_project_list. Something like below.
def build_project_list = array_build_list[index]

Jenkins assign input to environment variable

Trying to assign user input from 1 stage to use in another stage , thought to do it with env variables but cant figure it out
code so far :
environment {
access_key =''
}
stages {
stage('User input for Auth'){
input {
message 'enter access_key'
parameters {
string 'access_key_input'
string 'secret_key_input'
}
}
environment {
access_key = sh(script:"echo ${access_key_input}", returnStdout: true).trim()
}
steps{
sh "echo ${env.access_key}"
}
}
stage("Build") {
steps {
sh "echo ${env.access_key}"
}
}
}
logs :
+ echo XXX
+ echo XXX
XXX
+ echo null
null
You can assign the input to a global variable and then access that wherever you want.
def INPUT_PARAMS = null
pipeline {
agent {
node {
label 'any'
}
}
options {
timestamps()
}
stages {
stage('User input for Auth') {
steps{
script {
INPUT_PARAMS = input message: "enter access_key", parameters: [
string(description: 'Access key', defaultValue: '', name: 'access_key_input'),
string(description: 'Secret access key', defaultValue: '', name: 'secret_key_input')
]
}
sh "echo ${INPUT_PARAMS.access_key_input}"
}
}
stage("Build") {
steps {
sh "echo ${INPUT_PARAMS.access_key_input}"
}
}
}
}
Input request
Output
Started by user Praveen Premaratne
Replayed #152
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on devops-jenkins in /home/jenkins/workspace/Test
[Pipeline] {
[Pipeline] timestamps
[Pipeline] {
[Pipeline] stage
[Pipeline] { (User input for Auth)
[Pipeline] script
[Pipeline] {
[Pipeline] input
[2021-07-15T22:35:33.625Z] Input requested
[2021-07-15T22:35:43.540Z] Approved by Praveen Premaratne
[Pipeline] }
[Pipeline] // script
[Pipeline] sh
[2021-07-15T22:35:43.886Z] + echo a
[2021-07-15T22:35:43.886Z] a
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Build)
[Pipeline] sh
[2021-07-15T22:35:44.230Z] + echo a
[2021-07-15T22:35:44.230Z] a
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // timestamps
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

Jenkins Pipeline with conditional "When" expression of choice parameters

I'm new to Groovy. I'm not able to figure out what's wrong here.
Depends on the choice of input, I expect the script to execute either Step 'Hello' or 'Bye' but it skips both. I mostly orientated to this Jenkins pipeline conditional stage using "When" for choice parameters, but still can't figure it out.
How can I use those choice parameters correctly?
pipeline {
agent any
stages {
stage('Init') {
steps('Log-in'){
echo 'Log-in'
}
}
stage('Manual Step') {
input {
message "Hello or Goodbye?"
ok "Say!"
parameters{choice(choices:['Hello','Bye'], description: 'Users Choice', name: 'CHOICE')}
}
steps('Input'){
echo "choice: ${CHOICE}"
echo "choice params.: " + params.CHOICE //null
echo "choice env: " + env.CHOICE //Hello
}
}
stage('Hello') {
when{ expression {env.CHOICE == 'Hello'}}
steps('Execute'){
echo 'Say Hello'
}
}
stage('Bye') {
when{ expression {env.CHOICE == 'Bye'}}
steps('Execute'){
echo 'Say Bye'
}
}
}
}
Output:
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Init)
[Pipeline] echo
Log-in
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Manual Step)
[Pipeline] input
Input requested
Approved by Admin
[Pipeline] withEnv
[Pipeline] {
[Pipeline] echo
choice: Hello
[Pipeline] echo
choice params.: null
[Pipeline] echo
choice env: Hello
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Hello)
Stage "Hello" skipped due to when conditional
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Bye)
Stage "Bye" skipped due to when conditional
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
From the docs:
Any parameters provided as part of the input submission will be available in the environment for the rest of the stage.
This means that your parameter CHOICE does not exist in the other stages. If you want to have a parameter that's available on all the stages, you can define a parameter outside of the stage, i.e.:
pipeline {
agent any
parameters {
choice(choices:['Hello','Bye'], description: 'Users Choice', name: 'CHOICE')
}
stages {
stage('Init') {
steps('Log-in') {
echo 'Log-in'
}
}
stage('Manual Step') {
steps('Input') {
echo "choice: ${CHOICE}"
echo "choice params.: " + params.CHOICE
echo "choice env: " + env.CHOICE
}
}
stage('Hello') {
when {
expression { env.CHOICE == 'Hello' }
}
steps('Execute') {
echo 'Say Hello'
}
}
stage('Bye') {
when {
expression {env.CHOICE == 'Bye'}
}
steps('Execute'){
echo 'Say Bye'
}
}
}
}
This will behave as expected. The difference is that the job won't ask you for input, instead, you will provide the wanted parameters before pressing build.

Executing parallel stages with declarative pipeline only using last item from list

I'm not sure what i'm doing wrong here, since currently when i try to iterate on a list the creation of the stages seems fine, but when executing the shellscript, the value used is allways the last item of the list:
Working pipeline:
pipeline {
agent any
stages {
stage('set servers') {
steps {
script {
my_list = ['server1','server-2','server-3']
}
}
}
stage('Execute then') {
parallel {
stage('shouter') {
steps {
script {
shouter = [:]
script {
for(i in my_list) {
shouter["${i}"] = {
echo "standupandshout.sh ${i}"
}
}
}
parallel shouter
}
}
}
}
}
}
}
Output:
Console Output:
Replayed #4
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] node
Running on Jenkins in /var/lib/jenkins/workspace/test
[Pipeline] {
[Pipeline] stage
[Pipeline] { (set servers)
[Pipeline] script
[Pipeline] {
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Execute then)
[Pipeline] parallel
[Pipeline] [shouter] { (Branch: shouter)
[Pipeline] [shouter] stage
[Pipeline] [shouter] { (shouter)
[Pipeline] [shouter] script
[Pipeline] [shouter] {
[Pipeline] [shouter] script
[Pipeline] [shouter] {
[Pipeline] [shouter] }
[Pipeline] [shouter] // script
[Pipeline] [shouter] parallel
[Pipeline] [server1] { (Branch: server1)
[Pipeline] [server-2] { (Branch: server-2)
[Pipeline] [server-3] { (Branch: server-3)
[Pipeline] [server1] echo
[server1] standupandshout.sh server-3
[Pipeline] [server1] }
[Pipeline] [server-2] echo
[server-2] standupandshout.sh server-3
[Pipeline] [server-2] }
[Pipeline] [server-3] echo
[server-3] standupandshout.sh server-3
[Pipeline] [server-3] }
[Pipeline] [shouter] // parallel
[Pipeline] [shouter] }
[Pipeline] [shouter] // script
[Pipeline] [shouter] }
[Pipeline] [shouter] // stage
[Pipeline] [shouter] }
[Pipeline] // parallel
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
Desired output:
[Pipeline] [server1] echo
[server1] standupandshout.sh server-1
[Pipeline] [server1] }
[Pipeline] [server-2] echo
[server-2] standupandshout.sh server-2
[Pipeline] [server-2] }
[Pipeline] [server-3] echo
[server-3] standupandshout.sh server-3
This is due to groovy closures and when the code they contain gets evaluated.
http://blog.freeside.co/2013/03/29/groovy-gotcha-for-loops-and-closure-scope/
When the closures are run the value that is bound to the variable i is the value it had on the final iteration of the loop rather than the iteration where the closure was created. The closures' scopes have references to i and by the time any of the closures are executed i is 5.
Variables local to the loop body do not behave like this, obviously because each closure scope contains a reference to a different variable
This is why your stage name is ok but your value is not.
What’s the solution? Should we always use .each rather than a for loop? Well, I kind of like for loops in many cases and there can be memory utilization differences (don’t take that to mean loops are "better" or "more efficient").
If you simply alias the loop variable and refer to that alias in the
closure body all will be well
def fns = []
for (i in (1..5)) {
def myi = i
def isq = i * i
fns << {->
println "$myi squared is $isq"
}
}
fns.each { it() }
So this should work:
script {
shouter = [:]
for(i in my_list) {
def val = i
shouter["${i}"] = {
echo "standupandshout.sh ${val}"
}
}
parallel shouter
}

Resources