Jenkins Pipeline job can't find script due to @tmp path being created

21,586

Solution 1

I guess your pwd is not in PATH so you have to call it like this: sh './update-plugins.sh'

Solution 2

Have you tried using the jenkins workspace environment variable WORKSPACE (absolute path of the workspace)? With that your line would look something like this:

sh '${WORKSPACE}/jenkins/pipeline/update-jenkins-plugins-ppln/update-plugins.sh'

Solution 3

I was having the same issue. I think @Oren technically answered your question about why Jenkins creates this tmp space, but I can share some info about how I solved it.

Basically, my Jenkins host symlinks bin/sh to dash; not bash. So, using a POSIX-compliant shell script solved the issue for me.

For example, I was trying to use shopt -s extglob to do some pattern matching:

stage {
    def shellCommand = $/ "rm -rf ! (_data|_includes|_plugins|Gemfile|_config.yml|page-builder)"/$
    sh(returnStdout: true, script: shellCommand).trim()
}

Since dash doesn't support extglob, replacing that with a POSIX-compliant find command worked:

stage {
    sh('find . -regextype posix-extended -not -regex ".*includes.*|.*data.*|.*plugins.*|.*config.yml|.*Gemfile.*"')
} 
Share:
21,586

Related videos on Youtube

Alex
Author by

Alex

Updated on July 05, 2022

Comments

  • Alex
    Alex almost 2 years

    I am writing a pipeline job that will call another script to execute. The Jenkinsfile and script exist in the same directory and yet the job fails to find the script to run.

    This is the relevant bit of script;

    stage ('Update') {
        try {
            dir('jenkins/pipeline/update-jenkins-plugins-ppln') {
                sh 'ls -l'
                sh 'update-plugins.sh'
            }
    }
    

    which returns the following error;

    [update-jenkins-plugins-ppln] Running shell script
    + ls -l
    total 8
    -rw-r--r-- 1 jenkins jenkins 2441 Dec 20 09:34 Jenkinsfile
    -rwxr-xr-x 1 jenkins jenkins  506 Dec 19 14:06 update-plugins.sh
    [Pipeline] sh
    [update-jenkins-plugins-ppln] Running shell script
    + update-plugins.sh
    /var/lib/jenkins/workspace/update-jenkins-plugins-ppln/jenkins/pipeline/update-jenkins-plugins-ppln@tmp/durable-11cefdd0/script.sh: 2: /var/lib/jenkins/workspace/update-jenkins-plugins-ppln/jenkins/pipeline/update-jenkins-plugins-ppln@tmp/durable-11cefdd0/script.sh: update-plugins.sh: not found
    

    As you can see, the pathing I'm using is correct because according to the ls the file I need update-plugins.sh is in the directory I've pathed to. For some reason though, when actually searching for the script Jenkins is adding @tmp/durable-8d48734f/script.sh onto the path.

    Various troubleshooting:

    • I read that you have to checkout the branch again even if you're already checking it out to get the Jenkinsfile, so I am.
    • I have ssh'd into the Jenkins box to check and yes, the script is there.

    Why is Jenkins adding the @tmp bit, and is there a way to prevent this behavior?

    • MisterStrickland
      MisterStrickland almost 6 years
      Hi. Just wondering if you found a solution to your problem.
    • Alex
      Alex almost 6 years
      @Phil Nope, it's still an issue
  • Alex
    Alex over 7 years
    Tried that already; same error + ./update-plugins.sh /var/lib/jenkins/workspace/update-jenkins-plugins-ppln@tmp/d‌​urable-88ee0ed4/scri‌​pt.sh: 2: /var/lib/jenkins/workspace/update-jenkins-plugins-ppln@tmp/d‌​urable-88ee0ed4/scri‌​pt.sh: ./update-plugins.sh: not found
  • izzekil
    izzekil over 7 years
    Then that's strange. I can only suggest you to try sh ' bash update-plugins.sh' or to calculate full path using pipeline func pwd(), concatenate and call the script by absolute path.
  • Markus Weninger
    Markus Weninger almost 7 years
    For me it worked using './' in the beginning of the script's name.
  • overexchange
    overexchange about 5 years
    Yes, /bin/sh -> /bin/dash in my case
  • Alexander Samoylov
    Alexander Samoylov almost 5 years
    For we adding of './' also worked. I think it may depend on the version of plugins and/or Jenkins. I use Jenkins 2.164.3, Pipeline Plugin 2.6, Durable Task Plugin 1.29.