2

I have this jenkins job which is used to delete all items into AWS ECR repository:

pipeline {
    agent any
    environment {   
      AWS_ACCOUNT_ID="0000000021764"
      AWS_DEFAULT_REGION="us-east-1"
      IMAGE_REPO_NAME="testio"
      IMAGE_TAG="latest"
      REPOSITORY_URI = "0000000021764.dkr.ecr.us-east-1.amazonaws.com/testio:latest"
      REPOSITORY_NAME = "testio"
    }
    
  stages{    
    stage('Delete all image in repository') {
            steps {
                
                script{
                        docker.withRegistry('https://0000000021764.dkr.ecr.us-east-1.amazonaws.com') {
                         sh '''
                             aws ecr batch-delete-image --repository-name $REPOSITORY_NAME --image-ids "$(aws ecr list-images --repository-name $REPOSITORY_NAME --query 'imageIds[*]' --output json
                                )" || true
                         '''
                    }
                }

            }                
        }    
    }
    
}

Is it possible to have dropdown list from which I can select which item to be deleted?

Peter Penzov
  • 1,126
  • 134
  • 430
  • 808

2 Answers2

1

You can use Jenkins Input Step for this. Since I don't have an AWS repo I can't give an exact solution, but the following is how you can do this.

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

  stages{
    stage('Delete all image in repository') {
            steps {

                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')]
                        docker.withRegistry('https://0000000021764.dkr.ecr.us-east-1.amazonaws.com') {
                         sh """
                             aws ecr batch-delete-image --repository-name $REPOSITORY_NAME --image-ids \"$INPUT_IMAGE_TO_DELETE\" || true
                         """
                    }
                }

            }
        }
    }

}

def getDockerImages() {
  def images = []
  def output = sh(returnStdout: true, script: "aws ecr list-images --repository-name $REPOSITORY_NAME --query 'imageIds[*]'").trim()
  // Process the output to have a List of images
  println output
  return images
}

If you want to select multiple images use the multiselect Parameter instead of Choice Parameter. Make sure you change the REPOSITORY_NAME and the REGION to what ever values are applicable to you.

Update

Here is the full working example to delete the selected tags.

pipeline {
    agent any

    stages {
        stage('Hello') {
            steps {
                echo 'Hello World'
                script {
                    REPOSITORY_NAME = 'ycr-repository'
                    REGION = 'us-east-1'
                    INPUT_IMAGE_TO_DELETE = input message: 'Please Select Docker Image to delete', ok: 'Next',
                                                          parameters: [
                                                          choice(name: 'IMAGE_NAME', choices: getDockerImages(REPOSITORY_NAME), description: 'Select Docker Image to delete')]
                                                    
                    
                    echo "Deleting the image: ${INPUT_IMAGE_TO_DELETE}" 
                    sh "aws ecr batch-delete-image --repository-name $REPOSITORY_NAME --image-ids imageTag=$INPUT_IMAGE_TO_DELETE --region $REGION"
                }
            }
        }
    }
}

def getDockerImages(REPOSITORY_NAME) {
    output = sh(returnStdout: true, script: "aws ecr list-images --repository-name $REPOSITORY_NAME --output json").trim()
    json = readJSON text: output
    println json
    return json.imageIds.imageTag
}
ycr
  • 12,828
  • 2
  • 25
  • 45
  • I tested it but unfortunately the dropdown list is empty – Peter Penzov Mar 15 '23 at 12:49
  • @PeterPenzov can you add a few logs in the `getDockerImages` and see what you are returning? Or print the `output` in the same method and share, it so I can provide a concrete answer. – ycr Mar 15 '23 at 13:04
  • Cn you give me example how to add debug? I'm not familiar with this script syntax. – Peter Penzov Mar 17 '23 at 22:12
  • @PeterPenzov you can simply use a println statement, check the updated getDockerImages function. – ycr Mar 17 '23 at 22:18
  • ok, I get this output: https://pastebin.com/VwiU9KFL I need to display `0.0.1-SNAPSHOT` as a dropdown item? How I can display it? – Peter Penzov Mar 17 '23 at 23:33
  • @PeterPenzov check the answer here. https://stackoverflow.com/a/75129937/2627018 – ycr Mar 17 '23 at 23:43
  • I have a question: How full URL can be used for repository not only the repository name? – Peter Penzov Mar 19 '23 at 20:41
  • @PeterPenzov That I'm not sure, but this is what the document has. https://docs.aws.amazon.com/AmazonECR/latest/userguide/delete_image.html – ycr Mar 19 '23 at 21:02
0

Yes it's possible, example

stage('Release MoM EE Docker Image') {
  if (env.BRANCH_NAME == 'master' || env.BRANCH_NAME ==~ /releases\/.*/) {
    version = sh(script: "./version docker", returnStdout: true).trim()
    build(
         job: '/marathon-dcos-plugins/release-mom-ee-docker-image/master',
         parameters: [string(name: 'from_image_tag', value: version)],
         propagate: true
    )
  }
}

Similar question asked here on jenkins pipeline how to give choice parameters dynamically

Tutorial on declarative pipeline parameters

hmhkata
  • 21
  • 1
  • 5