美文网首页
jenkins harbor操作

jenkins harbor操作

作者: _fishman | 来源:发表于2020-05-25 13:33 被阅读0次
    #!groovy
    @Library('jenkinslibrary@master') _
    
    def tools = new org.devops.tools()
    
    String registryName = "${env.registryName}"
    String serviceName = "${env.serviceName}"
    String tagName = "${env.tagName}"
    def harborProjects = []
    
    currentBuild.description = "Trigger by ${serviceName} ${tagName}"
    
    
    pipeline {
        agent { node { label "build"} }
    
        stages{
    
            stage("GetHarborTags"){
                steps{
                    timeout(time:5, unit:"MINUTES"){
                        script{
                            tools.PrintMes("获取Harbor仓库中的项目信息","green")
                            println(serviceName)
    
                            try {
                                response = httpRequest authentication: 'harbor-admin,
                                                       url: "https://registry.demo.com/api/repositories/${registryName}/${serviceName}/tags",
                                                       ignoreSslErrors: true
                                //println(response.content)
                                response = readJSON text: """${response.content}"""
    
                            } catch(e){
                                response = ['name':'']
                                println(e)
                                println("Harbor镜像不存在此标签!")
    
                            }
    
                            /*println(tagName)
                            for (tagname in response){
                                //println(response)
                                harborProjects << tagname['name']
                            }
    
                            println(harborProjects)*/
    
                        }
                    }
                }
            }
    
    
    
            stage("DeleteHarborTags"){
                steps{
                    timeout(time:20, unit:"MINUTES"){
                        script{
                            tools.PrintMes("总共找到 ${harborProjects.size()} 个标签","green")
                            sumImageNum = harborProjects.size()
                            for (tag in harborProjects){
                                sumImageNum -= 1
    
                                tools.PrintMes(" ${sumImageNum}  Delete Tags ---> ${registryName} --> ${serviceName} --> ${tag} ","green")
    
                                httpRequest httpMode: 'DELETE',
                                           authentication: 'c016027e-0573-4246-93cf-f4a55b08a86a',
                                           url: "https://registry.demo.com/api/repositories/${registryName}/${serviceName}/tags/${tag}",
                                           ignoreSslErrors: true
    
                                sleep 1
                            }
                        }
                    }
                }
            }
        }
    
    
        post {
            always{
                script{
                    cleanWs notFailBuild: true 
                }
            }
        }
    }
    
    
    
    
    
    

    相关文章

      网友评论

          本文标题:jenkins harbor操作

          本文链接:https://www.haomeiwen.com/subject/jylyahtx.html