Jenkins REST API

文章目录

    • Token
      • 生成token
    • credentials
      • 创建证书
      • 获取证书
      • 删除证书
    • job
      • 获取job
      • 新建job
      • 删除job
      • 更新job
    • Build Job
      • 触发构建(无参)
      • 触发构建(带参)
    • 视图
      • 将job放入视图
      • 将job从视图中移除
    • 用户
      • 创建用户
      • 删除用户

Token

点击用户小头像,设置API token.
Jenkins REST API_第1张图片

之后使用Postman采用basic auth方式,输入用户名和token.
Jenkins REST API_第2张图片

生成token

管理员用户无法为其他用户生成token,只能用户去jenkins配

POST /user/${USER_NAME}/descriptorByName/jenkins.security.ApiTokenProperty/generateNewToken?newTokenName=${TOKEN_NAME}

credentials

创建证书

路径:

# 样例路径 
#http://192.168.10.3:8080/user/oneslide/credentials/store/user/domain/_/createCredentials
POST <path to context>/credentials/store/<store id>/domain/<domain name>/createCredentials

请求体:

<com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl>
  <scope>GLOBALscope>
  <id>my-credentials-example-idid>
  <description>This is an example from REST APIdescription>
  <username>adminusername>
  <password>
     icywater
  password>
com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl>

获取证书

GET /user/oneslide/credentials/store/user/domain/_/credential/gitlab/config.xml

其中gitlab为credential的名字,oneslide为用户名。

删除证书

POST 
/user/oneslide/credentials/store/user/domain/_/credential/${credentials_id}/doDelete

job

获取job

GET /job/${JOB_NAME}/config.xml

样例返回:

样例返回的xml可以用于创建job


<flow-definition plugin="[email protected]">
    <actions>
        <org.jenkinsci.plugins.pipeline.modeldefinition.actions.DeclarativeJobAction plugin="[email protected]"/>
        <org.jenkinsci.plugins.pipeline.modeldefinition.actions.DeclarativeJobPropertyTrackerAction plugin="[email protected]">
            <jobProperties/>
            <triggers/>
            <parameters/>
            <options/>
        org.jenkinsci.plugins.pipeline.modeldefinition.actions.DeclarativeJobPropertyTrackerAction>
    actions>
    <description>description>
    <keepDependencies>falsekeepDependencies>
    <properties>
        <hudson.plugins.jira.JiraProjectProperty plugin="[email protected]"/>
        <hudson.security.AuthorizationMatrixProperty>
            <inheritanceStrategy class="org.jenkinsci.plugins.matrixauth.inheritance.InheritParentStrategy"/>
            <permission>com.cloudbees.plugins.credentials.CredentialsProvider.View:csspermission>
            <permission>hudson.model.Item.Discover:csspermission>
            <permission>hudson.model.Item.Read:csspermission>
        hudson.security.AuthorizationMatrixProperty>
    properties>
    <definition class="org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition" plugin="[email protected]">
        <script>pipeline {
    agent any

    tools {
        // Install the Maven version configured as "M3" and add it to the path.
        maven &quot;maven&quot;
    }

    stages {
        stage(&apos;Build&apos;) {
            steps {
                // Get some code from a GitHub repository
                git &apos;http://gitlab.cicd.com/root/sample-app.git&apos;
            
                // Run Maven on a Unix agent.
                sh &quot;mvn -Dmaven.test.failure.ignore=true clean package&quot;
            
                // To run Maven on a Windows agent, use
                // bat "mvn -Dmaven.test.failure.ignore=true clean package"
            }

            post {
                // If Maven was able to run the tests, even if some of the test
                // failed, record the test results and archive the jar file.
                success {
                    junit &apos;**/target/surefire-reports/TEST-*.xml&apos;
                    archiveArtifacts &apos;target/*.jar'
                }
            }
        }
    }
}
script>
        <sandbox>truesandbox>
    definition>
    <triggers/>
    <disabled>falsedisabled>
flow-definition>

新建job

POST /createItem?name=${JOB_NAME}

请求体:

//刚刚的获取job信息的响应体。

删除job

POST /job/${JOB_NAME}/doDelete

更新job

POST /job/${JOB_NAME}/config.xml

请求体:

获取job接口的响应xml

Build Job

触发构建(无参)

POST /job/${JOB_NAME}/build?delay=0sec

触发构建(带参)

POST /job/${JOB_NAME}/buildWithParameters?delay=0sec

请求体:
form-data格式,注意参数可以为空,但是不能少。

Jenkins REST API_第3张图片
其中参数在job config.xml中可以获取到。

Jenkins REST API_第4张图片

视图

将job放入视图

POST view/${VIEW_NAME}/addJobToView?name=${JOB_NAME}

将job从视图中移除

POST /view/${VIEW_NAME}/removeJobFromView?name=${JOB_NAME}

用户

创建用户

POST /securityRealm/createAccountByAdmin

请求体格式: form data

样例:
Jenkins REST API_第5张图片

删除用户

POST /securityRealm/user/${USER_NAME}/doDelete

你可能感兴趣的:(构建工具)