持续集成交付CICD: Sonarqube REST API 查找与新增项目

目录

一、实验

[1.SonarQube REST API 查找项目](#1.SonarQube REST API 查找项目)

[2.SonarQube REST API 新增项目](#2.SonarQube REST API 新增项目)


一、实验

1.SonarQube REST API 查找项目

(1)Postman测试

转换成cURL代码

(2)Jenkins添加凭证

(3)修改流水线

bash 复制代码
pipeline {

    agent { label "build"}

    stages {
        stage("Sonar"){
            steps{
                script {
                   projectName = "devops03-maven5-service"
                   if (SearchProject(projectName)){
                       println("${projectName} exist")
                   } else {
                       println("${projectName} not fonud")
                   }

                }
            }
        }
    }
}

def SonarRequest(apiUrl,method){
    withCredentials([string(credentialsId: "855da77c-ad38-4f06-a941-3a5afcf6bbc6", variable: 'SONAR_TOKEN')]) {
        sonarApi = "http://192.168.204.8:9000/api"
        response = sh returnStdout: true,
                script: """
                 curl --location \
                 --request ${method} \
                 "${sonarApi}/${apiUrl}" \
                 --header "Authorization: Basic ${SONAR_TOKEN}"
                 """
        // json格式化
        try {
            response = readJSON text: """ ${response - "\n"} """
        } catch(e){
            response = readJSON text: """{"errors" : true}"""
        }
        return response

    }
}

//查找项目
def SearchProject(projectName){
    apiUrl = "projects/search?projects=${projectName}"
    response = SonarRequest(apiUrl,"GET")
    if (response.paging.total == 0){
        return false
    }
    return true

}

(4)任意项目中回放进行测试

(5)成功

2.SonarQube REST API 新增项目

(1)Postman测试

(2)发送转换成cURL代码

(3)Sonarqube已新增go项目

(4)修改流水线

bash 复制代码
pipeline {

    agent { label "build"}

    stages {
        stage("Sonar"){
            steps{
                script {
//                    projectName = "devops03-maven5-service"
                    projectName = "devops03-gradle-service"
                   if (SearchProject(projectName)){
                       println("${projectName} exist")
                   } else {
                       println("${projectName} not fonud")
                       //项目不存在
                       CreateProject(projectName)
                   }

                }
            }
        }
    }
}

def SonarRequest(apiUrl,method){
    withCredentials([string(credentialsId: "855da77c-ad38-4f06-a941-3a5afcf6bbc6", variable: 'SONAR_TOKEN')]) {
        sonarApi = "http://192.168.204.8:9000/api"
        response = sh returnStdout: true,
                script: """
                 curl --location \
                 --request ${method} \
                 "${sonarApi}/${apiUrl}" \
                 --header "Authorization: Basic ${SONAR_TOKEN}"
                 """
        // json格式化
        try {
            response = readJSON text: """ ${response - "\n"} """
        } catch(e){
            response = readJSON text: """{"errors" : true}"""
        }
        return response

    }
}

//查找项目
def SearchProject(projectName){
    apiUrl = "projects/search?projects=${projectName}"
    response = SonarRequest(apiUrl,"GET")
    if (response.paging.total == 0){
        return false
    }
    return true

}

//创建项目
def CreateProject(projectName){
    apiUrl = "projects/create?name=${projectName}&project=${projectName}"
    response = SonarRequest(apiUrl,"POST")
    try{
        if (response.project.key == projectName ) {
            println("Project Create success!...")
            return true
        }
    }catch(e){
        println(response.errors)
        return false
    }
}

(5)任意项目中回放进行测试

(6)成功

(7)Sonarqube已新增gradle项目

相关推荐
jingyu飞鸟34 分钟前
linux系统源代码安装apache、编译隐藏版本号
linux·运维·apache
2401_858286112 小时前
OS15.【Linux】gdb调试器的简单使用
linux·运维·服务器·开发语言·gdb
c30%003 小时前
内网渗透——红日靶场五
运维·服务器
zjw_rp4 小时前
centos停止维护后更换yum源
linux·运维·centos
宇钶宇夕4 小时前
EPLAN 电气制图:建立自己的部件库,添加部件-加SQL Server安装教程(三)上
运维·服务器·数据库·程序人生·自动化
susu10830189114 小时前
Debian 11 Bullseye 在线安装docker
运维·docker·debian
love530love5 小时前
Docker 稳定运行与存储优化全攻略(含可视化指南)
运维·人工智能·windows·docker·容器
岁岁岁平安5 小时前
CentOS-7-x86_64解决:使用NAT模式无法ping通www.baidu.com或无法ping 8.8.8.8问题。
linux·运维·centos·centos-7
运维小贺5 小时前
各服务器厂商调整BIOS睿频教程
linux·运维·服务器·性能优化
网硕互联的小客服5 小时前
如何排查服务器中已经存在的后门程序?
运维·服务器·github