Работа с jenkins через консоль (CLI) в Unix/Linux

Очень удобно работать в консоле и работа с jenkins — не исключение.

Когда-то в своей статье, я приводил пример установки jenkins на Unix/Linux ОС, если потребуется установка, то можно использовать следующий ман:
Установка Jenkins в Unix/Linux

А я расскажу как можно работать с Jenkins прямо из консоли, для этого потребуется сам дженкинс и его командная строка ( в виде файла):

  • /usr/bin/jenkins-cli.jar — Путь где лежит дженкинс файл для работы с командной строкой. 
  • http://192.168.130.10:8080 — УРЛа по которой доступен дженкинс.

Находим где находиться дженкинс:

┌(vagrant@vagrant-ansible)─(✓)─(10:19 am Wed Dec 14)
└─(/var/lib/jenkins)─(19 files, 68Kb)─> locate jenkins.war
/usr/lib/jenkins/jenkins.war

┌(vagrant@vagrant-ansible)─(✓)─(10:20 am Wed Dec 14)
└─(/var/lib/jenkins)─(19 files, 68Kb)─>

Вот он:

┌(vagrant@vagrant-ansible)─(✗)─(10:31 am Wed Dec 14)
└─(/var/lib/jenkins)─(19 files, 68Kb)─> sudo find / -name "jenkins-cli.jar"
/var/cache/jenkins/war/WEB-INF/jenkins-cli.jar

Но он находиться не в самом лучшем месте, по этому, я его скопирую для удобства использования:

# cp /var/cache/jenkins/war/WEB-INF/jenkins-cli.jar /usr/bin/jenkins-cli.jar
# chmod +x /usr/bin/jenkins-cli.jar

PS: Можно так же, взять данную тулзу с сайта (локально-установленного дженкинса):

# wget http://192.168.130.10:8080/jnlpJars/jenkins-cli.jar -O /usr/bin/jenkins-cli.jar

Вот так вот.

Работа jenkins плагинами через консоль

Для поиска плагинов используем:

$ sudo java -jar /var/cache/jenkins/war/WEB-INF/jenkins-cli.jar -s http://192.168.130.10:8080/ list-plugins

Так я скопировал его в другое место, то у меня запуск выглядит:

