How to read subfolders in jenkins pipeline

瘦欲@ 提交于 2021-01-21 10:01:34

问题


I'm trying to write a pipeline jenkins job that loop through the subfolder of a specific directory and launch something. The problem is to access the filesystem. For some reason it does not seems to read the file system at all, neither its own workspace.

This is the snippet I'm using

node ('label') {
    workspacePath = '/opt/installersWS'
    ws(workspacePath){
        stage ("test"){
            ...some stuff...
            runtimeBuildDir = new File(workspacePath + "/components")
            echo runtimeBuildDir.getPath()
            if (runtimeBuildDir.exists()){
                echo "search for subfolders"
            } else {
                echo "main folder not existing"
            }

        }
    }
}

The folder of course is existing on the server but the run always return with the second echo.

UPDATE: I discover that all the gradle/java instruction given in this way is not targeting the node but are running on the master. This was why I didn't found the directories. So I was completely misunderstanding how the pipeline is working.

Said that.. any idea on how can I retrieve that? Is there a way to set an gradle property from the shell step for example?

Thanks, Michele


回答1:


Below solved my issue. It could help someone who is looking for the same. Sub folders can be added to list using function which returns the list of directory in pipeline like below.

@NonCPS
def readDir()
{
    def  dirsl = [] 
    new File("${workspace}").eachDir()
    {
        dirs -> println dirs.getName() 
        if (!dirs.getName().startsWith('.')) {
            dirsl.add(dirs.getName())
        }
    }
    dirsl
}

Then in your pipeline script, call the function like below and do whatever is required.

stage ('Build'){

    dirsl = readDir()
    def size = dirsl.size()
    print size
    for ( int i = 0; i < size; i++) {
        "Do whatever appropriate"
    }
}



回答2:


This is the way I solved it, running a shell that returned the output

def subfolders = sh(returnStdout: true, script: 'ls -d RuntimeBuild/*').trim().split(System.getProperty("line.separator"))

Then I was able to cicle the list with the name of the directories and manipulate them.

For Windows that could become:

def subfolders = bat(script: '@dir /B RuntimeBuild', returnStdout: true).split(/\n\r/)


来源:https://stackoverflow.com/questions/41620409/how-to-read-subfolders-in-jenkins-pipeline

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!