天天看點

Jenkins REST API

jenkins api也提供了類似于SDK,有py,go,java的。基于Jenkins去開發,使用SDK就行了。一般不會通過這種API方式去通路,用SDK更加安全一些。SDK别人幫你寫好了,對于項目內建更加友善。

WEB API寫一些腳本的時候可能會用到。

也可以對Jenkins進行插件的開發,這些也可以基于共享庫或者函數來實作,隻不過在界面上通過添加參數來實作。這樣也能夠實作類似插件的功能。

你想看某個作業的資訊,後面加上api就行了。

Jenkins REST API

項目API

可以将這些api封裝到共享庫裡面去 

擷取項目資訊 

  • 接口:​​http://127.0.0.1:8080/job/{projectName}/api/json​​
  • 方式:GET
  • 執行個體:​​http://127.0.0.1:8080/job/demo-test-03/api/json​​
Jenkins REST API
curl --location --request GET 'http://139.198.166.235:8080/job/acmp-myapp-service/api/json' \
--header 'Authorization: Basic YWRtaW46YWRtaW4='      
"displayName": "acmp-myapp-service",

pipeline {
    agent any

    stages {
        stage('Hello') {
            steps {
                script{
                    response1= sh returnStdout: true, script: 
                    """
                    curl --location \
                    --request GET \
                    http://139.198.166.235:8080/job/acmp-myapp-service/api/json \
                    --header \'Authorization: Basic YWRtaW46YWRtaW4='
                    """
                    response1 = response1 - "\n"
                    println(response1)

                    result = readJSON text: "${response1}"
                    println(result.displayName)
                }
            }
        }
    }
}      

 擷取項目建構資訊 

  • 接口:​​http://127.0.0.1:8080/job/{projectName}/{buildNumber}/api/json​​
  • 執行個體:​​http://127.0.0.1:8080/job/test-devops-service/1/api/json​​
  • 方式:GET
curl --location --request GET 'http://139.198.166.235:8080/job/acmp-myapp-service/30/api/json' \
--header 'Authorization: Basic YWRtaW46YWRtaW4='      

擷取項目配置

  • 接口:​​http://127.0.0.1:8080/job/{projectName}/config.xml​​
  • 執行個體:​​http://127.0.0.1:8080/job/test-devops-service/config.xml​​
  • 方式:GET
curl --location --request GET 'http://139.198.166.235:8080/job/acmp-myapp-service/config.xml' \
--header 'Authorization: Basic YWRtaW46YWRtaW4=' \
--header 'Cookie: JSESSIONID.73190a41=node0bg89j02scsymdz0l1n2qv36s15.node0'      
  • 接口:​​http://127.0.0.1:8080/createItem?name={projectName}​​
  • 參數: --data-binary​​@config.xml ​​
  • 頭部:-H  "Content-Type:text/xml"
  • 方式: POST