Skip to main content

passing arguments to a shell script in a pipeline



  • Jc Ferrer

    Following is a code snippet of a pipeline calling a library resource shell script 'myscript'. What would be the syntax to pass arguments to  'myscript'? 


    stages {

        stage('build') {

          steps {

            sh libraryResource('jenkins/myscript')



  • Denys Digtiar

    The `libraryResource` is going to read your shell script into a string. The way you use it with `sh` step you cannot easily pass the arguments since there won't be an executable script pass to pass them two. You can possibly craft the script in a certain way (e.g., only define functions) and then append to it the parameters (e.g. a call to the main function).

    You can also write the content of the file to the workspace and then call it from the sh step:

    writeFile file: '', text: "libraryResource('jenkins/myscript')"

    sh "bash param1 param2"

  • Jc Ferrer

    Thanks a lot for the response. It works. However, I had to change the syntax to make it work in a declarative pipeline:

    writeFile (file: 'myscript', text: libraryResource('jenkins/myscript'))



Please sign in to leave a comment.

About CloudBees Support

Our Support Engineers are available to help with any questions or problems you may have with any of our products.