列出要删除到jenkins作业中的项目

zysjyyx4  于 2023-03-17  发布在  Jenkins
关注(0)|答案(2)|浏览(154)

bounty将在5天后过期。回答此问题可获得+50的声誉奖励。Peter Penzov正在寻找来自声誉良好来源的答案

我有这个jenkins作业,用于删除AWS ECR存储库中的所有项目:

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

            }                
        }    
    }
    
}

是否有可能有下拉列表,我可以选择要删除的项目?

zz2j4svz

zz2j4svz1#

是的,这是可能的,例如

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

jenkins pipeline how to give choice parameters dynamically上也提出了类似的问题
关于declarative pipeline parameters的教程

jjhzyzn0

jjhzyzn02#

你可以使用Jenkins Input Step来实现这个问题,因为我没有AWS的repo,所以我不能给予一个精确的解决方案,但是下面是你如何做到这一点。

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

  return images
}

如果要选择多个图像,请使用multiselect参数而不是选择参数。

相关问题