我正在尝试编写一个普通的DSL脚本,该脚本从yml文件中获取项目列表,并为每个项目创建一个pipelineJob。
我不知道如何从groovy脚本中将变量传递给Jenkinsfile。
这是yml:
-
name: my-first-dep
git: myaccount@vs-ssh.visualstudio.com:v3/myaccount/MyProject/my-first-dep
branch: develop
-
name: my-second-dep
git: myaccount@vs-ssh.visualstudio.com:v3/myaccount/MyProject/my-second-dep
branch: develop
这是常规脚本:
@Grab('org.yaml:snakeyaml:1.17')
import org.yaml.snakeyaml.Yaml
dependencies = new Yaml().load(readFileFromWorkspace("dep.yml"))
String folder = "dep-folder";
dependencies.each{ v ->
name = v['name']
git = v['git']
branch = v['branch']
createPipelineBuildJob(this, name, git, branch, folder)
}
def createPipelineBuildJob(def context, String name, String git, String branch, String folder){
pipelineJob(folder+"/"+name+"-build") {
environmentVariables{
envs('GIT_URL': git, 'GIT_BRANCH': branch)
}
definition {
cps {
script(context.readFileFromWorkspace('Jenkinsfile'))
sandbox()
}
}
}
}
这是Jenkins文件:
pipeline {
agent any
stages {
stage('Cloning Git') {
steps {
git branch: "${env.GIT_BRANCH}", url: "${env.GIT_URL}" // IT DOESN'T TAKE THIS ENV VARIABLE FROM GROOVY SCRIPT
}
}
stage ('Artifactory configuration') {
steps {
rtMavenDeployer (
id: "MAVEN_DEPLOYER",
serverId: "artifactory",
releaseRepo: "libs-release-local",
snapshotRepo: "libs-snapshot-local"
)
rtMavenResolver (
id: "MAVEN_RESOLVER",
serverId: "artifactory",
releaseRepo: "libs-release",
snapshotRepo: "libs-snapshot"
)
}
}
stage('Build') {
steps {
rtMavenRun (
tool: 'M3',
pom: 'pom.xml',
goals: 'clean install -U',
deployerId: "MAVEN_DEPLOYER",
resolverId: "MAVEN_RESOLVER"
)
}
}
stage ('Publish build info') {
steps {
rtPublishBuildInfo (
serverId: "artifactory"
)
}
}
}
}