如何在 Jenkins 中使用 Pipeline 插件调用 Jenkinsfile 中的 java 函数
How to call java function in Jenkinsfile using Pipeline plugin in Jenkins
我在 jenkins 中使用管道插件。我的 Jenkinsfile
有 numToEcho =1,2,3,4
但我想调用 Test.myNumbers()
来获取值列表。
- 如何在 Jenkinsfile 中调用 myNumbers() java 函数?
- 或者我是否需要一个单独的 groovy 脚本文件,我应该将该文件放在 java 具有测试 class 的 jar 中?
我的 Jenkins 文件:
def numToEcho = [1,2,3,4]
def stepsForParallel = [:]
for (int i = 0; i < numToEcho.size(); i++) {
def s = numToEcho.get(i)
def stepName = "echoing ${s}"
stepsForParallel[stepName] = transformIntoStep(s)
}
parallel stepsForParallel
def transformIntoStep(inputNum) {
return {
node {
echo inputNum
}
}
}
import com.sample.pipeline.jenkins
public class Test{
public ArrayList<Integer> myNumbers() {
ArrayList<Integer> numbers = new ArrayList<Integer>();
numbers.add(5);
numbers.add(11);
numbers.add(3);
return(numbers);
}
}
您可以在 Groovy 文件中编写您的逻辑,您可以将其保存在 Git 存储库、Pipeline Shared Library 或其他地方。
例如,如果您的存储库中有文件 utils.groovy
:
List<Integer> myNumbers() {
return [1, 2, 3, 4, 5]
}
return this
在您的 Jenkinsfile
中,您可以通过 load
step 像这样使用它:
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`…
}
我在 jenkins 中使用管道插件。我的 Jenkinsfile
有 numToEcho =1,2,3,4
但我想调用 Test.myNumbers()
来获取值列表。
- 如何在 Jenkinsfile 中调用 myNumbers() java 函数?
- 或者我是否需要一个单独的 groovy 脚本文件,我应该将该文件放在 java 具有测试 class 的 jar 中?
我的 Jenkins 文件:
def numToEcho = [1,2,3,4]
def stepsForParallel = [:]
for (int i = 0; i < numToEcho.size(); i++) {
def s = numToEcho.get(i)
def stepName = "echoing ${s}"
stepsForParallel[stepName] = transformIntoStep(s)
}
parallel stepsForParallel
def transformIntoStep(inputNum) {
return {
node {
echo inputNum
}
}
}
import com.sample.pipeline.jenkins
public class Test{
public ArrayList<Integer> myNumbers() {
ArrayList<Integer> numbers = new ArrayList<Integer>();
numbers.add(5);
numbers.add(11);
numbers.add(3);
return(numbers);
}
}
您可以在 Groovy 文件中编写您的逻辑,您可以将其保存在 Git 存储库、Pipeline Shared Library 或其他地方。
例如,如果您的存储库中有文件 utils.groovy
:
List<Integer> myNumbers() {
return [1, 2, 3, 4, 5]
}
return this
在您的 Jenkinsfile
中,您可以通过 load
step 像这样使用它:
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`…
}