┌(vagrant@vagrant-ansible)─(✗)─(12:34 pm Wed Dec 14)
└─(~)─(2 files, 44Kb)─> sudo java -jar /usr/bin/jenkins-cli.jar -s http://192.168.130.10:8080/ list-plugins
workflow-cps               Pipeline: Groovy                                                 2.23
bouncycastle-api           bouncycastle API Plugin                                          2.16.0
cloudbees-folder           Folders Plugin                                                   5.14
structs                    Structs Plugin                                                   1.5
junit                      JUnit Plugin                                                     1.19
antisamy-markup-formatter  OWASP Markup Formatter Plugin                                    1.5
github-api                 GitHub API Plugin                                                1.80
pam-auth                   PAM Authentication plugin                                        1.3
pipeline-stage-step        Pipeline: Stage Step                                             2.2
windows-slaves             Windows Slaves Plugin                                            1.2
ssh-credentials            SSH Credentials Plugin                                           1.12
display-url-api            Display URL API                                                  0.5
mailer                     Mailer Plugin                                                    1.18
ldap                       LDAP Plugin                                                      1.13
git                        Git plugin                                                       3.0.1
token-macro                Token Macro Plugin                                               2.0
external-monitor-job       External Monitor Job Type Plugin                                 1.6
icon-shim                  Icon Shim Plugin                                                 2.0.3
github                     GitHub plugin                                                    1.24.0 (1.25.0)
matrix-auth                Matrix Authorization Strategy Plugin                             1.4
git-client                 Git client plugin                                                2.1.0
script-security            Script Security Plugin                                           1.24
matrix-project             Matrix Project Plugin                                            1.7.1
build-timeout              build timeout plugin                                             1.18
credentials                Credentials Plugin                                               2.1.10
git-server                 GIT server Plugin                                                1.7
workflow-step-api          Pipeline: Step API                                               2.5 (2.6)
workflow-cps-global-lib    Pipeline: Shared Groovy Libraries                                2.5
plain-credentials          Plain Credentials Plugin                                         1.3
credentials-binding        Credentials Binding Plugin                                       1.10
mapdb-api                  MapDB API Plugin                                                 1.0.9.0
timestamper                Timestamper                                                      1.8.7
workflow-api               Pipeline: API                                                    2.8
workflow-basic-steps       Pipeline: Basic Steps                                            2.3
branch-api                 Branch API Plugin                                                1.11.1
resource-disposer          Resource Disposer Plugin                                         0.3
subversion                 Subversion Plug-in                                               2.7.1
ws-cleanup                 Workspace Cleanup Plugin                                         0.32
ant                        Ant Plugin                                                       1.4
gradle                     Gradle Plugin                                                    1.25
pipeline-milestone-step    Pipeline: Milestone Step                                         1.2
workflow-support           Pipeline: Supporting APIs                                        2.11
workflow-multibranch       Pipeline: Multibranch                                            2.9.2
pipeline-build-step        Pipeline: Build Step                                             2.4
jquery-detached            JavaScript GUI Lib: jQuery bundles (jQuery and jQuery UI) plugin 1.2.1
durable-task               Durable Task Plugin                                              1.12
pipeline-input-step        Pipeline: Input Step                                             2.5
ssh-slaves                 SSH Slaves plugin                                                1.12
ace-editor                 JavaScript GUI Lib: ACE Editor bundle plugin                     1.1
workflow-scm-step          Pipeline: SCM Step                                               2.3
scm-api                    SCM API Plugin                                                   1.3
email-ext                  Email Extension Plugin                                           2.52
workflow-job               Pipeline: Job                                                    2.9
pipeline-graph-analysis    Pipeline Graph Analysis Plugin                                   1.3
pipeline-rest-api          Pipeline: REST API Plugin                                        2.4
handlebars                 JavaScript GUI Lib: Handlebars bundle plugin                     1.1.1
momentjs                   JavaScript GUI Lib: Moment.js bundle plugin                      1.1.1
workflow-aggregator        Pipeline                                                         2.4
pipeline-stage-view        Pipeline: Stage View Plugin                                      2.4
workflow-durable-task-step Pipeline: Nodes and Processes                                    2.5
github-branch-source       GitHub Branch Source Plugin                                      1.10.1
github-organization-folder GitHub Organization Folder Plugin                                1.5

┌(vagrant@vagrant-ansible)─(✓)─(01:17 pm Wed Dec 14)
└─(~)─(2 files, 44Kb)─>

Чтобы установить плагин, используем:

# java -jar /usr/bin/jenkins-cli.jar -s http://192.168.130.10:8080/ install-plugin xunit

Где xunit — имя плагина.

Можно создать файл ( допустим plugins.txt), в нем построчно прописать названия плагинов для установки. После чего, можно установить их следующим образом:

# java -jar /usr/bin/jenkins-cli.jar -s http://192.168.130.10:8080/ install-plugin < plugins.txt

Экспорт плагинов осуществляется со сторонними плагинами или просто скопировать конфиг-файлы с:

# ls -alh /var/lib/jenkins/plugins/

Я этого не делал никогда, кому интересно — может проверить 🙂

Установка плагина по УРЛу:

$ java -jar /usr/bin/jenkins-cli.jar -s http://192.168.130.10:8080 install-plugin http://updates.jenkins-ci.org/download/plugins/AdaptivePlugin/0.1/AdaptivePlugin.hpi

Чтобы переустановить все плагины в Jenkins-е, я нашел следующий подход. Переходим в папку с плагинами, у меня это:

$ cd ~ && curl 'localhost:8080/jnlpJars/jenkins-cli.jar' > ~/jenkins-cli.jar && cd /var/jenkins_home/plugins

И выполняем:

$ for i in $(ls -d */ | awk '{print substr($0, 1, length($0)-1)}'); \
do java -jar ~/jenkins-cli.jar -s http://localhost:8080/ -auth YOUR_USER_HERE:YOUR_PW_HERE install-plugin $i; \
done

Получаем список установленных плагинов. Но стягиваем для начала тулу:

