鎸佺画闆嗘垚楂樼骇绡囦箣Jenkins cli涓嶫enkins ssh

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了鎸佺画闆嗘垚楂樼骇绡囦箣Jenkins cli涓嶫enkins ssh相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/%e6%b5%8b%e8%af%95%e7%8e%af%e5%a2%83' title='娴嬭瘯鐜'>娴嬭瘯鐜   key   lock   obs   涓嬫媺   hit   鑺傜偣   绔彛閰嶇疆   

绯诲垪鐩綍

Jenkins Cli浠嬬粛

Jenkins Cli涓篔enkins鎻愪緵鐨勪竴涓猚li宸ュ叿,姝ゅ伐鍏峰姛鑳介潪甯稿己澶?鍙互瀹屾垚璇稿閲嶅惎jenkins,鍒涘缓/鍒犻櫎job,鏌ョ湅job鎺у埗鍙拌緭鍑?娣诲姞/鍒犻櫎鑺傜偣绛夊姛鑳?浣嗘槸瀹為檯宸ヤ綔涓?鍍忓垱寤轰换鍔¤繖鏍风殑閰嶇疆鏄剧劧cli闈炲父鍚冨姏,涓嶅鐩存帴鍦╳eb绠$悊鐣岄潰鎿嶄綔,浣嗘槸瀵逛簬閲嶅惎Jenkins,鏌ョ湅璇婃柇淇℃伅绛?鎵ц涓€涓墜鍔ㄦ瀯寤轰换鍔$瓑,鍒欑洿鎺ヤ娇鐢╟li姣旇繘鍏eb绠$悊鐣岄潰鎿嶄綔鏇村姞鏂逛究.鍥犳浠€涔堟椂鍊檞eb绠$悊鐣岄潰,浠€涔堟椂鍊欎娇鐢╟li,瑕佺湅鏄惁鏈夊埄浜庢彁鍗囩敓浜у姏,鏄惁鏈夊埄浜庢彁鍗囦釜浜鸿兘鍔?鏄惁鏈夊埄浜庢彁鍗囧洟闃熺殑鑷姩鍖栦綔涓氭按骞宠繖浜涙寚鏍?涓嶈浠ヤ负浣跨敤cli灏变唬琛ㄦ按骞抽珮鑰岀洸鐩娇鐢╟li浠庤€屽鑷存晥鐜囦笅闄嶆垨鑰呴棶棰樺澶?

jenkins cli鍙互閫氳繃jenkins鎻愪緵鐨刯ar鍖呮潵鍒涘缓涓€涓猚li鐜鎴栬€呬娇鐢╯sh瀹㈡埛绔潵鎵цcli,鏈妭涔熶細鍒嗗埆浠嬬粛瀹冧滑.

浣跨敤jenkins鑷韩瀹㈡埛绔潵鎵цcli

鎴戜滑鎵撳紑jenkins鐨勫畨瑁呯洰褰?杩涘叆鍒?code>war\WEB-INF鐩綍,姝ょ洰褰曚笅闈㈡湁涓€涓?code>jenkins-cli.jar鏂囦欢,瀹冧究鏄敤鏉ュ垱寤簀enkins cli鐜鐨?鎴戜滑鍦ㄨ繖涓洰褰曚笅鎵撳紑鍛戒护琛屽伐鍏?鐒跺悗鎵цjava -jar jenkins-cli.jar help渚垮彲浠ョ湅鍒板畠鐨勮緭鍑哄府鍔╀俊鎭?/p>

鎶€鏈浘鐗? src=

鍙互鐪嬪埌-s閫夐」鍙互鎸囧畾涓€涓猽rl,杩欎釜url灏辨槸jenkins web绠$悊鐣岄潰鐨剈rl,鐜板湪鏄祴璇曠幆澧?url涓?code>http://localhost:8080,褰撶劧鎯宠鎵цcli,杩橀渶瑕佽緭鍏ヤ綘鐨勭敤鎴峰悕鍜屽瘑鐮?杩欓噷閫氳繃 -auth閫夐」鎸囧畾鐢ㄦ埛鍚嶅瘑鐮?

鎴戜滑鎶婁笂浠ヤ俊鎭患鍚堣捣鏉?缁勬垚浠ヤ笅鍛戒护

java -jar jenkins-cli.jar -s http://localhost:8080 -auth tylerzhou:瀵嗙爜 help

浠ヤ笂璇彞涓?help涓鸿鎵ц鐨勫懡浠?

