Jenkins Shared Libraries: is it possible to pass arguments to shell scripts imported as 'libraryResource'? - shell

I have the following setup:
(Stripped out) Jenkinsfile:
#Library('my-custom-library') _
pipeline {
agent any
stages {
stage('Example') {
steps {
printHello name: 'Jenkins'
}
}
}
}
my-custom-library/resources/com/org/scripts/print-hello.sh:
#!/bin/bash
echo "Hello, $1"
my-custom-library/vars/printHello.groovy:
def call(Map parameters = [:]) {
def printHelloScript = libraryResource 'com/org/scripts/print-hello.sh'
def name = parameters.name
//the following line gives me headaches
sh(printHelloScript(name))
}
I expect Hello, Jenkins, but it throws the following exception:
groovy.lang.MissingMethodException: No signature of method:
java.lang.String.call() is applicable for argument types:
(java.lang.String) values: [Jenkins]
Possible solutions: wait(), any(), wait(long),
split(java.lang.String), take(int), each(groovy.lang.Closure)
So, is it possible to do something like described above, without mixing Groovy and Bash code?

Yes, check out withEnv
The example they give looks like;
node {
withEnv(['MYTOOL_HOME=/usr/local/mytool']) {
sh '$MYTOOL_HOME/bin/start'
}
}
More applicable to you:
// resources/test.sh
echo "HI here we are - $PUPPY_DOH --"
// vars/test.groovy
def call() {
withEnv(['PUPPY_DOH=bobby']) {
sh(libraryResource('test.sh'))
}
}
Prints:
[Pipeline] {
[Pipeline] withEnv
[Pipeline] {
[Pipeline] libraryResource
[Pipeline] sh
+ echo HI here we are - bobby --
HI here we are - bobby --
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
Using that, you can pass it in using a scoped named variable, something like
def call(Map parameters = [:]) {
def printHelloScript = libraryResource 'com/org/scripts/print-hello.sh'
def name = parameters.name
withEnv(['NAME=' + name]) { // This may not be 100% syntax here ;)
sh(printHelloScript)
}
// print-hello.sh
echo "Hello, $name"

Related

Bad substitution when passing parameter to shell script in Jenkins

I'm attempting to pass value of a variable by setting the stdOut of a shell script.
However in The console of Jenkins displays of followings:
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Feature Segunda)
[Pipeline] echo
${params.Segunda}
[Pipeline] sh
/var/jenkins_home/workspace/pruebaParametrizada#tmp/durable-71aead85/script.sh: 1: /var/jenkins_home/workspace/pruebaParametrizada#tmp/durable-71aead85/script.sh: Bad substitution
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Declarative: Post Actions)
[Pipeline] echo
Building finished successfully
I use to escape the quotes, nothing works. I get a bad substitution error. I've also tried without double quotes.
If I hardcode in the shell script arguments, it runs fine.
pipeline {
agent any
parameters {
string(defaultValue: "", description: '', name: 'One')
string(defaultValue: "", description: '', name: 'Two')
}
stages {
stage('Git Checkout') {
steps {
git credentialsId: 'personal-github', url: 'https:xxx'
}
}
stage('Maven Compile') {
steps {
sh 'mvn clean compile'
}
}
stage('Test One') {
steps {
//ERROR
sh 'mvn test -Dcucumber.options="-t #${params.One}"'
//This Works
//sh 'mvn test -Dcucumber.options="-t #One"'
}
}
}
post {
always {
echo 'Building finished successfully'
cucumber failedFeaturesNumber: -1,
failedScenariosNumber: -1,
failedStepsNumber: -1,
fileIncludePattern: '**/*.json',
jsonReportDirectory: 'target/cucumber/',
pendingStepsNumber: -1,
reportTitle: 'test features',
skippedStepsNumber: -1,
sortingMethod: 'ALPHABETICAL',
undefinedStepsNumber: -1
}
}
}
I found the way to pass parameters as follows`
sh """mvn test -Dcucumber.options='-t #${params.One}'"""
Thanks to all, for responses.

Jenkins declarative pipeline get environment variable at post stage

I am getting runtime value in build stage stage which I stored in an environment variable . I saved that to env.cfg file under WORKSPACE .
Now I am trying to get that value in post pipeline step to be used in email communication. I tried load method but it did not work
Any help ?
post {
always {
echo $SNAPSHOT / /this always comes null
}
}
This is the way you can access an environment variable across the pipeline
pipeline {
agent any;
environment {
MESSAGE="Hello World"
}
stages {
stage('one') {
steps {
echo "${env.MESSAGE}"
sh "echo $MESSAGE"
script {
print env.MESSAGE
}
}
}
}
post {
success {
echo "${env.MESSAGE}"
script {
print env.MESSAGE
}
}
failure {
echo "${env.MESSAGE}"
script {
print env.MESSAGE
}
}
}
}
but as per your scenario let say I have a file called .env with the content below in the current Jenkins job WORKSPACE and I want to read and make this env variable in the pipeline.
.env
export SNAPSHOT=1.0.0
export MESSAGE='Hello World'
export MESSAGE_FROM_ENV_FILE='Hello From .env file'
your pipeline should look like
scripted pipeline
node {
stage('one') {
sh """
source $WORKSPACE/.env
echo \$SNAPSHOT
echo \$MESSAGE
echo \$MESSAGE_FROM_ENV_FILE
"""
}
}
declarative pipeline
pipeline {
agent any;
stages {
stage('build') {
steps {
sh """
source $WORKSPACE/.env
echo \$SNAPSHOT
echo \$MESSAGE
echo \$MESSAGE_FROM_ENV_FILE
"""
}
}
}
post {
success {
sh """
source $WORKSPACE/.env
echo \$SNAPSHOT
echo \$MESSAGE
echo \$MESSAGE_FROM_ENV_FILE
"""
}
}
}
You need a global variable:
SNAPSHOT = ""
println "SNAPSHOT is ${SNAPSHOT}"
pipeline {
agent any
stages {
stage('Build') {
steps {
script {
println "SNAPSHOT is ${SNAPSHOT}"
SNAPSHOT = "Modified"
println "SNAPSHOT is now ${SNAPSHOT}"
}
}
}
}
post {
always {
echo "${SNAPSHOT}"
}
}
}

Jenkinsfile setting stageResult variable based on conditions

I'm trying to script a Jenkinsfile in Declarative Syntax, requirement here is if set environment variables are not a match then pipeline stage should be depict as "Aborted" but the build status could be either Aborted or unstable, on Jenkins online documentation and from Snippet Generator did see the following style
catchError(buildResult: 'ABORTED', stageResult: 'ABORTED') {
// some block
}
catchError does not serve my purpose as it is appropriate for use when the script inside the stage does not return true or if there is an execution error, although default when condition in Jenkins Declarative Syntax is appropriate, Jenkins does not allow setting stage result to 'ABORTED'
when {
expression {
SCM_BRANCH_NAME ==~ /(master|QA)/
}
expression{
ENVIRONMENT ==~ /(QA)/
}
allOf{
environment ignoreCase: true,name: 'PRODUCT_NAME' , value: 'PRODUCT-1'
}
}
Please view the Sample Jenkinsfile below using if and else format
Sample Jenkinsfile
pipeline {
agent {
node {
label 'master'
}
}
environment{
ENVIRONMENT = "QA"
PRODUCT_NAME = "PRODUCT-1"
SCM_BRANCH_NAME = "master"
}
stages{
stage('Testing-1') {
when{
expression{
ENVIRONMENT ==~ /(QA)/
}
}
steps {
script {
if (PRODUCT_NAME == PRODUCT-1){
sh """
echo "Reached Here ${ENVIRONMENT} - ${PRODUCT_NAME} - ${SCM_BRANCH_NAME}"
// do testing for product 1
"""
}
else{
stageResult = 'ABORTED'
echo "PRODUCT not Available for Testing"
}
}
}
}
stage('Testing-2'){
steps{
sh '''
echo "Reached Second Stage"
'''
}
}
}
}
Any suggestions of how to implement the scenario that if conditions are not met set the stageResult as Abort, any suggestions either with a plugin or with a sample notation script is greatly appreciated
Thanks
Below snippet worked for me. You can use catch error block and throw the error from that block when some condition met. You can also catch the exception in the post stage failure section.
pipeline {
agent {
node {
label 'master'
}
}
environment{
ENVIRONMENT = "QA"
PRODUCT_NAME = "PRODUCT-1"
SCM_BRANCH_NAME = "master"
}
stages{
stage('Testing-1') {
when{
expression{
ENVIRONMENT ==~ /(QA)/
}
}
steps {
script {
catchError(buildResult: 'FAILURE', stageResult: 'ABORTED'){
if (PRODUCT_NAME != PRODUCT-1) {
error ("PRODUCT not Available for Testing")
}
sh """
echo "Reached Here ${ENVIRONMENT} - ${PRODUCT_NAME} - ${SCM_BRANCH_NAME}"
// do testing for product 1
"""
}
}
}
post {
failure {
echo "Something Failed and error has been catched"
}
}
}
stage('Testing-2'){
steps{
sh '''
echo "Reached Second Stage"
'''
}
}
}

variables not getting populated in sh of jenkins pipeline

I have the below code and username,pwd and modulename from previous stages are not getting populated when I'm doing curl in sh script.
Please let me know what do I need to fix it
def USERNAME
def PASSWORD
def MODULE_NAME
node {
try {
stage('userAuth') {
withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: 'ID-Creds',
usernameVariable: 'user', passwordVariable: 'pwd']]) {
USERNAME="$user"
PASSWORD="$pwd"
echo "$USERNAME:$PASSWORD" //This is fine
}
}
stage('readPOM') {
def pom = readMavenPom file: 'pom.xml'
MODULE_NAME = pom.module
echo "$MODULE_NAME" //This is printing fine
}
stage('do curl') {
def revision = sh(script: '''
AUTH="$USERNAME:$PASSWORD"; //Not getting populated getting empty
RespInfo=$(curl -u $AUTH "https://host/apis/${MODULE_NAME}/deployments"); //Not getting populated getting empty for modulename
currntRev=$(jq -r .revision[0].name <<< "${RespInfo}");
echo $currntRev
''',returnStdout: true).split()
}
}
catch (e) {
throw e
} finally {
}
}
You have to use double quotes (""") to apply string interpolation:
def revision = sh(script: """
AUTH=\"$USERNAME:$PASSWORD\"; //Not getting populated getting empty
A easier way is to concat two strings as following:
def revision = sh(
returnStdout: true,
script: '''
AUTH="$user:$pwd";
RespInfo=$(curl -u "$AUTH" "https://host/apis/''' + MODULE_NAME + '''/deployments");
currntRev=$(jq -r .revision[0].name <<< "${RespInfo}");
echo $currntRev
'''
).split()
Note: string wrapped in """ will be expanded, but not when wrapped in '''
USERNAME and PASSWORD are Groovy variable, when they are wrapped in """ or "", Groovy executor will expand them before script be executed.
user and pwd are Shell variable, we should use Shell variable when use '''

List in place sorting in Jenkins Pipelines

I am trying to sort a list of objects in Jenkins pipelines. I'm getting different results running code below locally or within Jenkins:
pipeline {
agent any
stages {
stage('default'){
steps {
script {
#NonCPS
def nonCpsTest = {
def list = [
['CreationDate': '200'],
['CreationDate': '300'],
['CreationDate': '100'],
]
def rval = list.sort { it['CreationDate'] }
echo "Rval=$rval"
echo "List=$list"
}
nonCpsTest()
}
}
}
}
}
When I execute this script locally using groovy shell (groovysh) result is
groovy:000> list = [[ 'CreationDate':200 ], [ 'CreationDate':300 ], [ 'CreationDate':100 ]]
===> [[CreationDate:200], [CreationDate:300], [CreationDate:100]]
groovy:000> rval = list.sort { it['CreationDate'] }
===> [[CreationDate:100], [CreationDate:200], [CreationDate:300]]
groovy:000> list
===> [[CreationDate:100], [CreationDate:200], [CreationDate:300]]
groovy:000> list == rval
===> true
While on the Jenkins server I'm getting following
[Pipeline] {
[Pipeline] stage
[Pipeline] { (default)
[Pipeline] script
[Pipeline] {
[Pipeline] echo
Rval=300
[Pipeline] echo
List=[[CreationDate:200], [CreationDate:300], [CreationDate:100]]
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Is Jenkins workflow making list immutable in anyway, or overriding sort method, and if so, how to do in place list sorting within the Jenkins pipeline code?
the problem that you declared nonCpsTest as a variable and it references to closure, so #NonCPS does not work in this case
the following variant works fine:
#NonCPS
def nonCpsTest() {
def list = [
['CreationDate': '200'],
['CreationDate': '300'],
['CreationDate': '100'],
]
def rval = list.sort{ it['CreationDate'] }
echo "Rval=$rval"
echo "List=$list"
}
node{
nonCpsTest()
}

Resources