小编典典
您可以将逻辑编写在Groovy文件中,该文件可以保存在Git存储库,管道共享库或其他地方。
例如,如果您utils.groovy的存储库中有文件:
List myNumbers() {
return [1, 2, 3, 4, 5]
}
return this
在您的中Jenkinsfile,您可以通过以下load步骤使用它:
def utils
node {
// Check out repository with utils.groovy
git 'https://github.com/…/my-repo.git'
// Load definitions from repo
utils = load 'utils.groovy'
}
// Execute utility method
def numbers = utils.myNumbers()
// Do stuff with `numbers`…
另外,您可以签出Java代码并运行它,然后捕获输出。然后,您可以将其解析为列表,或稍后在管道中所需的任何数据结构。例如:
node {
// Check out and build the Java tool
git 'https://github.com/…/some-java-tools.git'
sh './gradlew assemble'
// Run the compiled Java tool
def output = sh script: 'java -jar build/output/my-tool.jar', returnStdout: true
// Do some parsing in Groovy to turn the output into a list
def numbers = parseOutput(output)
// Do stuff with `numbers`…
}
2020-07-25