您的位置:首页 > 其它

jenkins 命令行窗口CLI

2018-01-12 11:35 169 查看
    jenkins有一个内置的命令行窗口CLI,CLI允许用户和管理员通过脚本或者shell环境访问jenkins。这对于日常任务脚本,执行大批的更新,发现并修理故障等非常方便。
    可以通过SSH协议的方式访问命令行窗口,也可以通过jenkins CLI 客户端,一个jenkins提供的jar文件来访问命令行窗口。
   出于安全原因,不推荐使用jenkins2.53及更旧版本和LTS2.46.1及更旧版本的cli:虽然当前没有已知的缺陷,但已经有几个缺陷被发布和修改过,jenkins使用的远程协议有固有的远程代码执行漏洞,甚至有“预授权”漏洞利用(任何用户都可以物理访问jenkins网络)
jenkins2.54及更新版本和LTS 2.46.2及更新版本带的分布式客户端被认为是安全的,使用默认(http或者ssh)模式,因为这是使用标准的ssh命令。
使用SSH协议的CLI
安装新的jenkins,ssh服务模式是禁止的。管理员可以选择设置特殊端口或者询问jenkins选择Configure Global Security 页面上的一个随机端口。为了确定随机分配的SSH端口,请检查Jenkins URL返回的头文件,例如
%curl -Lv https://JENKINS_URL/login2>&1 | grep 'X-SSH-Endpoint'
< X-SSH-Endpoint : localhost:53801
%
使用随机SSH端口(例如53801),和身份验证配置,任何新版本的SSH客户端可以安全地执行CLI命令行。
授权
不管用户使用jenkins属主的哪种授权,都必须拥有全局Overall/Read 权限以便执行CLI。用户可能需要根据执行的命令添加权限。
授权依赖基于SSH 公有/私有 秘钥授权。为了添加使用用户的SSH 公有秘钥,导航到   https://JENKINS_URL/user/USERNAME/configure 粘贴一个SSH公有秘钥到就近的文本框中。


通用命令

jenkins有一系列CLI内置命令,这些命令在每一个jenkins环境中都可见,例如 build 或者 list-jobs.插件也可能提供CLI命令;为了确定jenkins环境给定的全部的可用命令,执行cli  help 命令。
% ssh -l kohsuke -p 53801 localhost help
下面的命令列表不是详尽的,但是是jenkinsCLI 使用的有益的起点。
build
最通用和的cli 命令是build,它允许用户触发任何有权限的任务或者通道(pipeline)
最基本的使用是简单的触发任何或者通道和退出,但是作为附件选项,用户也可以传递参数,选举 SCM,或者甚至跟随触发构建或者运行通道的控制台输出。
% ssh -l kohsuke -p 53801 localhost help build 
java -jar jenkins-cli.jar build JOB [-c] [-f] [-p] [-r N] [-s] [-v] [-w]
Starts a build, and optionally waits for a completion.  Aside from general
scripting use, this command can be used to invoke another job from within a
build of one job.  With the -s option, this command changes the exit code based
on the outcome of the build (exit code 0 indicates a success) and interrupting
the command will interrupt the job.  With the -f option, this command changes
the exit code based on the outcome of the build (exit code 0 indicates a
success) however, unlike -s, interrupting the command will not interrupt the
job (exit code 125 indicates the command was interrupted).  With the -c option,
a build will only run if there has been an SCM change.
JOB : Name of the job to build
-c  : Check for SCM changes before starting the build, and if there's no
change, exit without doing a build
-f  : Follow the build progress. Like -s only interrupts are not passed
through to the build.
-p  : Specify the build parameters in the key=value format.
-s  : Wait until the completion/abortion of the command. Interrupts are passed
through to the build.
-v  : Prints out the console output of the build. Use with -s
-w  : Wait until the start of the command
% ssh -l kohsuke -p 53801 localhost build build-all-software -f -v
Started build-all-software #1
Started from command line by admin
Building in workspace /tmp/jenkins/workspace/build-all-software
[build-all-software] $ /bin/sh -xe /tmp/hudson1100603797526301795.sh
+ echo hello world
hello world
Finished: SUCCESS
Completed build-all-software #1 : SUCCESS
%

console
同样有用的是console 命令,它检索特定的构建或通道运行的控制台输出。当没有提供构建数字,console命令将会输出最后构建的控制台输出。
%ssh -l kohsuke -p 53801 localhost help console
% ssh -l kohsuke -p 53801 localhost help console

java -jar jenkins-cli.jar console JOB [BUILD] [-f] [-n N]
Produces the console output of a specific build to stdout, as if you are doing 'cat build.log'
JOB   : Name of the job
BUILD : Build number or permalink to point to the build. Defaults to the last
build
-f    : If the build is in progress, stay around and append console output as
it comes, like 'tail -f'
-n N  : Display the last N lines
% ssh -l kohsuke -p 53801 localhost console build-all-software
Started from command line by kohsuke
Building in workspace /tmp/jenkins/workspace/build-all-software
[build-all-software] $ /bin/sh -xe /tmp/hudson1100603797526301795.sh
+ echo hello world
yes
Finished: SUCCESS
%
who-am-i
who-am-i 命令对列举当前用户的授权和可用权限是有用的。当因为缺少特定权限而导致的调试找不到命令是有益的。
% ssh -l kohsuke -p 53801 localhost help who-am-i

java -jar jenkins-cli.jar who-am-i
Reports your credential and permissions.
% ssh -l kohsuke -p 53801 localhost who-am-i
Authenticated as: kohsuke
Authorities:
authenticated
%
使用cli客户端
虽然基于ssh的cli是便捷的也能覆盖大多数需求,但是有一些场景分布式的jenkins cli客户端更加合适一些。例如,默认的cli客户端是http协议的,这意味着使用它不需要防火墙开启额外端口。
下载客户端
cli客户端可以从jenkins属主的 /jnlpJars/jenkins-cli.jar 路径直接下载,有效的下载路径: https://JENKINS_URL/jnlpJars/jenkins-cli.jar
虽然一个cli的jar 文件可以用来针对不同版本的jenkins,但是使用中可能出现兼容性问题,请从jenkins 属主重新下载最新的jar文件。
使用客户端
通用的引入客户端的语法如下:
java -jar jenkins-cli.jar [-s JENKINS_URL] [global  options...] command [command options...] [arguments...]
JENKINS_URL 可以是通过环境变量获取指定。其他通用选项的摘要可以通过运行客户机来显示,而不需要任何参数。

客户端连接模式
2.54以上版本/2.46.2以上版本的客户端可有三种基本模式来使用,通过全局选项 -http: -ssh;和 -remoting 来选择。
HTTP连接模式
//未完待续
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: