jenkins 带有全局变量的ExtendedChoice参数不起作用

8aqjt8rx  于 2022-11-01  发布在  Jenkins
关注(0)|答案(1)|浏览(286)

我正在将Jenkins全局变量引用变量JOB_BASE_NAME传递给groovy脚本。我在Groovy脚本中使用extendedChoice参数,它负责列出特定存储库中ECR的容器映像。在我的示例中,Jenkins作业名称和ECR存储库名称是等效的。
例如:

我尝试了几件事,但所有的时间,我结束了一个空的响应容器图像列表部分。
请帮助我弄清楚它是开箱即用的还是我如何实现它
谢谢
这是我的代码

pipeline {
  agent {
    label 'centos7-slave'
  }

  stages {
    stage('Re Tag RELEASE TAG AS UAT') {
        environment {
          BRANCH = "${params.GITHUB_BRANCH_TAG}"
        }
        input {
          message 'Select tag'
          ok 'Release!'
          parameters {
            extendedChoice(
              bindings: '',
              groovyClasspath: '',
              multiSelectDelimiter: ',',
              name: 'DOCKER_RELEASE_TAG',
              quoteValue: false,
              saveJSONParameterToFile: false,
              type: 'PT_SINGLE_SELECT',
              visibleItemCount: 5,
              groovyScript: '''
              import groovy.json.JsonSlurper
                  def AWS_ECR = ("/usr/local/bin/aws ecr list-images --repository-name abc/${JOB_BASE_NAME}  --filter tagStatus=TAGGED --region ap-southeast-1").execute()
                  def DATA = new JsonSlurper().parseText(AWS_ECR.text)
                  def ECR_IMAGES = []
                  DATA.imageIds.each {
                      if(("$it.imageTag".length()>3))
                      {
                          ECR_IMAGES.push("$it.imageTag")
                      }
                  }
              return ECR_IMAGES.grep( ~/.*beta.*/ ).sort().reverse()
              '''
            )
          }
        }
      steps {
        script {
          def DOCKER_TAG = sh(returnStdout: true, script:"""
          #!/bin/bashF
          set -e
          set -x
          DOCKER_TAG_NUM=`echo $DOCKER_RELEASE_TAG | cut -d "-" -f1`
          echo \$DOCKER_TAG_NUM
          """)
          DOCKER_TAG = DOCKER_TAG.trim()
          DOCKER_TAG_NUM = DOCKER_TAG
        }
        sh "echo ${AWS_ECR} | docker login --username AWS --password-stdin  ${ECR}"
        sh "docker pull ${ECR}/${REPOSITORY}:${DOCKER_RELEASE_TAG}"
        sh " docker tag ${ECR}/${REPOSITORY}:${DOCKER_RELEASE_TAG} ${ECR}/${REPOSITORY}:${DOCKER_TAG_NUM}-rc"
        sh "docker push ${ECR}/${REPOSITORY}:${DOCKER_TAG_NUM}-rc"
      }
    }
  }
}
x7rlezfr

x7rlezfr1#

您可以利用Groovy String Interpolation来替换脚本中参数的作业基名称,但是脚本不能访问脚本范围之外的任何变量。
您可以尝试以下操作:

  • 使用函数为参数编写Groovy脚本
  • 该函数接受JOB_BASE_NAME
  • 使用Groovy字符串插值替换为真实的值。
pipeline {
  agent {
    label 'centos7-slave'
  }

  stages {
    stage('Re Tag RELEASE TAG AS UAT') {
        environment {
          BRANCH = "${params.GITHUB_BRANCH_TAG}"
        }
        input {
          message 'Select tag'
          ok 'Release!'
          parameters {
            extendedChoice(
              bindings: '',
              groovyClasspath: '',
              multiSelectDelimiter: ',',
              name: 'DOCKER_RELEASE_TAG',
              quoteValue: false,
              saveJSONParameterToFile: false,
              type: 'PT_SINGLE_SELECT',
              visibleItemCount: 5,
              groovyScript: list_ecr_images("${env.JOB_BASE_NAME}")
            )
          }
        }
      steps {
        script {
          def DOCKER_TAG = sh(returnStdout: true, script:"""
          #!/bin/bashF
          set -e
          set -x
          DOCKER_TAG_NUM=`echo $DOCKER_RELEASE_TAG | cut -d "-" -f1`
          echo \$DOCKER_TAG_NUM
          """)
          DOCKER_TAG = DOCKER_TAG.trim()
          DOCKER_TAG_NUM = DOCKER_TAG
        }
        sh "echo ${AWS_ECR} | docker login --username AWS --password-stdin  ${ECR}"
        sh "docker pull ${ECR}/${REPOSITORY}:${DOCKER_RELEASE_TAG}"
        sh " docker tag ${ECR}/${REPOSITORY}:${DOCKER_RELEASE_TAG} ${ECR}/${REPOSITORY}:${DOCKER_TAG_NUM}-rc"
        sh "docker push ${ECR}/${REPOSITORY}:${DOCKER_TAG_NUM}-rc"
      }
    }
  }
}

def list_ecr_images(jobBaseName) {
    def _script = """
        import groovy.json.JsonSlurper
            def AWS_ECR = [
                '/usr/local/bin/aws',
                'ecr list-images',
                "--repository-name abc/${jobBaseName}",
                '--filter tagStatus=TAGGED',
                '--region ap-southeast-1'
            ].execute().text
            def DATA = new JsonSlurper().parseText(AWS_ECR)
            def ECR_IMAGES = []
            DATA.imageIds.each {
                if((it.imageTag.length()>3))
                {
                    ECR_IMAGES.push(it.imageTag)
                }
            }
        return ECR_IMAGES.grep( ~/.*beta.*/ ).sort().reverse()
    """
    return _script.stripIndent()
}

相关问题