3 comments

  • 0
    Avatar
    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')

          }

        }

  • 0
    Avatar
    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: 'script.sh', text: "libraryResource('jenkins/myscript')"

    sh "bash script.sh param1 param2"

  • 0
    Avatar
    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'))

    JC

Please sign in to leave a comment.