0

I want to implement jenkins job which can delete docker image in aws ecr using drop down menu:

pipeline {
    agent any
    environment {
      AWS_ACCOUNT_ID="123456"
      AWS_DEFAULT_REGION="us-east-1"
      IMAGE_NAME="testio"
      IMAGE_TAG="latest"
      REPOSITORY_URI = "123456.dkr.ecr.us-east-1.amazonaws.com/testio:latest"
      REPOSITORY_NAME = "testio"
    }

    stages {
        stage('Hello') {
            steps {
                echo 'Hello World'
                script {
                    INPUT_IMAGE_TO_DELETE = input message: 'Please Select Docker Image to delete', ok: 'Next',
                                                          parameters: [
                                                          choice(name: 'IMAGE_NAME', choices: getDockerImages(), description: 'Select Docker Image to delete')]
                                                          
                    aws ecr batch-delete-image --region $AWS_DEFAULT_REGION --repository-name $REPOSITORY_URI --image-ids "$REPOSITORY_URI"
                                      
                }
            }
        }
    }
}

def getDockerImages() {
    output = sh(returnStdout: true, script: "aws ecr list-images --repository-name testio --output json").trim()
    json = readJSON text: output
    println json
    return json.imageIds.imageTag
}

Image name is populated into drop down. I get this error:

groovy.lang.MissingPropertyException: No such property: ecr for class: groovy.lang.Binding
    at groovy.lang.Binding.getVariable(Binding.java:63)
    at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetProperty(SandboxInterceptor.java:285)

Do you know how I can fix this?

Peter Penzov
  • 1,126
  • 134
  • 430
  • 808
  • Can you provide the consecutive stages. The jenkinsfile only shows how you get the list of images and your choice parameter stage. Where is the stage that requests the delete ? – djmonki Mar 19 '23 at 05:18
  • Sorry. Missed that part. Post is updated. – Peter Penzov Mar 19 '23 at 08:53
  • @PeterPenzov Answer here should address your issue. https://stackoverflow.com/a/75744706/2627018 – ycr Mar 19 '23 at 18:10

0 Answers0