jenkins管道中简单的groovy功能不起作用

时间:2017-11-10 08:11:12

标签: curl groovy jenkins-pipeline

我尝试使用myFunction.groovy

中的curl下载文件
#!/usr/bin/env groovy

def call(String URL = 'input') {
def command="curl '${URL}' --create-dirs -o output/data.jsp"
println command.execute().text
println "ls -ltrah".execute().text
}

也尝试过:

def call(String URL = 'input') {
    echo "${URL}"
    echo "test"
    def command = ["curl", "${URL}", "--create-dirs", "-o", "output/data.jsp"].execute().text
    println "ls -ltrah".execute().text
}

此命令甚至可以在我们的jenkins slave的执行脚本部分中使用:

println "curl 'http://xxx/data.jsp' --create-dirs -o output/data.jsp".execute().text

我在jenkins管道中用以下方式调用它:

myFunction 'http://xxx/file.jsp'

每次都在后期输出:

ENOENT: no such file or directory, open 'output/data.jsp'

我做错了什么? 当我在bash命令中执行命令时,该命令有效。

1 个答案:

答案 0 :(得分:1)

<强>被修改

根据mkobit关联的Best Practices GuideFile Operations Plugin可以帮助您,查看examples

folderCreateOperation('test')
fileDownloadOperation('http://localhost/test.txt','','','test','test.txt')

<强>原始

我不会通过curl执行此操作,您可以使用Groovy SDK:

File file = new File('test/test.txt')
file.getParentFile().mkdirs()
file.write('http://www.google.com'.toURL().text)

如果您不需要创建父目录,那么这是一个单行:

new File('test.txt').write('http://www.google.com'.toURL().text)

要使用curl解决您的问题,请检查curl版本并在必要时进行更新。

相关问题