I want to pass in jenkins parameters as array args to my bash script.
I have tried below but no luck.
stage('Foo Step') {
steps {
script {
sh "chmod 755 runFooBar.sh"
sh """
./runFooBar.sh --baz="${params.BAZ[#]}" --bar="${params.BAR}" --foo="${params.FOO[#]}"
"""
}
}
}
I am getting this error when I run above script.
WorkflowScript: 48: unexpected token: # # line 48, column 75.
./runFooBar.sh --baz="${params.BAZ[#]}" --bar
^
Thank you in advance.
Cheers!
Had similar issue to resolve.
Try to remove the '[#]' as groovy seems not to appreciate this.
In my case it looks as follows and works thus far:
steps {
script {
RECIPIENTS = '"user1#domain" "user2#domain" "user3#domain"'
sh "./send_email.sh ${RECIPIENTS}"
}
}
Related
I have a shell script inside my jenkins pipeline which will call mvn. For that i have to pass variable value to mvn. The variable is not passing inside the Jenkins pipeline's shell. But when trying from local machine shell it is working fine as expected.
ARTIFACT_NAME="Sample_Artifact"
pipeline{
agent {
node{
label "${AGENT}"
}
}
stages{
stage("Setting MultiJob Properties"){
steps{
sh '''set +x
export VERSION=$(mvn -B -q -Dexec.executable=echo -Dexec.args=\${${ARTIFACT_NAME}} )
echo $VERSION
'''
}
}
}
}
Expected Process: export VERSION=$(mvn -B -q -Dexec.executable=echo -Dexec.args=${Sample_Artifact} )
Expected Output: 1.0001
ARTIFACT_NAME - I am passing it from Jenkins UI.
${${ARTIFACT_NAME}} - This variable is perfectly replace value in Freestyle jobs and it is throwing error in the Pipeline jobs.
Error Message: script.sh: 3: Bad substitution
Can Anyone please help me to resolve the issue?
As Ian wrote, you're passing the whole script as a literal (''') instead of an interpolated string ("""), so the variable name doesn't get substituted with its value:
pipeline{
agent {
node {
label AGENT
}
}
stages {
stage("Setting MultiJob Properties") {
steps {
sh """set +x
export VERSION=\$(mvn -B -q -Dexec.executable=echo -Dexec.args=\${$ARTIFACT_NAME})
echo \$VERSION"""
}
}
}
}
How do I use shell arrays in a Jenkinsfile?
My Jenkins job has a String parameter PROJECTS that is a comma-separated list of projects to build. I have a Build step in which I run some shell script to split that parameter into an array, and then pass that array to a build script:
...
stage("Build") {
steps {
sh"""
projects_list=(${env.PROJECTS//,/ })
./build_script ${projects_list[#]}
"""
}
}
...
however, the Jenkins build keeps failing due to this:
WorkflowScript: 132: unexpected token: # # line 132, column 104.
build_script ${projects_list[#]}
^
1 error
Please see the below code which gives desired result:
Please note : I am using bat command and calling shell scripts inside via cygwin as am using Windows machine.
...
def PROJECTS = "ABC,XYZ"
stage("Build") {
steps {
bat'cygwin.bat -c \"projects_list=(${PROJECTS//,/ }); ./buildscript.sh ${projects_list[#]} \"'
}
}
...
cygwin.bat
IF [%1] == [-c] (
C:\Cygwin\bin\bash.exe -l -i %*
) ELSE (
startC:\Cygwin\bin\mintty.exe --exec C:\Cygwin\bin\bash.exe -l -i
)
With sh: The syntax would be same, just use sh rather than bat and call the command without cywgin.bat -c
I am trying to execute shell script from my jenkins pipeline. I have provided absolute and relative path in the shell command and still I am facing No such file or directory error while building the pipeline.
This is simple script but yet not working.
Try 1:
stage ( 'Executing shell script' ) {
steps {
sh '/home/patching/shell_script.sh'
}
}
Try 2:
stage ( 'Executing shell script' ) {
steps {
sh './shell_script.sh'
}
}
Try 3:
stage ( 'Executing shell script' ) {
steps {
dir ('/home/patching/shell_script.sh){
sh './shell_script.sh'
}
}
I really don't know what is really wrong with the script. Could some one help me on this?
I got the issue why it wasn't able to find the file that I wanted to run. It was running on different slave.
Can you try this
pipeline {
agent any
stages {
stage('Hello World') {
steps {
script {
sh '''
/home/ubuntu/First.sh
'''
}
}
}
}
}
I am new to Jenkins, Groovy and pipelines. I have created a simple pipeline stage like so:
//working build but not setting env variables
node('build-01') {
stage('Building') {
echo "[*] Starting build (id: ${env.BUILD_ID}) on ${env.JENKINS_URL}"
try {
sh 'ls -l'
//ls shows the damn file
sh '. setup-target'
} catch(all) {
sh "echo 'Failed to run setup-target script with error: ' ${all}"
}
}
}
This works. But I want to modify/add environment variables to the session running this script (this script is a bash file with the correct shebang line on top). So I did:
node('build-01') {
withEnv(["CMAKE_INSTALL_DIR=${WORKSPACE}", "SDK_INSTALL_DIR=${WORKSPACE}"]){
stage('Building') {
echo "[*] Starting build (id: ${env.BUILD_ID}) on ${env.JENKINS_URL}"
try {
sh 'ls -l'
//ls shows the damn file
sh '. setup-target'
} catch(all) {
sh "echo 'Failed to run setup-target script with error: ' ${all}"
}
}
}
}
This errors out with:
/home/jenkins-sw/ci/workspace/myWorkSpace#tmp/durable-6d30b48d/script.sh: line 1: .: setup-target: file not found
and
Failed to run setup-target script with error: hudson.AbortException: script returned exit code 1
But the environment variables are set, I check this by doing a sh 'printenv' right below the ls -l line. Interestingly ls -l does show the script.
What am I missing?
UPDATE
The following:
node('build-01') {
withEnv(["CMAKE_INSTALL_DIR=${WORKSPACE}", "SDK_INSTALL_DIR=${WORKSPACE}"]){
stage('Building') {
echo "[*] Starting build (id: ${env.BUILD_ID}) on ${env.JENKINS_URL}"
try {
sh 'ls -l'
//ls shows the damn file
sh './setup-target'
} catch(all) {
sh "echo 'Failed to run setup-target script with error: ' ${all}"
}
}
}
}
results in:
/home/jenkins-sw/ci/workspace/myWorkSpace#tmp/durable-6d30b48d/script.sh: line 1: ./setup-target: Permission denied
Interesting. How is withEnv effecting permissions? What?! And if I chmod that file to have permissions, i get a new error, something related to "missing workspace".
I figured it out. I was cloning directly into the workspace and then setting my environment variables to point to the workspace as well. I modified both those things. I now create a dir in my workspace and clone into it and I also set my environment variables to directories inside my workspace. Like so:
node('build-01') {
withEnv(["CMAKE_INSTALL_DIR=${WORKSPACE}/cmake_install", "SDK_INSTALL_DIR=${WORKSPACE}/sdk"]){
stage('Building') {
echo "[*] Starting build (id: ${env.BUILD_ID}) on ${env.JENKINS_URL}"
try {
sh 'ls -l'
//ls shows the damn file
dir('path/to/checkout/') {
sh '. ./setup-target'
}
} catch(all) {
sh "echo 'Failed to run setup-target script with error: ' ${all}"
}
}
}
}
This works.
My guess would be that either CMAKE_INSTALL_DIR or SDK_INSTALL_DIR are on the path.
Instead of sh '. setup-target' you should sh './setup-target'.
In this question
#SiegeX gives a nice way of cleaning the bash PATH variable from
duplicate entries:
PATH=$(awk 'BEGIN{ORS=":";RS="[:\n]"}!a[$0]++' <<<"${PATH%:}")
this works well when I type it in the command line.
I tried using this in a bash function to be able to apply it to other variables:
function dupremove()
{
${1}=$(awk 'BEGIN{ORS=":";RS="[:\n]"}!a[$0]++' <<<"${1%:}")
}
but when I execute it it gives the error:
> dupremove PATH
bash: PATH=PATH:: command not found
Any idea on I can write the function?
This works for me (TM)
function dupremove
{
eval path=\$$1
export $1=$(awk 'BEGIN{ORS=":";RS="[:\n]"}!a[$0]++' <<< $path)
}