We are using the Pipeline Shared Libraries plugin to factorize the code common to our different Jenkins pipelines.
From its documentation, it offers a resources
top-level folder for non-Groovy files. As we rely on different bash functions, we would like to host them in a separate .sh
file (thus they could also be used by other processes than Jenkins).
The same documentation tells us about using libraryResource
step to load those resource files. We can successfully call this method within our Groovy script, giving it our resource file name as argument (function.sh
). But from here, we were not able to find a way to invoke the foofoo
function defined in function.sh
from the same Groovy script.
sh "foofoo" #error: foofoo is not defined
We also tried to first source it like this:
sh "source function.sh && foofoo"
But it fails at the source
step, stating that function.sh
is not found.
What would be the correct procedure to invoke a bash function defined in function.sh
To fix the problem, change your line to sh "./build.sh" or sh "bash build.sh" , so that the sh block in the Jenkinsfile can correctly locate the build.sh script that you want to execute. Show activity on this post. Show activity on this post. Double check the command sh 'pwd; ls -l; jenkins.sh "build.sh"' .
A shared library in Jenkins is a collection of Groovy scripts shared between different Jenkins jobs. To run the scripts, they are pulled into a Jenkinsfile. Each shared library requires users to define a name and a method of retrieving source code.
According to the documentation
External libraries may load adjunct files from a resources/ directory using the libraryResource step. The argument is a relative pathname, akin to Java resource loading:
def request = libraryResource 'com/mycorp/pipeline/somelib/request.json'
The file is loaded as a string, suitable for passing to certain APIs or saving to a workspace using writeFile.
It is advisable to use an unique package structure so you do not accidentally conflict with another library.
I assume the following will work
def functions = libraryResource 'com/mycorp/pipeline/somelib/functions.sh'
writeFile file: 'functions.sh', text: functions
sh "source function.sh && foofoo"
Since you are using Jenkins Pipelines V2, it would be better for you to create a shared library for this. The code below will work. Along with writing the file, you will need to provide execute privilege to the file as well:
def scriptContent = libraryResource "com/corp/pipeline/scripts/${scriptName}"
writeFile file: "${scriptName}", text: scriptContent
sh "chmod +x ${scriptName}"
Hope this helps!!
Use bash shebang (#!/bin/bash) at the beginning of your sh step to tell Jenkins to use bash, and then load your lib like you would in bash, e.g.:
sh '''#!/bin/bash
. path/to/shared_lib.bash
myfunc $myarg
'''
Mind the fact, that path/to/shared_lib.bash
has be checked in your repo in order for this to work.
All the previous answers are poor solutions as the script is parsed as a text file when it is transferred which corrupts it.
Quotes etc are messed up and it attempts to substitute variables.
It needs to be transferred verbatim.
The only solutions are to store the script on a file server, download it and run it e.g.:
sh """
wget http://some server/path../yourscript.sh
chmod a+x yourscript.sh
"""
...or checkout the script from the repo directly and use it locally like this:
withCredentials([usernamePassword(
credentialsId: <git access credentials>,
usernameVariable: 'username',
passwordVariable: 'password'
)])
{
sh """
git clone http://$username:$password@<your git server>/<shared library repo>.git gittemp
cd gittemp
git checkout <the branch in the shared library>
cd ..
mv -vf gittemp/<path to file>/yourscript.sh ./
"""
}
...then later run your script:
sh "./yourscript.sh ...."
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With