0

I'm in the process of passing a from the Jenkins Global Variable Reference variable called JOB_BASE_NAME to the groovy script. I'm using extendedChoice parameter with Groovy script and it is responsible for listing container images from the ECR on a specific repository. In my case Jenkins job names and ECR repository names are equivalent.

Ex:

I tried several things but all time I ended up with an empty response to the container images listing part.

Please help me to figure out is it outofthebox or how can i implement this thing

Thanks

Here is my Code

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"
      }
    }
  }
}

Damith Udayanga
  • 726
  • 7
  • 18

1 Answers1

0

You can leverage Groovy String Interpolation to replace the job base name in the script for the parameter, but the script can't access any variable out of the scope of the script.

You can try as following:

  • Use a function to compose the Groovy script for parameter
  • The function accept the JOB_BASE_NAME value
  • Use Groovy string interpolation to replace to real value.
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()
}
yong
  • 13,357
  • 1
  • 16
  • 27
  • Still did not work – Damith Udayanga Oct 22 '22 at 02:57
  • @DamithUdayanga, There is a tiny issue on `'--repository-name abc/${jobBaseName}',` in `def list_ecr_images(jobBaseName)`. We need to use double quotation `"--repository-name abc/${jobBaseName}"`, due to we need Groovy String Interpolation work on the groovy variable `jobBaseName`. – yong Oct 27 '22 at 01:17