如何在詹金斯管道中读取子文件夹 [英] How to read subfolders in jenkins pipeline

查看:70
本文介绍了如何在詹金斯管道中读取子文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个管道jenkins作业,该作业循环通过特定目录的子文件夹并启动某些程序.问题是访问文件系统.由于某种原因,它似乎根本不读取文件系统,也不读取它自己的工作空间.

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.

更新: 我发现以这种方式给出的所有gradle/java指令都没有针对该节点,而是在主节点上运行.这就是为什么我没有找到目录的原因.所以我完全误解了管道的工作方式.

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.

说了这一点..关于如何找回它的任何想法吗?例如,是否可以通过shell步骤设置gradle属性?

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?

谢谢, 米歇尔

推荐答案

下面解决了我的问题.它可以帮助正在寻找相同物品的人. 子文件夹可以使用函数添加到列表中,该函数返回管道中的目录列表,如下所示.

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"
    }
}

这篇关于如何在詹金斯管道中读取子文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