将 dsl 作业外壳合并为一个

Merge dsl job shells in just one

我有这一步

 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