闇€瑕佹敞鎰忕殑鏄?杩欓噷鎵ц鐨勫懡浠ょ洿鎺ユ槸鍛戒护鍚嶇О,涓嶈兘鍔犲儚鍏跺畠鍛戒护琛屽伐鍏?code>- -- 鎴?鏍囪瘑

濡傛灉浣犳壘涓嶅埌Jenkins-cli.jar,鍙互閫氳繃鍦ㄦ祻瑙堝櫒杈撳叆http://localhost:8080/jnlpJars/jenkins-cli.jar杩涜涓嬭浇淇濆瓨,娉ㄦ剰鎶婁互涓婂湴鍧€鏇挎崲涓烘偍鐨勫疄闄呭湴鍧€

鐐瑰嚮鍥炶溅,鍙互鐪嬪埌鎵€鏈塉enkins cli鍙墽琛岀殑鍛戒护鍜屽懡浠ょ殑绠€鐭弿杩?涓嬮潰浠嬬粛鍑犱釜鍙兘姣旇緝甯哥敤鐨勫懡浠?/p>

1) list-jobs,鍙互鍒楀嚭Jenkins閲屾墍鏈夌殑job,灏卞儚鎵撳紑jenkins web绠$悊鐣岄潰棣栭〉鐪嬪埌鐨勯偅鏍?

鍛戒护濡備笅

java -jar jenkins-cli.jar -s http://localhost:8080 -auth 鎮ㄧ殑璐︽埛:鎮ㄧ殑瀵嗙爜 list-jobs

闇€瑕佹敞鎰忕殑鏄?Jenkins cli闇€瑕佹瘡娆℃墽琛屽懡浠ょ殑鏃跺€欓兘甯︿笂java -jar jenkins-cli.jar -s http://localhost:8080 -auth 鎮ㄧ殑璐︽埛:鎮ㄧ殑瀵嗙爜+瑕佹墽琛岀殑鍛戒护,鑰屼笉鏄繘鍏ヤ竴涓幆澧冨悗鍙緭鍏ュ懡浠ゅ氨琛屼簡.

2) build+瑕佹瀯寤虹殑job鍚?鍗冲彲閫氳繃cli鏉ヨЕ鍙戜竴娆℃瀯寤?

鏋勫缓鎴愬姛鍚?鎴戜滑鎵撳紑web绠$悊鐣岄潰渚垮彲浠ョ湅鍒板涓€娆℃瀯寤?

cli鏋勫缓鍛戒护鏄竴涓潪甯稿疄鐢ㄧ殑鍛戒护,铏界劧鎴戜滑鐨勫ぇ閮ㄥ垎浠诲姟閮芥槸鍩轰簬git鐨勪竴涓嚜鍔ㄥ寲娴佺▼.浣嗘槸鏈変簺鏃跺€欎篃闇€瑕佹墜鍔ㄦ墽琛屼竴浜涜剼鏈潵瀹屾垚宸ヤ綔涓殑涓€浜涜嚜鍔ㄥ寲鎿嶄綔.濡傛灉姣忔鎵撳紑web绠$悊鐣岄潰鎵嬪姩鎵ц鏄剧劧涓嶅閫氳繃cli鏉ユ墽琛屾晥鐜囬珮.

3) restart/safe-restart 鍙互閲嶅惎jenkins鏈嶅姟,閫氳繃瀹冧滑鐨勫悕绉板彲浠ョ湅鍒皊afe-restart涓哄畨鍏ㄩ噸鍚?瀹冧細绛夊埌鎵€鏈夌殑鎿嶄綔閮藉畬鎴愮劧鍚庢墽琛岄噸鍚?/p>

4) clear-queue 娓呴櫎鏋勫缓闃熷垪.娌℃湁瀹為檯jenkins浣跨敤缁忛獙鐨勬湅鍙嬪彲鑳戒笉鐭ラ亾,鐢变簬Jenkins鑷韩鍘熷洜鎴栬€呮垜浠剼鏈祴璇曚笉澶熷厖鍒嗗鑷寸殑bug,鏈夋椂鍊欎細閫犳垚Jenkins鏋勫缓闃诲,涓€鐩村浜庢瀯寤虹姸鎬佹棤娉曞畬鎴?杩欐椂鍊欓€氳繃web绠$悊鐣岄潰鐐瑰嚮鍙栨秷鏋勫缓涔熸棤娉曞彇娑堟帀.姝ゆ椂鍛嗕互灏濊瘯杩欎釜鍛戒护.褰撶劧涔熷彲鑳戒粛鐒舵棤娉曠粓姝?姝ゆ椂闇€瑕佷娇鐢╮estart鍛戒护鏆村姏閲嶅惎鏈嶅姟.

