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

后端 未结 2 1602
心在旅途
心在旅途 2020-12-19 11:47

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

<         


        
2条回答
  •  攒了一身酷
    2020-12-19 12:26

    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"
    

提交回复
热议问题