我有这个步骤
def createJob(def jobName,
def branchName) {
job(jobName) {
steps {
shell('export AWS_DEFAULT_REGION=eu-west-1')
shell('$(aws ecr get-login --region eu-west-1)')
shell('docker build -t builder -f ./images/'+branchName+'/Dockerfile .')
shell('docker tag -f '+branchName+':latest *******.dkr.ecr.eu-west-1.amazonaws.com/'+branchName+':latest')
shell('docker push *********.dkr.ecr.eu-west-1.amazonaws.com/'+branchName+':latest)')
}
}
}
我怎样才能将所有这些添加到一个 shell 中?
我试过这个方法
shell( '''
export AWS_DEFAULT_REGION=eu-west-1
$(aws ecr get-login --region eu-west-1)
docker build -t builder -f ./images/'+branchName+'/Dockerfile .
''')
但是变量branchName 被视为字符串。 问候。
最佳答案
改用双引号,它支持插值(单引号和单三引号不支持)。然后你可以使用${}
在字符串中插入变量
shell( """
export AWS_DEFAULT_REGION=eu-west-1
$(aws ecr get-login --region eu-west-1)
docker build -t builder -f ./images/${branchName}/Dockerfile .
""")
有关更多信息,请参阅groovy documentation on string interpolation .
关于jenkins - 将 dsl 工作 shell 合并为一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37071261/