book
  • README
  • cheatsheet
    • bash
      • builtin
      • syntactic sugar
      • cmd
      • havefun
    • text-processing
      • awk
      • sed
      • html
      • json
      • regex
      • unicode
    • osx
    • curl
    • tricky
    • widget
    • proxy
    • colors
    • math
    • media
    • ssl
      • keystore
      • verification
      • server
      • client
      • tricky
    • windows
      • powershell
      • choco
      • wsl
      • wt
      • shortcut
      • clsid
      • env
      • shell:folder
  • vim
    • nvim
    • install
    • color
    • plugins
      • usage
      • other plugins
      • deprecated
    • tricky
    • viml
    • windows
    • troubleshooting
  • devops
    • admin tools
    • ssh
    • git
      • config
      • alias
      • submodule
      • eol
      • example
      • gerrit
        • gerrit API
      • github
      • troubleshooting
      • tricky
      • statistics
    • pre-commit
    • release-tools
    • tmux
      • cheatsheet
    • ansible
    • vault
    • artifactory
      • api
      • cli
      • aql
      • nginx cert
    • klocwork
      • kwadmin
      • kwserver
      • api
      • q&a
    • elk
    • mongodb
    • android
    • mobile
  • jenkins
    • config
      • windows
    • appearance
    • troubleshooting
    • jenkinsfile
      • utility
      • parallel
      • build
      • envvar
      • properties
      • trigger
      • node
    • script
      • job
      • build
      • stage
      • agent
      • security & authorization
      • exception
      • monitor
      • tricky
    • api
      • blueocean
    • cli
    • plugins
      • kubernetes
      • docker
      • shared-libs
      • lockable-resource
      • ansicolor
      • badge
      • groovy-postbuild
      • simple-theme
      • customizable-header
      • artifactory
      • jira-steps
      • job-dsl
      • build-timeline
      • crumbIssuer
      • coverage
      • uno-choice
      • tricky
  • virtualization
    • kubernetes
      • init
        • kubespray
        • kubeadm
          • environment
          • crio v1.30.4
          • docker v1.15.3
          • HA
        • addons
        • etcd
      • kubectl
        • pod
        • deploy
        • replicasets
        • namespace
        • secrets
      • node
      • certificates
      • events
      • kubeconfig
      • kubelet
      • troubleshooting
      • cheatsheet
      • auth
      • api
      • tools
        • monitor
        • helm
        • network
        • minikube
    • docker
      • run & exec
      • voume
      • remove
      • show info
      • dockerfile
      • dockerd
      • tricky
      • troubleshooting
      • windows
    • crio
    • podman
  • ai
    • prompt
  • osx
    • apps
      • init
      • brew
    • defaults
    • system
    • network
    • script
    • tricky
  • linux
    • devenv
    • util
      • time & date
      • output formatting
      • params
      • tricky
    • nutshell
    • disk
    • network
    • troubleshooting
    • system
      • apt/yum/snap
      • authorization
      • apps
      • x11
    • ubuntu
      • systemctl
      • x
    • rpi
  • programming
    • groovy
    • python
      • config
      • basic
      • list
      • pip
      • q&a
    • others
    • archive
      • angular
      • maven
      • mysql
        • installation
        • logs
      • ruby
        • rubyInstallationQ&A
  • tools
    • fonts
    • html & css
    • Jira & Confluence
    • node & npm
      • gitbook
      • hexo
      • github.page
      • code themes
    • app
      • microsoft office
      • vscode
      • virtualbox
      • iterm2
      • browser
      • skype
      • teamviewer
      • others
  • quotes
  • english
Powered by GitBook
On this page
  • safeRestart via API
  • execute Groovy script with an API call
  • stop build via api
  • get builds information
  • list plugins
  • builds

Was this helpful?

  1. jenkins

api

PrevioustrickyNextblueocean

Last updated 1 year ago

Was this helpful?

reference:

to get crumb and sessoin :

  • $ SERVER='https://localhost:443'
    $ COOKIEJAR="$(mktemp)"
    $ CRUMB=$(curl -u "admin:admin" \
                   --cookie-jar "${COOKIEJAR}" \
                   'https://${SERVER}/crumbIssuer/api/json' |
                   jq -r '[.crumbRequestField, .crumb] | join(":")'
             )

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
$ 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
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:

  • $ 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

$ curl -s \
       -u<username>:<password> \
       --cookie "${COOKIEJAR}" \
       -H "${CRUMB}" \
       https://<JENKINS_DOMAIN_NAME>/pluginManager/api/json?depth=1  |
       jq -r '.plugins[] | "\(.shortName):\(.version)"' |
       sort
  • $ 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"
        },
        ...
    }
$ 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
  • $ 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"
}
  • $ 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:]

    • 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"
    }
  ]
]

i.e.:

execute Groovy script with an API call
stop build via api
USING JENKINS / HUDSON REMOTE API TO CHECK JOBS STATUS
justlaputa/jenkins-api.md
via job api
using api (curl)
or
using cli
by ssh
additional format
remove empty line from output
Example for Jenkins API
CSRF Protection Explained
Remote access API
How to build a job using the REST API and cURL?
7 useful Jenkins Rest services
imarslo : crumb issuer
imarslo : script console
with sessoin (cookie)
safeRestart via API
execute Groovy script with an API call
stop build via api
get builds information
list plugins
using api (curl)
using cli
builds
get particular build parameters
get all parameters via Json format