Not able to fetch parameters in Jenkins Pipeline script - jenkins

I am trying to execute a .vbs file by passing parameters in Jenkins pipeline script.
After clicking "Build with Parameters" I entered all input parameters but below script is not passing the values I entered.
The '${param.XXX}' seems to be not working. The values passed to vbs are always ${params.DELIVERY} ${params.SOURCE_ENV} ${params.TERGET_ENV} ${params.GENREPORT}
but ideally arguments passed to vbs should be equal to the data entered before triggering pipeline. Can anyone please help me?
#!/usr/bin/env groovy
pipeline {
agent any
parameters {
string(defaultValue: '', description: 'Delivery name', name: 'DELIVERY')
choice(choices:'DEV1\nDEV2\nDEV3',description: 'Select Source environment', name: 'SOURCE_ENV')
choice(choices:'TEST1\nTEST2\nTEST3',description: 'Select target environment', name: 'TERGET_ENV')
choice(choices:'Yes\nNo',description: 'Generate Report?', name: 'GENREPORT')
}
stages {
stage("Start Batch") {
steps {
bat '''
echo '${params.DELIVERY}'
echo '${params.SOURCE_ENV}'
echo '${params.TERGET_ENV}'
echo '${params.GENREPORT}'
cd "C:\\Users\\DELIVERY_BATCH\\src"
cscript.exe DELEXCEBATCH.vbs "C:\\Users\\Documents\\BatchFiles" ${params.DELIVERY} ${params.SOURCE_ENV} ${params.TERGET_ENV} ${params.GENREPORT}
EXIT /B 0
'''
}
}
stage("Create Summary Excel sheet") {
steps {
bat '''
echo 'Batch Execution is successful'
'''
}
}
}
}