浣跨敤api token鐧婚檰

浠ヤ笂鎴戜滑閮芥槸閫氳繃鐢ㄦ埛鍚?瀵嗙爜鐨勬柟寮忔潵鐧婚檰cli,杩欐牱鎶婃槑鏂囧瘑鐮佹毚闇插嚭鏉ユ槸涓嶅彲鍙栫殑,鍏跺疄Jenkins cli杩樺彲浠ラ€氳繃鐢ㄦ埛鍚?apitoken鐨勬柟寮忔潵瀹炵幇鐧婚檰cli.涓嬮潰鎴戜滑浠嬬粛浜斿浣曡缃產pi token.

鎴戜滑鎵撳紑jenkins web绠$悊鐣岄潰,鐐瑰嚮宸︿晶鐨?code>People鎸夐挳,鍦ㄥ嚭鐜扮殑鐣岄潰涓細鍒楀嚭鍙兘涓嶆涓€涓敤鎴?姝ゆ椂鐐瑰嚮鑷韩鐧婚檰jenkins鐨勭敤鎴峰悕,鍦ㄨ繘鍏ョ殑鐣岄潰涓偣鍑?code>configure鎸夐挳,鍦ㄥ嚭鐜扮殑鐣岄潰涓壘鍒?code>Api token鏍?/p>

鎶€鏈浘鐗? src=

鐐瑰嚮Add new token渚夸細鍑虹幇涓€涓敓鎴恡oken鐨勫皬鐣岄潰,杈撳叆token鐨勫悕绉?鍙互鏄换鎰忓悕绉?鐒跺悗鐐瑰嚮generate,杩欐椂鍊欏氨浼氱敓鎴愪竴涓猼oken

鎶€鏈浘鐗? src=

娉ㄦ剰涓€瀹氳鎶婅繖涓猼oken澶嶅埗涓嬫潵鐒跺悗淇濆瓨鍒板叾瀹冨湴鏂?涓嬫鍐嶆壘寮€鐨勬椂鍊欏氨鐪嬩笉鍒板畠浜?

瀹屾垚鍚庣偣鍑讳繚瀛?

姝ゆ椂鎴戜滑鍦╟li涓緭鍏ヤ互涓嬪懡浠?/p>

java -jar jenkins-cli.jar -s http://localhost:8080 -auth tylerzhou:11f52cef1324556a41d966083ffcf0ac1b

鍏朵腑tylerhzou涓虹敤鎴峰悕,鍚庨潰灏辨槸鎴戜滑鍒氭墠鍒涘缓鐨則oken

濡傛灉鎵ц鎴愬姛,浠ヤ笂鍛戒护灏辫緭鍑簀enkins cli鐨勬墍鏈夊懡浠?

鎶婂懡浠や俊鎭繚瀛樺埌鍗曠嫭鏂囦欢

鎴戜滑鍙互鐪嬪埌,浣跨敤token鐨勬柟寮忕櫥闄嗕細瀵艰嚧鍛戒护琛岄潪甯搁暱,涓ラ噸褰卞搷鍛戒护鍙鎬?鍏跺疄鎴戜滑鍙互鎶奱uth淇℃伅淇濆瓨鍒颁竴涓枃浠舵枃浠堕噷.鎴戝湪Jenkins瀹夎鐩綍/war/WEB-INF鐩綍涓嬪垱寤轰簡涓€涓悕涓?code>password.txt鐨勬枃鏈枃浠?鍐呭濡備笅

tylerzhou:11f52cef1324556a41d966083ffcf0ac1b

涔熷嵆涓婇潰auth閲岃緭鍏ョ殑鐢ㄦ埛鍚?apitoken

杩欐椂鎴戜滑鍦?auth閫夐」閲?閫氳繃鎸囧畾@鏂囦欢鍚嶆柟寮忔寚瀹氬寘鍚敤鎴峰悕(瀵嗙爜鎴杢oken)鐨勬枃浠舵潵瀹炵幇鐧婚檰.

浠g爜濡備笅

java -jar jenkins-cli.jar -s http://localhost:8080 -auth @password.txt

浠ヤ笂@鍚庨潰鐨刾assword.txt鍗充负鎴戜滑鍒涘缓鐨勫瘑鐮佹枃浠?/p>

