jenkins - 当这两个文件在不同的作业中时,如何将参数从 Jenkins 文件传递到 Groovy 脚本文件
问题描述
我有两个 Jenkins 工作,比如 Job1 和 Job2。Job1 使用 Jenkinsfile 执行,Job2 使用 Groovy 脚本执行。
使用的参数是键值对类型的,这些参数将从 Jenkinsfile 传递到 Groovy Script,从而使 Job2 与 Job1 并行运行。
请提出一种方法来完成上述场景。
下面是我的代码:
Job1 中使用的 Jenkins(caller) 文件:
#!groovy
library <internal library I am using>
pipeline {
agent any
stages {
stage ('callParallelJob') {
steps {
script {
------- Some code------
def gitCommitterEmail = sh (
script: 'git --no-pager show -s --format=\'%ae\'',
returnStdout: true
).trim()
echo "git committer: ${gitCommitterEmail}"
appConfig.gitCommitterEmail = gitCommitterEmail
def gitBranch = "${env.GIT_BRANCH}".trim()
appConfig.gitBranch = gitBranch
def jenkinsBuildUrl = "${env.BUILD_URL}".trim()
appConfig.jenkinsBuildUrl = jenkinsBuildUrl
def ciData = [
'git': [
'git_repo': "${git_repo}",
'git_branch': "${env.BRANCH_NAME}",
'git_committer': "${gitCommitterEmail}",
'git_commit': "${GIT_COMMIT}"
],
'jenkins': [
'build_url': "${env.BUILD_URL}",
'job_name': "${JOB_NAME}",
'timestamp': "${BUILD_TIMESTAMP}"
]
]
writeYaml file: 'ci.yml', data: ciData
echo 'calling another job'
build job: '<job2Namehere>, parameters: [
string(name: 'Testparam', value: "123"),
], propagate: true, wait: false
echo 'called another job'
}
}
}
我想将 appConfig 和 ci.yml 数据传递给下面的 groovy 脚本
Job2 中使用的 Groovy 文件:
pipeline {
agent any
stages {
stage('Experiment'){
steps{
script{
sh 'echo ${Testparam}'
-------Some code here-------
}}}}}
解决方案
您需要将调用的作业配置为接受参数,然后您的代码应该可以工作。