This:
bat '''
Should be:
bat """
Because variables are only evaluated when double quotes are used.

Related

Need to get user email id as input and send logs in jenkins

I have a shell script wil runs on taking user inputs and send logs to users when fails syntax I use: ./script.sh env usecase emailid
Now am doing a jenkins build and not sure on how to get user input for email id . I am currently getting 2 inputs using choice parameter.
I want user to give email id and its passed as a parameter .
#Library('Shared#release/v1')
import jenkins.BuildSupport
properties([parameters([choice(choices: ['dev''uat', 'prod'], description: 'Select the Environment', name: 'ENVIRONMENT'), choice(choices: ['a1','a2','all'], description: 'Select the Service', name: 'SERVICENAME')])])
node{
WORKSPACE = pwd()
//checkout code from shared library
stage ('Code Checkout'){
codeCheckout
}
//post build work
stage('Executing Health Check') {
withEnv(["WORKSPACE=${WORKSPACE}","ENVIRONMENT=${params.ENVIRONMENT}","SERVICENAME=${params.SERVICENAME}",]) {
sh '''
set +x
ls -l
./script.sh ${ENVIRONMENT} ${SERVICENAME}
'''
}
}
}
I need the script.sh to take 3rd parameter which will be the email id entered by user
So couple of things going on here. First, you need to add a string parameter to ask the user for input, then you need to pass that to the shell script, and then you need to make sure the shell script can use it.
I don't see the need for withEnv, you can pass variables to a script without that.
Just make sure your shell script is getting the EMAIL_ADDRESS from $3
#!groovy
#Library('Shared#release/v1')
import jenkins.BuildSupport
properties([parameters([string(name: 'EMAIL_ADDRESS', description: 'Enter the email address'), choice(choices: ['dev','uat','prod'], description: 'Select the Environment', name: 'ENVIRONMENT'), choice(choices: ['a1','a2','all'], description: 'Select the Service', name: 'SERVICENAME')])])
node{
WORKSPACE = pwd()
//checkout code from shared library
stage ('Code Checkout'){
codeCheckout
}
//post build work
stage('Executing Health Check') {
sh '''
set +x
ls -l
./script.sh $ENVIRONMENT $SERVICENAME $EMAIL_ADDRESS
'''
}
}
Example of sending email from Jenkins scripted pipeline/ Groovy
stage('Email the results') {
emailext attachLog: true,
attachmentsPattern: '*',
to: "${EMAIL_ADDRESS}",
subject: "${currentBuild.currentResult} - ${ENVIRONMENT} ${SERVICE}",
body: """
Blah blah blah
"""
}

How to pass groovy variable to powershell in Jenkins pipeline?

I'm trying to pass groovy variable to powershell script inside of jenkins pipeline, all in the same place but i don't know how. i tried different ways without success.
I require this to obtain the name of the person who approved the step of PIPELINE and pass it to powershell, which connects with SQL SERVER
stage('Step1'){
steps{
script{
def approverDEV
approverDEV = input id: 'test', message: 'Hello', ok: 'Proceed?', parameters: [choice(choices: 'apple\npear\norange', description: 'Select a fruit for this build', name: 'FRUIT'), string(defaultValue: '', description: '', name: 'myparam')], submitter: 'user1,user2,group1', submitterParameter: 'APPROVER'
echo "This build was approved by: ${approverDEV['APPROVER']}"
}
}
}
stage('Step2'){
steps{
script{
powershell ('''
# Example echo "${approverDEV['APPROVER']}"
# BUT THIS DOESN'T WORK :(
''')
}
}
}
I expect the output is the name of the approver stored in the variable GROOVY approverDEV
Dagett is correct, use double-quotes around the powershell script, then the variables will be evaluated:
script{
powershell ("""
# Example echo "${approverDEV['APPROVER']}"
# BUT THIS DOESN'T WORK :(
""")
}
Using triple double quotes in Groovy is called 'multi-line GString'. In a GString, variables will be evaluated before creating the actual String.

How to pass variables set in sh script to subsequent Jenkins Pipeline Steps

I have a jenkins pipeline file where i need to call an sh file
node {
stage("Stage1") {
checkout scm
sh '''
echo "Invoking the sh script"
valueNeedstobepassed = "test"
'''
}
stage ('stage2') {
Need to refer the "valueNeedstobepassed" varaible in my
pipleline step
}
}
I am not able to refer the variable "valueNeedstobepassed" on stage 2
Any help please?

Jenkins Pipeline Conditional Stage based on Environment Variable

I want to create a Jenkins (v2.126) Declarative syntax pipeline, which has stages with when() clauses checking the value of an environment variable. Specifically I want to set a Jenkins job parameter (so 'build with parameters', not pipeline parameters) and have this determine if a stage is executed.
I have stage code like this:
stage('plan') {
when {
environment name: ExecuteAction, value: 'plan'
}
steps {
sh 'cd $dir && $tf plan'
}
}
The parameter name is ExecuteAction. However, when ExecuteAction is set via a Job "Choice" parameter to: plan, this stage does not run. I can see the appropriate value is coming in via environment variable by adding this debug stage:
stage('debug') {
steps {
sh 'echo "ExecuteAction = $ExecuteAction"'
sh 'env'
}
}
And I get Console output like this:
[Pipeline] stage
[Pipeline] { (debug)
[Pipeline] sh
[workspace] Running shell script
+ echo 'ExecuteAction = plan'
ExecuteAction = plan
[Pipeline] sh
[workspace] Running shell script
+ env
...
ExecuteAction=plan
...
I am using the when declarative syntax from Jenkins book pipeline syntax, at about mid-page, under the when section, built-in conditions.
Jenkins is running on Gnu/Linux.
Any ideas what I might be doing wrong?
Duh! You need to quote the environment variable's name in the when clause.
stage('plan') {
when {
environment name: 'ExecuteAction', value: 'plan'
}
steps {
sh 'cd $dir && $tf plan'
}
}
I believe you need to use params instead of environment. Try the following:
when {
expression { params.ExecuteAction == 'plan' }
}

Jenkins pipeline "when" condition with sh defined variable

I'm trying to create a Jenkins pipeline where I in my first stage I define a variable in a sh shell script.
Then I want to run the next stages using a "when" condition depending on the previous defined variable.
pipeline {
agent { label 'php71' }
stages {
stage('Prepare CI...') {
steps{
sh '''
# Get the comment that was made on the PR
COMMENT=`echo $payload | jq .comment.body | tr -d '"'`
if [ "$COMMENT" = ":repeat: Jenkins" ]; then
BUILD="build"
fi
'''
}
}
stage('Build Pre Envrionment') {
agent { label 'php71' }
when {
expression { return $BUILD == "build" }
}
steps('Build') {
sh '''
echo $BUILD
echo $COMMENT
'''
}
}
}
}
This gives me an error:
groovy.lang.MissingPropertyException: No such property: $BUILD for class: groovy.lang.Binding
How can I do it? Is it possible?
Thank you!
Probably use Jenkins scripted pipeline which is more flexible than declarative.
Print the value in the sh script and use returnStdout to make it available to the pipeline script. See How to do I get the output of a shell command executed using into a variable from Jenkinsfile (groovy)? for more details.

Resources