SSH鏂瑰紡鐧婚檰Jenkins鎵цcli

浣跨敤SSH鏂瑰紡鐧婚檰Jenkins闇€瑕佽繘琛岄厤缃?榛樿鎯呭喌涓媕enkins SSHD port浣跨敤鐨勬槸涓€涓殢鏈虹鍙e彿,杩欐牱鏄剧劧涓嶅埄浜巗sh鐧婚檰,浣跨敤ssh鐧婚檰鎴戜滑闇€瑕佹樉寮忕煡閬撶鍙e彿鏄灏?鎴戜滑鎵撳紑Manage jenkins>Configure Global Security鎵惧埌SSH Server鏍?鎶婇粯璁andom閫夐」鍒囦负fixed鐒跺悗杈撳叆涓€涓悓鐢ㄧ鍙e彿,鎴戜娇鐢ㄧ殑鏄?code>16022閰嶇疆瀹屾垚鍚庣偣鍑讳繚瀛?绔彛閰嶇疆濂戒簡,鎴戜滑杩橀渶瑕佹坊鍔犲叕閽ュ埌褰撳墠鐢ㄦ埛閰嶇疆椤?璇峰弬鐓т笂闈?code>浣跨敤api token鐧婚檰涓€鑺?杩涘叆鍒板綋鍓嶇敤鎴疯缃晫闈?杩欐鎴戜滑涓嶉厤缃產pitoken,鑰屾槸寰€涓嬫媺鎵惧埌SSH Public Keys閫夐」,鎶婃垜浠敓鎴愮殑ssh鍏挜澶嶅埗鍒拌繖閲?鍏充簬鐢熸垚ssh key璇峰弬闃呭叾瀹冭祫鏂欐垨鑰呭墠闈㈢珷鑺?杩欓噷涓嶅啀璧樿堪.

瀹屾垚浠ュ悗,鎴戜滑灏卞彲浠ラ€氳繃ssh鏂瑰紡鎵ц鍛戒护浜?

杈撳叆浠ヤ笅鍛戒护

ssh -l tylerzhou -p 16022 localhost help

鍏朵腑-l鎸囧畾鐨勭敤鎴蜂负鎴戜滑鐧婚檰jenkins鏃剁殑璐︽埛.
濡傛灉閰嶇疆鎴愬姛,浠ヤ笂鍛戒护灏变細鍒楀嚭鎵€鏈夌殑Jenkins 鍛戒护鍜岀畝鐭粙缁?

ssh鎵ц鐨勫懡浠ゅ拰涓婇潰閫氳繃jenkins-cli.jar鎵ц鐨勫懡浠ゆ槸涓€鏍?鍙互浜掔浉鍙傜収.

浣跨敤bat绠€鍗曞皝瑁?/h3>

鍙互鐪嬪埌,浠ヤ笂鎵цcli涓嶈鏄€氳繃宸ュ叿杩樻槸ssh,姣忔閮介渶瑕佸甫涓婁竴浜涘浐瀹氱殑鐧婚檰淇℃伅,闈炲父鐑?鎴戜滑鍙互杩涜涓€涓嬬畝鍗曠殑灏佽,杩欐牱姣忔鍙渶瑕佽緭鍏ュ懡浠?涓嶅啀闇€瑕佹瘡娆¢噸澶嶈緭鍏ュ浐瀹氬唴瀹?/p>

鎴戜滑鎶婂畠灏佽鎴愪釜bat鍛戒护,鍛戒护濡備笅

@echo off
cls
:start
set /p arg="璇疯緭鍏ユ偍鐨勫懡浠? "
java -jar jenkins-cli.jar -s http://localhost:8080 -auth @password.txt %arg%
goto start

浠ヤ笂鍛戒护浜ч鍏堝垱寤轰竴涓猻tart鏍囩,鐒跺悗鎻愮ず鐢ㄦ埛杈撳叆鍛戒护,杈撳叆浠ュ悗浼犲埌鐨刯enkins cli宸ュ叿閲?鐒跺悗鎵цgoto璇彞璺冲埌start鏍囩.浠ヤ笂鍛戒护浼氶噸澶嶆墽琛?鎯宠缁堟闇€瑕佹寜涓媍trl+c鏉ョ粓姝?/p>

浣跨敤powershell鑴氭湰杩涜灏佽

