鎸佺画闆嗘垚楂樼骇绡囦箣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>
鍙互鐪嬪埌-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>
鐐瑰嚮Add new token
渚夸細鍑虹幇涓€涓敓鎴恡oken鐨勫皬鐣岄潰,杈撳叆token鐨勫悕绉?鍙互鏄换鎰忓悕绉?鐒跺悗鐐瑰嚮generate,杩欐椂鍊欏氨浼氱敓鎴愪竴涓猼oken
娉ㄦ剰涓€瀹氳鎶婅繖涓猼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-鍙戠孩鍖呮渚媉瀹炵幇