Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

jenkins - How to load files from resources folder in Shared library without knowing their names (or number)?

As you know, in Shared libraries in Jenkins, it is possible to load a resource (located in resources folder) by doing:

libraryResource("script.sh")

Now my use case is that I want to load number of files inside a folder under resources :

+ resources
 + teamA
  + script1.sh
  + script2.sh

And I want to load all those files before doing anything : I did a method in the shared library:

new File(scriptsFolder).eachFile() { file->

 writeFile([file:"${env.workspace}/${file.getName()}",text:libraryResource("$scriptsFolder/${file.getName()}")])
 sh("chmod +x ${env.workspace}/${file.getName()}")
}

where scriptsFolder= "teamA"

Of cource I'm getting java.io.IOException: Is a directory Because libraryResource must get a file path parameter.

So is, there a way to load all those files without knowing their names or their number?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can get absolute path of your shared library using Groovy @SourceURI annotation:

// vars/get_resource_dir.groovy
import groovy.transform.SourceURI
import java.nio.file.Path
import java.nio.file.Paths

class ScriptSourceUri {
    @SourceURI
    static URI uri
}

def call() {
    Path scriptLocation = Paths.get(ScriptSourceUri.uri)
    return scriptLocation.getParent().getParent().resolve('resources').toString()
}

Using the path you can invoke your scripts as usual:

sh "${get_resource_dir()}/com/example/test.sh"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...