$ cd ~ && curl 'localhost:8080/jnlpJars/jenkins-cli.jar' > jenkins-cli.jar

Пендалим груви скрипт:

$ cat > plugins.groovy << EOF
def plugins = jenkins.model.Jenkins.instance.getPluginManager().getPlugins()
plugins.each {println "${it.getShortName()}: ${it.getVersion()}"}
EOF

Или:

$ echo 'def plugins = jenkins.model.Jenkins.instance.getPluginManager().getPlugins()
plugins.each {println "${it.getShortName()}: ${it.getVersion()}"}' >> plugins.groovy

И запускаем:

$ java -jar ~/jenkins-cli.jar -s http://localhost:8080/ -auth YOUR_USER:YOUR_PW groovy = < plugins.groovy > plugins.txt

Работа с сервисами jenkins

Выключить дженкис через терминал можно:

$ sudo java -jar /usr/bin/jenkins-cli.jar -s http://192.168.130.10:8080/ safe-shutdown --username "captain" --password "captain"

Перезагуризть:

┌(vagrant@vagrant-ansible)─(✓)─(11:56 am Wed Dec 14)
└─(~)─(2 files, 36Kb)─> sudo java -jar /usr/bin/jenkins-cli.jar -s http://192.168.130.10:8080/ safe-restart --username "captain" --password "captain"
java.io.IOException: No X-Jenkins-CLI2-Port among [X-Jenkins, null, Server, X-Content-Type-Options, X-Hudson, Date, X-Jenkins-Session, Cache-Control, X-Hudson-Theme, Set-Cookie, Expires, Content-Length, Content-Type]
	at hudson.cli.CLI.getCliTcpPort(CLI.java:284)
	at hudson.cli.CLI.<init>(CLI.java:128)
	at hudson.cli.CLIConnectionFactory.connect(CLIConnectionFactory.java:72)
	at hudson.cli.CLI._main(CLI.java:473)
	at hudson.cli.CLI.main(CLI.java:384)
	Suppressed: java.io.IOException: Server returned HTTP response code: 503 for URL: http://192.168.130.10:8080/cli
		at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1876)
		at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1474)
		at hudson.cli.FullDuplexHttpStream.<init>(FullDuplexHttpStream.java:78)
		at hudson.cli.CLI.connectViaHttp(CLI.java:152)
		at hudson.cli.CLI.<init>(CLI.java:132)
		... 3 more

┌(vagrant@vagrant-ansible)─(✗)─(11:57 am Wed Dec 14)
└─(~)─(2 files, 36Kb)─>

Есть еще несколько разновидностей команд, но мне хватает этого.

Работа с jenkins job-ами через консоль

Создать джобу:

# java -jar /usr/bin/jenkins-cli.jar -s http://192.168.130.10:8080 build job_name

Чтобы создать задачу (job-у) с XML файла, выполните:

# java -jar /usr/bin/jenkins-cli.jar -s http://192.168.130.10:8080 create-job newmyjob < myjob.xml

Чтобы создать  бэкап (дамп) задачи (job-ы) с XML файла, выполните:

# java -jar /usr/bin/jenkins-cli.jar -s http://192.168.130.10:8080 get-job myjob > myjob.xml

Я создал задачу и выполняю ее каждую неделю:

