Jenkins 插件 Extended Choice Parameter

        stage("DeployHosts"){
            steps {
                script {
                  println(env.deployHosts)
                  for (host in "${env.deployHosts}".split(",")){
                      sh "echo ${host}"
                  }
                }
            }

 

 

高级一点的用法,input和Extended Choice Parameter结合

/*
清理docker镜像

1. 获取镜像列表
2. 用户选择删除
3. 调用api删除

*/
pipeline {
    agent {
        label "build"
    }

    stages{

        stage("GetTags"){
            steps{
                script{
                    env.projectName = "library"
                    env.repoName = "sonarqube"
                    env.result = GetArtifactTag(env.projectName, env.repoName)
                    env.result = env.result - '[' - ']'
                    println(result) // 8.9, 8.9.1, 8.9.2-community
                }
            }
        }

        stage("Clean"){
            steps{
                script{

                    def result = input  message: "是否删除${env.projectName}项目的${env.repoName}这些标签:", 
                                        parameters: [extendedChoice(defaultValue: "${env.result}", 
                                                                    multiSelectDelimiter: ',', 
                                                                    name: 'taga', 
                                                                    quoteValue: false, 
                                                                    saveJSONParameterToFile: false, 
                                                                    type: 'PT_CHECKBOX', 
                                                                    value: "${env.result}", 
                                                                    visibleItemCount: 20)]
                    println("${result}")
                    // println("Delete  ${taga}, doing.......")
                    // tags = "${taga}" - '[' - ']'

                    for(t in result.split(',')){
                        println("Delete >>>>" + t.trim())
                        DeleteArtifactTag(env.projectName,env.repoName, t.trim())
                    }
                }
            }

        }
    }
}


// 删除镜像tag
def DeleteArtifactTag(projectName,repoName, tagName){
    harborAPI = "http://192.168.1.200:8088/api/v2.0/projects/${projectName}/repositories/${repoName}"
    apiURL = "artifacts/${tagName}/tags/${tagName}"
    sh """ curl -X DELETE "${harborAPI}/${apiURL}" -H "accept: application/json"  -u admin:Harbor12345 """
}


// 获取镜像的所有标签
// acmp-nginx-service
def GetArtifactTag(projectName,repoName ){
    harborAPI = "http://121.40.102.116:8888/api/v2.0/projects/${projectName}/repositories/${repoName}"
    apiURL = "artifacts?page=1&page_size=10"
    response = sh returnStdout: true, script:  """curl -X GET "${harborAPI}/${apiURL}" -H "accept: application/json" -u admin:Harbor12345 """
    response = readJSON text: """${response - "\n"}""" 
    tags = []
    for (t in response[0].tags){
        tags << t.name
    }

    return tags   // [8.9, 8.9.1, 8.9.2-community]
}

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值