0

I have just started looking into a shared libarary with jenkins in order to combine a load of scripts and pipelines across multiple repos that are pretty much identical.

I have the shared lib loaded and working but when tryign to execute the scripts i the resources folder i keep geting not found errors:

../releaseTagging-EO2DMYOPJ6JGB6JT5Q2RSFJWJWWPALA7F25H7CQNYBEV4ITTEB6Q@tmp/build.sh: not found

I am creating a copy of the file using the following:

createTempLocation(String path) {
  String tmpDir = pwd tmp: true
  return tmpDir + File.separator + new File(path).getName()
}

and

copyGlobalLibraryScriptcall(String srcPath, String destPath = null) {
  destPath = destPath ?: createTempLocation(srcPath)
  writeFile file: destPath, text: libraryResource(srcPath)
  echo "copyGlobalLibraryScript: copied ${srcPath} to ${destPath}"
  sh "chmod +x ${destPath}"
  echo "added executable permissions to ${destPath}"
  return destPath
}

I am then calling the last function thusly:

runBuild(Map config) {
    def script = copyGlobalLibraryScript('build.sh')
    sh script
}

(i realise i can collapse the above function in to one line)

This in turn then gets called via (trimed the whole file to relevent part):

pipeline {
        agent any
        stages {
            stage('Build') {
                steps {
                    timestamps {
                        checkout scm

                        bbNotify( key: buildKey, name: BuildName) {
                            runBuild()
                        }

                        stash includes: '**', name: 'RelToSTAN'
                    }
                }
            }
}

This all fails with the error at the top of the question, however when sshing on to the build server i can find that file int he location specified.

I dont understand why Jenkins cannot find it and execute it.

StephenKing
  • 36,187
  • 11
  • 83
  • 112
Scott-David Jones
  • 292
  • 1
  • 6
  • 22

1 Answers1

0

The issue will be the following: When using a java File object it‘ll always refer to some location on the Jenkins master. And of course it usually cannot run inside the sandbox.

On the other hand the readFile and writeFile methods always refer to some path on the build agent reserved by the node block where the call is encapsulated.

Long story short: Do not use the File class. Unfortunately you’ll need to create the temp path manually. But that shouldn’t be too hard.

Joerg S
  • 4,730
  • 3
  • 24
  • 43