#!/bin/bash
#IFS for jobs with spaces.
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
for i in $(java -jar /run/jenkins/war/WEB-INF/jenkins-cli.jar -s http://server:8080/ list-jobs); 
do 
java -jar /run/jenkins/war/WEB-INF/jenkins-cli.jar -s http://server:8080/ get-job ${i} > ${i}.xml;
done
IFS=$SAVEIFS
mkdir deploy
tar cvfj "jenkins-jobs.tar.bz2" ./*.xml

Еще пример скрипта для export-а джобов в папку:

#! /bin/bash
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
declare -i j=0
for i in $(java -jar jenkins-cli.jar -s http://server:8080/jenkins list-jobs --username **** --password ***);
do
let "j++";
echo $j;
if [ $j -gt 283 ] // If you have more jobs do it in chunks as it will terminate in the middle of the process. So Resume your job from where it ends.
then
java -jar jenkins-cli.jar -s http://lxvbmcbma:8080/jenkins get-job --username **** --password **** ${i} > ${i}.xml;
echo "done";
fi
done

Тогда import джобов будет:

for f in *.xml;
do
echo "Processing ${f%.*} file.."; //truncate the .xml extention and load the xml file for job creation
java -jar jenkins-cli.jar -s http://server:8080/jenkins create-job ${f%.*} < $f
done

Чтобы сконвертировать Jenkins job XML в JJB YAML нужно использовать плагин — jenkins-job-wrecker. Его можно найти на гитхабе ( как пример).

Хочу заметить, что можно использовать jenkins-job-builder для облегченной работы с джобами. У меня не дошли до него руки ( на момент написания статьи).

Для помощи, можно использовать help команду:

$ java -jar ~/jenkins-cli.jar -s http://localhost:8080/ -auth captain:captain help
  add-job-to-view
    Adds jobs to view.
  build
    Builds a job, and optionally waits until its completion.
  cancel-quiet-down
    Cancel the effect of the "quiet-down" command.
  clear-queue
    Clears the build queue.
  connect-node
    Reconnect to a node(s)
  console
    Retrieves console output of a build.
  copy-job
    Copies a job.
  create-credentials-by-xml
    Create Credential by XML
  create-credentials-domain-by-xml
    Create Credentials Domain by XML
  create-job
    Creates a new job by reading stdin as a configuration XML file.
  create-node
    Creates a new node by reading stdin as a XML configuration.
  create-view
    Creates a new view by reading stdin as a XML configuration.
  declarative-linter
    Validate a Jenkinsfile containing a Declarative Pipeline
  delete-builds
    Deletes build record(s).
  delete-credentials
    Delete a Credential
  delete-credentials-domain
    Delete a Credentials Domain
  delete-job
    Deletes job(s).
  delete-node
    Deletes node(s)
  delete-view
    Deletes view(s).
  disable-job
    Disables a job.
  disable-plugin
    Disable one or more installed plugins.
  disconnect-node
    Disconnects from a node.
  enable-job
    Enables a job.
  enable-plugin
    Enables one or more installed plugins transitively.
  get-credentials-as-xml
    Get a Credentials as XML (secrets redacted)
  get-credentials-domain-as-xml
    Get a Credentials Domain as XML
  get-gradle
    List available gradle installations
  get-job
    Dumps the job definition XML to stdout.
  get-node
    Dumps the node definition XML to stdout.
  get-view
    Dumps the view definition XML to stdout.
  groovy
    Executes the specified Groovy script.
  groovysh
    Runs an interactive groovy shell.
  help
    Lists all the available commands or a detailed description of single command.
  import-credentials-as-xml
    Import credentials as XML. The output of "list-credentials-as-xml" can be used as input here as is, the only needed change is to set the actual Secrets which are redacted in the output.
  install-plugin
    Installs a plugin either from a file, an URL, or from update center.
  install-tool
    Performs automatic tool installation, and print its location to stdout. Can be only called from inside a build. [deprecated]
  keep-build
    Mark the build to keep the build forever.
  list-changes
    Dumps the changelog for the specified build(s).
  list-credentials
    Lists the Credentials in a specific Store
  list-credentials-as-xml
    Export credentials as XML. The output of this command can be used as input for "import-credentials-as-xml" as is, the only needed change is to set the actual Secrets which are redacted in the output.
  list-credentials-context-resolvers
    List Credentials Context Resolvers
  list-credentials-providers
    List Credentials Providers
  list-jobs
    Lists all jobs in a specific view or item group.
  list-plugins
    Outputs a list of installed plugins.
  login
    Saves the current credentials to allow future commands to run without explicit credential information. [deprecated]
  logout
    Deletes the credentials stored with the login command. [deprecated]
  mail
    Reads stdin and sends that out as an e-mail.
  offline-node
    Stop using a node for performing builds temporarily, until the next "online-node" command.
  online-node
    Resume using a node for performing builds, to cancel out the earlier "offline-node" command.
  quiet-down
    Quiet down Jenkins, in preparation for a restart. Don’t start any builds.
  reload-configuration
    Discard all the loaded data in memory and reload everything from file system. Useful when you modified config files directly on disk.
  reload-job
    Reload job(s)
  remove-job-from-view
    Removes jobs from view.
  replay-pipeline
    Replay a Pipeline build with edited script taken from standard input
  restart
    Restart Jenkins.
  restart-from-stage
    Restart a completed Declarative Pipeline build from a given stage.
  safe-restart
    Safely restart Jenkins.
  safe-shutdown
    Puts Jenkins into the quiet mode, wait for existing builds to be completed, and then shut down Jenkins.
  session-id
    Outputs the session ID, which changes every time Jenkins restarts.
  set-build-description
    Sets the description of a build.
  set-build-display-name
    Sets the displayName of a build.
  set-build-parameter
    Update/set the build parameter of the current build in progress. [deprecated]
  set-build-result
    Sets the result of the current build. Works only if invoked from within a build. [deprecated]
  set-external-build-result
    Set external monitor job result.
  shutdown
    Immediately shuts down Jenkins server.
  trigger-downstream-pipelines
    Triggers the downstream pipelines of the given Maven artifact based on their Maven dependencies
  update-credentials-by-xml
    Update Credentials by XML
  update-credentials-domain-by-xml
    Update Credentials Domain by XML
  update-job
    Updates the job definition XML from stdin. The opposite of the get-job command.
  update-node
    Updates the node definition XML from stdin. The opposite of the get-node command.
  update-view
    Updates the view definition XML from stdin. The opposite of the get-view command.
  version
    Outputs the current version.
  wait-node-offline
    Wait for a node to become offline.
  wait-node-online
    Wait for a node to become online.
  who-am-i
    Reports your credential and permissions.
root@jenkins:~#
root@jenkins:~# java -jar ~/jenkins-cli.jar -s http://localhost:8080/ -auth captain:captain help
  add-job-to-view
    Adds jobs to view.
  build
    Builds a job, and optionally waits until its completion.
  cancel-quiet-down
    Cancel the effect of the "quiet-down" command.
  clear-queue
    Clears the build queue.
  connect-node
    Reconnect to a node(s)
  console
    Retrieves console output of a build.
  copy-job
    Copies a job.
  create-credentials-by-xml
    Create Credential by XML
  create-credentials-domain-by-xml
    Create Credentials Domain by XML
  create-job
    Creates a new job by reading stdin as a configuration XML file.
  create-node
    Creates a new node by reading stdin as a XML configuration.
  create-view
    Creates a new view by reading stdin as a XML configuration.
  declarative-linter
    Validate a Jenkinsfile containing a Declarative Pipeline
  delete-builds
    Deletes build record(s).
  delete-credentials
    Delete a Credential
  delete-credentials-domain
    Delete a Credentials Domain
  delete-job
    Deletes job(s).
  delete-node
    Deletes node(s)
  delete-view
    Deletes view(s).
  disable-job
    Disables a job.
  disable-plugin
    Disable one or more installed plugins.
  disconnect-node
    Disconnects from a node.
  enable-job
    Enables a job.
  enable-plugin
    Enables one or more installed plugins transitively.
  get-credentials-as-xml
    Get a Credentials as XML (secrets redacted)
  get-credentials-domain-as-xml
    Get a Credentials Domain as XML
  get-gradle
    List available gradle installations
  get-job
    Dumps the job definition XML to stdout.
  get-node
    Dumps the node definition XML to stdout.
  get-view
    Dumps the view definition XML to stdout.
  groovy
    Executes the specified Groovy script.
  groovysh
    Runs an interactive groovy shell.
  help
    Lists all the available commands or a detailed description of single command.
  import-credentials-as-xml
    Import credentials as XML. The output of "list-credentials-as-xml" can be used as input here as is, the only needed change is to set the actual Secrets which are redacted in the output.
  install-plugin
    Installs a plugin either from a file, an URL, or from update center.
  install-tool
    Performs automatic tool installation, and print its location to stdout. Can be only called from inside a build. [deprecated]
  keep-build
    Mark the build to keep the build forever.
  list-changes
    Dumps the changelog for the specified build(s).
  list-credentials
    Lists the Credentials in a specific Store
  list-credentials-as-xml
    Export credentials as XML. The output of this command can be used as input for "import-credentials-as-xml" as is, the only needed change is to set the actual Secrets which are redacted in the output.
  list-credentials-context-resolvers
    List Credentials Context Resolvers
  list-credentials-providers
    List Credentials Providers
  list-jobs
    Lists all jobs in a specific view or item group.
  list-plugins
    Outputs a list of installed plugins.
  login
    Saves the current credentials to allow future commands to run without explicit credential information. [deprecated]
  logout
    Deletes the credentials stored with the login command. [deprecated]
  mail
    Reads stdin and sends that out as an e-mail.
  offline-node
    Stop using a node for performing builds temporarily, until the next "online-node" command.
  online-node
    Resume using a node for performing builds, to cancel out the earlier "offline-node" command.
  quiet-down
    Quiet down Jenkins, in preparation for a restart. Don’t start any builds.
  reload-configuration
    Discard all the loaded data in memory and reload everything from file system. Useful when you modified config files directly on disk.
  reload-job
    Reload job(s)
  remove-job-from-view
    Removes jobs from view.
  replay-pipeline
    Replay a Pipeline build with edited script taken from standard input
  restart
    Restart Jenkins.
  restart-from-stage
    Restart a completed Declarative Pipeline build from a given stage.
  safe-restart
    Safely restart Jenkins.
  safe-shutdown
    Puts Jenkins into the quiet mode, wait for existing builds to be completed, and then shut down Jenkins.
  session-id
    Outputs the session ID, which changes every time Jenkins restarts.
  set-build-description
    Sets the description of a build.
  set-build-display-name
    Sets the displayName of a build.
  set-build-parameter
    Update/set the build parameter of the current build in progress. [deprecated]
  set-build-result
    Sets the result of the current build. Works only if invoked from within a build. [deprecated]
  set-external-build-result
    Set external monitor job result.
  shutdown
    Immediately shuts down Jenkins server.
  trigger-downstream-pipelines
    Triggers the downstream pipelines of the given Maven artifact based on their Maven dependencies
  update-credentials-by-xml
    Update Credentials by XML
  update-credentials-domain-by-xml
    Update Credentials Domain by XML
  update-job
    Updates the job definition XML from stdin. The opposite of the get-job command.
  update-node
    Updates the node definition XML from stdin. The opposite of the get-node command.
  update-view
    Updates the view definition XML from stdin. The opposite of the get-view command.
  version
    Outputs the current version.
  wait-node-offline
    Wait for a node to become offline.
  wait-node-online
    Wait for a node to become online.
  who-am-i
    Reports your credential and permissions.

Утилита butler в Unix/Linux

Утилита butler служит для работы с job-ами и plugin-ами в Jenkins. Чтобы ее уставновить, выполните.

Установить butler в Linux:

Стоит выполнить команду:

$ wget https://s3.us-east-1.amazonaws.com/butlercli/1.0.0/linux/butler
$ chmod +x butler
$ mv butler /usr/local/bin/

Установить butler в Mac OS X:

Стоит выполнить команду:

$ wget wget https://s3.us-east-1.amazonaws.com/butlercli/1.0.0/osx/butler
$ chmod +x butler
$ mv butler /usr/local/bin/

Использование butler в Unix/Linux

Чтобы заэкспортить ваши плагины, выполните:

$ butler plugins export --server localhost:8080 --username YOUR_USER --password YOUR_PASSWORD

Чтобы заимпортить ваши плагины, выполните:

$ butler plugins import --server localhost:8080 --username YOUR_USER --password YOUR_PASSWORD

Чтобы заэкспортить ваши джобы, выполните:

$ butler jobs export --server localhost:8080 --username YOUR_USER --password YOUR_PASSWORD

Чтобы заимпортить ваши джобы, выполните:

$ butler jobs import --server localhost:8080 --username YOUR_USER --password YOUR_PASSWORD

Для помощи, можно выполнить:

$ butler help

Вот и все, тема «Работа с jenkins через консоль (CLI) в Unix/Linux» завершена.

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *

Этот сайт использует Akismet для борьбы со спамом. Узнайте, как обрабатываются ваши данные комментариев.