reference:

to get crumb and sessoin :

safeRestart via API

$ SERVER='https://localhost:443'
$ COOKIEJAR="$(mktemp)"
$ CRUMB=$(curl --cookie-jar "${COOKIEJAR}" \
               "https://${SERVER}/crumbIssuer/api/json" |
               jq -r '.crumbRequestField + ":" + .crumb'
         )
$ curl -v \
      -X POST \
      --cookie "${COOKIEJAR}" \
      -H "${CRUMB}" \
      -H "Content-Type: application/json" \
      -H "Accept: application/json"
      https://${SERVER}/safeRestart

execute Groovy script with an API call

$ curl -v \
       --user username:ApiToken \
       -d "script=$(cat /tmp/script.groovy)" \
       --cookie "${COOKIEJAR}" \
       -H "${CRUMB}" \
       http://JENKINS_URL/scriptText

# or
$ curl -v \
       --user username:ApiToken \
       -d "script=println 'this script works'" \
       --cookie "${COOKIEJAR}" \
       -H "${CRUMB}" \
       http://JENKINS_URL/scriptText

stop build via api

api comments
BUILD_URL/stop abort a build
BUILD_URL/term forcibly terminate a build
BUILD_URL/kill hard kill a pipeline

get builds information

[!TIP] reference:

  • via job api

    $ curl -sSLg \
           --cookie "${COOKIEJAR}" \
           -H "${CRUMB}" \
           http://jenkins:8080/job/my-job/api/json?tree=builds[id,number,duration,timestamp,builtOn]
    
  • get particular fields for all builds

    [!TIP] api format: api/json?tree=allBuilds[Bartifact,description,building,displayName,duration,estimatedDuration,fullDisplayName,id,number,queueId,result,timestamp,url]

    $ curl -s \
           --globoff \
           --cookie "${COOKIEJAR}" \
           -H "${CRUMB}" \
           'https://<JENKINS_DOMAIN_NAME>/job/<jobname>/api/json?tree=allBuilds[artifact,description,building,displayName,duration,estimatedDuration,fullDisplayName,id,number,queueId,result,timestamp,url]' |
           jq --raw-output .
    

list plugins

using api (curl)

$ curl -s \
       -u<username>:<password> \
       --cookie "${COOKIEJAR}" \
       -H "${CRUMB}" \
       https://<JENKINS_DOMAIN_NAME>/pluginManager/api/json?depth=1  |
       jq -r '.plugins[] | "\(.shortName):\(.version)"' |
       sort
  • or
    $ curl -s \
           --cookie "${COOKIEJAR}" \
           -H "${CRUMB}" \
           'https://<JENKINS_DOMAIN_NAME>/pluginManager/api/json?pretty=1&tree=plugins\[shortName,longName,version\]'
    {
      "_class": "hudson.LocalPluginManager",
      "plugins": [
        {
          "longName": "SSH Credentials Plugin",
          "shortName": "ssh-credentials",
          "version": "1.18.1"
        },
        {
          "longName": "Configuration as Code Plugin",
          "shortName": "configuration-as-code",
          "version": "1.47"
        },
        ...
    }
    

using cli

$ cat plugin.groovy
def plugins = jenkins.model.Jenkins.instance.getPluginManager().getPlugins()
plugins.each {println "${it.getShortName()}: ${it.getVersion()}"}
  • by jar
    $ curl -fsSL -O https://JENKINS_URL/jnlpJars/jenkins-cli.jar
    $ java -jar jenkins-cli.jar \
      [-auth <username>:<password>] \
      -s https://JENKINS_URL groovy = < plugin.groovy
    
    • or
      $ java -jar jenkins-cli.jar \
        [-auth <username>:<password>] \
        -s https://JENKINS_URL \
        list-plugins
      
  • by ssh

    $ ssh [-i <private-key>] [-l <user>] -p <port> JENKINS_URL groovy =< plugin.groovy
    
    • or

      $ ssh [-i <private-key>] [-l <user>] -p <port> JENKINS_URL list-plugins
      
    • or

      $ ssh [-i <private-key>] [-l <user>] -p <port> JENKINS_URL groovy < = <script.groovy>
      

builds

get particular build parameters

$ curl -s https://<JENKINS_DOMAIN_NAME>/job/<jobname>/<buildnum>/api/xml?xpath=/workflowRun/action/parameter[name="<param_name>"]/value
  • remove xml tag

    $ curl -s 'https://<JENKINS_DOMAIN_NAME>/job/<jobname>/<buildnum>/api/xml?xpath=/workflowRun/action/parameter\[name="tester"\]/value' |
           sed -re 's:<[^>]+>([^<]+)<.*$:\1:'
    
  • i.e.:

    $ curl -s --globoff 'https://<JENKINS_DOMAIN_NAME>/job/<jobname>/<buildnum>/api/xml?xpath=/*/action/parameter[name=%22id%22]'
    <parameter _class="hudson.model.StringParameterValue"><name>id</name><value>marslo</value></parameter>
    
    $ curl -s --globoff 'https://<JENKINS_DOMAIN_NAME>/job/<jobname>/<buildnum>/api/xml?xpath=/*/action/parameter[name=%22id%22]/value'
    <value>marslo</value>
    
    $ curl -s --globoff 'https://<JENKINS_DOMAIN_NAME>/job/<jobname>/<buildnum>/api/xml?xpath=/*/action/parameter[name=%22id%22]/value' |
           sed -re 's:<[^>]+>([^<]+)<.*$:\1:'
    marslo
    

get all parameters via Json format

[!TIP|label:api:] https://<JENKINS_DOMAIN_NAME>/job/<jobname>/<buildnum>/api/json?tree=actions[parameters[*]]

$ curl -s --globoff 'https://<JENKINS_DOMAIN_NAME>/job/<jobname>/<buildnum>/api/json?tree=actions[parameters[*]]' | jq --raw-output '.actions[].parameters[]?'
{
  "_class": "hudson.model.StringParameterValue",
  "name": "id",
  "value": "marslo"
}
{
  "_class": "hudson.model.StringParameterValue",
  "name": "gender",
  "value": "female"
}
  • additional format
    $ curl -s --globoff 'https://<JENKINS_DOMAIN_NAME>/job/<jobname>/<buildnum>/api/json?tree=actions[parameters[*]]' | jq --raw-output '.actions[].parameters[]? | .name + "\t" + .value'
    id  marslo
    gender  female
    

[!TIP|label:jq tips:]

  • remove empty line from output i.e.:
    • original jq --raw-output .actions[].parameters
    • remove empty line: jq --raw-output '[.actions[].parameters | select(length > 0) ]'
$ curl -s --globoff 'https://<JENKINS_DOMAIN_NAME>/job/<jobname>/<buildnum>/api/json?tree=actions[parameters[*]]' | jq --raw-output '[.actions[].parameters | select(length > 0)]'
[
  [
    {
      "_class": "hudson.model.StringParameterValue",
      "name": "id",
      "value": "marslo"
    },
    {
      "_class": "hudson.model.StringParameterValue",
      "name": "gender",
      "value": "female"
    }
  ]
]
Copyright © marslo 2020-2023 all right reserved,powered by GitbookLast Modified: 2024-03-12 15:01:28

results matching ""

    No results matching ""