通过 jcli 管理 Jenkins

jenkins-logo

Jenkins 是强大的自动化工具。

本文介绍如果通过 jcli(Jenkins Client)来管理 Jenkins 站点

环境

  • 启动 Jenkins Service

安装

Linux 下的用户,可通过一下命令安装 jcli

1
2
3
4
curl -L https://github.com/jenkins-zh/jenkins-cli/releases/latest/download/jcli-linux-amd64.tar.gz|tar xzv

# 将 jcli 后添加进环境变量
echo export PATH=~:$PATH >> ~/.bashrc && source ~/.bashrc

##使用

在使用 jcli 工具之前,需要先生成一份配置文件。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
jcli config gen -i=false > ~/.jenkins-cli.yaml
cat .jenkins-cli.yaml
jenkins_servers:
- name: yourServer
  url: http://localhost:8080/jenkins
  username: admin
  token: admin
  proxy: ""
  proxyAuth: ""
  insecureSkipVerify: true
  description: ""
preHooks: []
postHooks: []
pluginSuites: []
mirrors:
- name: default
  url: http://mirrors.jenkins.io/
- name: tsinghua
  url: https://mirrors.tuna.tsinghua.edu.cn/jenkins/
- name: huawei
  url: https://mirrors.huaweicloud.com/jenkins/
- name: tencent
  url: https://mirrors.cloud.tencent.com/jenkins/

可以登录到 jenkins 页面上去 User -> Configure -> API Token 生成一个 token 填入到上面对应字段。

jcli -h

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
Jenkins CLI written by golang which could help you with your multiple Jenkins,

We'd love to hear your feedback at https://github.com/jenkins-zh/jenkins-cli/issues

Usage:
  jcli [command]

Available Commands:
  casc        Configuration as Code
  center      Manage your update center
  completion  Generate shell completion scripts
  computer    Manage the computers of your Jenkins
  config      Manage the config of jcli
  credential  Manage the credentials of your Jenkins
  crumb       Print crumbIssuer of Jenkins
  cwp         Custom Jenkins WAR packager for Jenkins
  doc         Generate document for all jcl commands
  help        Help about any command
  job         Manage the job of your Jenkins
  open        Open your Jenkins with a browser
  plugin      Manage the plugins of Jenkins
  queue       Manage the queue of your Jenkins
  restart     Restart your Jenkins
  runner      The wrapper of jenkinsfile runner
  shell       Create a sub shell so that changes to a specific Jenkins remain local to the shell.
  shutdown    Puts Jenkins into the quiet mode, wait for existing builds to be completed, and then shut down Jenkins
  user        Print the user of your Jenkins
  version     Print the version of Jenkins CLI

Flags:
      --config-load           If load a default config file (default true)
      --configFile string     An alternative config file
      --debug                 Print the output into debug.html
      --doctor                Run the diagnose for current command
  -h, --help                  help for jcli
      --insecureSkipVerify    If skip insecure skip verify (default true)
  -j, --jenkins string        Select a Jenkins server for this time
      --logger-level string   Logger level which could be: debug, info, warn, error (default "warn")
      --proxy string          The proxy of connection to Jenkins
      --proxy-auth string     The auth of proxy of connection to Jenkins
      --proxy-disable         Disable proxy setting
      --token string          The token of Jenkins
      --url string            The URL of Jenkins
      --username string       The username of Jenkins

Use "jcli [command] --help" for more information about a command.

可以看出 jcli 里面涵盖了许多子命令,为了方便查询,jcli 提供了生成命令文档的功能

jcli doc [PATH]

在导出目录下:

jcli-command-doc

里面的内容为:

jcli-command-doc-content

常用命令

由于命令繁多,这里列出一些比较常用的命令,更多的功能请参阅上面生成的文档。

Center

Config

Credential

Job

Plugin

User

Jenkins

  • jcli restart - Restart your Jenkins
  • jcli shutdown - Puts Jenkins into the quiet mode, wait for existing builds to be completed, and then shut down Jenkins
  • jcli runner - The wrapper of jenkinsfile runner Get more about jenkinsfile runner from https://github.com/jenkinsci/jenkinsfile-runner
  • jcli shell - Create a sub shell so that changes to a specific Jenkins remain local to the shell.

参考

Jenkins CLI Tutorial

Jenkins CLI 文档

updatedupdated2020-08-272020-08-27