1. 程式人生 > >jenkins 命令列視窗CLI

jenkins 命令列視窗CLI

    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公有祕鑰到就近的文字框中。Adding public SSH keys for a user

通用命令

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連線模式

//未完待續