鐢变簬绗旇€呭bat涓嶆槸寰堢啛缁?鍥犳鍐欒捣澶嶆潅鑴氭湰鎰熻姣旇緝璐瑰姴,杩欓噷浣跨敤powershell杩涜涓€涓嬪皝瑁?鏀寔娓呭睆,鏌ョ湅鎵ц鐘舵€佸拰閫€鍑洪€夐」,鑴氭湰鍐呭濡備笅

[System.Console]::ForegroundColor=[System.ConsoleColor]::Green
 $writeout= 
  "  閫€鍑鸿鎸?鎴栬€呰緭鍏xit
  娓呭睆璇锋寜2鎴栬€呰緭鍏ls鎴栬€卌lear
  鏌ョ湅鎵ц鐘舵€佽鎸?
  鏌ョ湅甯姪璇疯緭鍏elp
  鏌ョ湅鎻愮ず淇℃伅璇锋寜4"
  Write-Host $writeout
[System.Console]::ForegroundColor=[System.ConsoleColor]::White

while ($true) 
$myvar=Read-Host "璇疯緭鍏ュ懡浠?"
if(($myvar -eq 1)-or($myvar -eq "exit"))break
elseif (($myval -eq 2) -or ($myvar -eq "cls") -or($myvar -eq "clear")) 
    Clear-Host

elseif ($myvar -eq 3) 
    if($LASTEXITCODE -eq 0)
      Write-Host "鎵ц鎴愬姛"
    else
        [System.Console]::ForegroundColor=[System.ConsoleColor]::Red
        Write-Host "鎵ц澶辫触"
        [System.Console]::ForegroundColor=[System.ConsoleColor]::White
    

elseif($myvar -eq 4)
[System.Console]::ForegroundColor=[System.ConsoleColor]::Green
Write-Host $writeout
[System.Console]::ForegroundColor=[System.ConsoleColor]::White

else 
   java -jar jenkins-cli.jar -s http://localhost:8080 -ssh -user tylerzhou $myvar.split(" ")

澶у鍒涘缓涓€涓猵s1鏂囦欢,鎶婁互涓婂唴瀹瑰鍒惰繘鍘荤劧鍚庢寜鎻愮ず鎿嶄綔,渚垮彲浠ユ墽琛岃剼鏈簡.

windows 娌℃湁ssh瀹㈡埛绔殑闂

鏈変簺绔ラ瀷鍦ㄨ窡鐫€鍋氫笂闈㈢殑ssh鏂瑰紡鐧婚檰鏃?鍙兘鍦ㄥ懡浠よ杈撳叆ssh浼氭彁绀烘壘涓嶅埌鍛戒护,杩欐槸鍥犱负鍙湁鏈€鏂扮増鏈殑windows 10(涓嶆槸鎵€鏈夌殑鐗堟湰閮芥湁).骞歌繍鐨勬槸,Jenkins-cli.jar涔熸彁渚涗簡ssh鐧婚檰鏂瑰紡.鎴戜滑杈撳叆浠ヤ笅鍛戒护

java -jar jenkins-cli.jar -s http://localhost:8080 -ssh -user tylerzhou help

閫氳繃浠ヤ笂鏂瑰紡鎸囧畾ssh鏂瑰紡鐧婚檰,骞舵寚瀹?user,涓嶉渶瑕佹寚瀹氱鍙?渚垮彲浠ヤ娇鐢╯sh鏂瑰紡杩炴帴浜?鎴戜滑鍙互浣跨敤涓婇潰璁插埌鐨勫皝闂柟娉曠畝鍗曠殑灏侀棴涓€涓?杩欐牱灏变笉鐢ㄦ瘡娆¢兘杈撳叆閲嶅鐨?鍥哄畾鐨勫唴瀹逛簡.

以上是关于鎸佺画闆嗘垚楂樼骇绡囦箣Jenkins cli涓嶫enkins ssh的主要内容,如果未能解决你的问题,请参考以下文章

闃舵1 璇█鍩虹+楂樼骇_1-3-Java璇█楂樼骇_02-缁ф壙涓庡鎬乢绗?鑺?鎶借薄绫籣19-鍙戠孩鍖呮渚媉瀹炵幇

楂樼骇鎸囦护

楂橀闈㈣瘯棰?--鎸佺画鏇存柊

python鍩烘湰璇硶(鎸佺画鏇存柊)

SqlServer楂樼骇鐗规€?-瀛樺偍杩囩▼

Codeforces VP/琛ラ灏忚 (鎸佺画濉潙)