您可以将逻辑编写在Groovy文件中,该文件可以保存在Git存储库,管道共享库或其他地方。
例如,如果您
utils.groovy的存储库中有文件:
List<Integer> myNumbers() { return [1, 2, 3, 4, 5]}return this在您的中
Jenkinsfile,您可以通过以下
load步骤使用它:
def utilsnode { // Check out repository with utils.groovy git 'https://github.com/…/my-repo.git' // Load definitions from repo utils = load 'utils.groovy'}// Execute utility methoddef 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`…}


