前言
企業(yè)線上環(huán)境是.NET Framework,自然使用Windows Server平臺。現(xiàn)在需求是要對.NET項目進行自動化持續(xù)集成,免得每次手動發(fā)布,一臺臺機器登錄,那種痛苦干過的都懂得,繁瑣、效率低下、誤操作等等。而.Net與Jenkins的結(jié)合使用目前要達到完全自動化還是有局限性的,關(guān)鍵是Windows環(huán)境,各種命令無法向Linux方便。.NET Core可能會好一些。現(xiàn)有的沒辦法,得上。
基本組合是Jenkins + Gitlab + Msbuild。實現(xiàn)代碼提交、編譯、部署。
成都網(wǎng)站設(shè)計、做網(wǎng)站,成都做網(wǎng)站公司-創(chuàng)新互聯(lián)建站已向1000多家企業(yè)提供了,網(wǎng)站設(shè)計,網(wǎng)站制作,網(wǎng)絡(luò)營銷等服務(wù)!設(shè)計與技術(shù)結(jié)合,多年網(wǎng)站推廣經(jīng)驗,合理的價格為您打造企業(yè)品質(zhì)網(wǎng)站。
"E:\Program Files (x86)\Tools\nuget.exe" restore "E:\jenkins-workspace\APIDataBM-test\BM.BMData.Web\BM.BMData.sln" -ConfigFile "C:\Users\Administrator\AppData\Roaming\NuGet\NuGet.Config" -NoCache
其中Nuget restore命令是獲取項目需要的程序包,解決包的依賴問題。
7) 構(gòu)建環(huán)境配置
選擇“Build a Visual Studio project or solution using MSBuild”配置如下:
MSBuild Version:之前Global Tool Configuration中配置的版本。
MSBuild Build File:要構(gòu)建的解決方案sln文件,也可以是項目.csproj文件。注意都是相對工作的路徑。
Command Line Arguments:
/t:Rebuild 重新生成
/p:Configuration=Debug 生成Debug模式
/t:resolveReferences
/p:WebProjectOutputDir="E:\Publish-web\APIData-Test\web" 構(gòu)建后sln輸出目標目錄
/p:OutputPath="E:\Publish-web\APIData-Test\web\bin" 輸出目標的bin目錄
8) 項目config配置
項目各個環(huán)境配置集中存儲于Gitlab某個倉庫中,開發(fā)需對配置更改直接Gitlab中更改,方便統(tǒng)一管理,并對某些敏感信息進行過濾,配置文件安全性增加了。
Windows batch command的bat文件內(nèi)容如下:
@echo off
cd /d E:\git-config\config-public
git checkout dev
git pull origin dev
echo 選擇部署的服務(wù)器IP: %1
xcopy E:\git-config\config-public\test\GroupAPIData\AB\%1 E:\jenkins-workspace\APIDataBK\TEST\AB /exclude:E:\Python-scripts\test\exclude.txt /s /e /h /y
@echo off<nul 3>nul
其中變量名表示方式Linux和Windows系統(tǒng)平臺方式有區(qū)別:
Windows:%BUILD_ENV% %變量名%
Linux:${BUILD_ENV} ,也可直接使用 $BUILD_ENV
從Git Lab倉庫中,pull下某個分支的最新配置文件。然后在本地的git config目錄下copy配置文件到項目中,其中exclude配置指定無需copy的文件,如隱藏的.gitkeep文件等。
注:運行Jenkins程序需改成系統(tǒng)administrator賬戶權(quán)限,默認是本地系統(tǒng)賬戶(system),否則執(zhí)行到git pull命令是報類似權(quán)限問題。
9) 參數(shù)化構(gòu)建
發(fā)布構(gòu)建的服務(wù)器是多臺情況下,這里選擇使用項目參數(shù)化方式,Jenkins中找到"This project is parameterized"項,選擇“choice parameter”,填寫好名稱(這個會作為參數(shù)后面使用到)、選項(這里根據(jù)實際環(huán)境用IP)、描述。如下圖:
uid = 0
gid = 0
use chroot = false
strict modes = false
hosts allow = *
log file = rsyncd.log
port = 873
[APIData]
path = /cygdrive/e/Web/APIData-test/web
secrets file = /cygdrive/c/Program Files (x86)/ICW/rsyncd.secrets
list = true
auth users = rsync_user
read only = false
cwRsync Client端的rsync命令在Jenkins中的配置如下:
"C:\Program Files (x86)\cwRsync\bin\rsync.exe" -avzP --progress --delete --port=873 --password-file="/cygdrive/c/Program Files (x86)/cwRsync/passwd.txt" /cygdrive/e/Publish-web/APIData-Test2/web/ rsync_user@10.10.10.53::APIData
配置完成后,Jenkins上點擊構(gòu)建。
3) Python腳本實現(xiàn)方式
winRM服務(wù)是windows server下PowerShell的遠程管理服務(wù)。Python腳本可通過連接winRM模塊操作windows命令行。
3.1配置winRM服務(wù)
在被控制windows server上winRM服務(wù)操作:
查看winRM服務(wù)狀態(tài),默認都是未啟動狀態(tài),下面命令執(zhí)行后無任何結(jié)果輸出,需執(zhí)行后續(xù)步驟進行開啟。
> winrm enumerate winrm/config/listener
配置winRM服務(wù)啟動
> winrm quickconfig
查看windows的winrm service listener
> winrm e winrm/config/listener
winrm service配置auth
> winrm set winrm/config/service/auth @{Basic="true"}
配置winrm service 加密方式為允許非加密
> winrm set winrm/config/service @{AllowUnencrypted="true"}
3.2配置python腳本
Job配置項構(gòu)建中,增加構(gòu)建步驟->Execute Windows batch command
Python腳本如下:
import winrm
import os
import sys
import time
env = os.getenv("ENV")
print('選擇發(fā)布構(gòu)建的服務(wù)器ENV是:', env)
if env == '10.10.10.10':
win2008r2 = winrm.Session('http://10.10.10.10:5985/wsman',auth=('deploy_user','xxxxxx'))
ip = win2008r2.run_cmd('ipconfig | findstr "10.10.10.10"')
app_list = win2008r2.run_cmd('C:\\Windows\\System32\\inetsrv\\appcmd.exe list app')
app_stop = win2008r2.run_cmd('C:\\Windows\\System32\\inetsrv\\appcmd.exe stop site DataapiBM')
backup = win2008r2.run_cmd('xcopy D:\\Web-prd\\DataapiBM D:\\Web-prd\\backup\\DataapiBM-%date:~8,2%月%date:~11,2%日%time:~0,2%時%time:~3,2%分%time:~6,2%秒\ /exclude:D:\\Web-prd\\backup\\exclude.txt /ryhs')
rsync_code = win2008r2.run_cmd('C:\\cwRsync\\bin\\rsync.exe -vzrtopg --numeric-ids --progress --port=1873 --password-file=/cygdrive/c/cwRsync/passwd.txt<c:\cwRsync\passwd.txt rsync_user@10.10.10.10::APIDataBM /cygdrive/d/Web-prd/DataapiBM')
app_start = win2008r2.run_cmd('C:\\Windows\\System32\\inetsrv\\appcmd.exe start site DataapiBM')
app = win2008r2.run_cmd('C:\\Windows\\System32\\inetsrv\\appcmd.exe list app | findstr DataapiBM')
print("當前發(fā)布的服務(wù)器IP:")
print(ip.std_out)
print("列出當前應(yīng)用pool站點app")
print(app_list.std_out.decode())
print(app_list.std_err)
print("開始停DataapiBM站點app")
print("Stoping...")
print(app_stop.std_out.decode())
print(app_stop.std_err)
time.sleep(3)
print("開始備份DataapiBM站點程序...")
print(backup.std_out)
print(backup.std_err)
time.sleep(3)
print("開始從代碼倉庫同步DataapiBM最新程序...")
print(rsync_code.std_out.decode())
print(rsync_code.std_err)
time.sleep(5)
print("開始啟動DataapiBM站點服務(wù)")
print("Starting...")
print(app_start.std_out.decode())
print(app_start.std_err)
time.sleep(5)
print(app.std_out.decode())
print(app.std_err)
print("站點服務(wù)DataapiBM已啟動成功,發(fā)布完成,請驗證!")
elif env == '10.10.10.11':
win2008r2 = winrm.Session('http://10.10.10.11:5985/wsman',auth=('deploy_user','HicoreP@ss'))
ip = win2008r2.run_cmd('ipconfig | findstr "10.10.10.11"')
app_list = win2008r2.run_cmd('C:\\Windows\\System32\\inetsrv\\appcmd.exe list app')
app_stop = win2008r2.run_cmd('C:\\Windows\\System32\\inetsrv\\appcmd.exe stop site DataapiBM')
backup = win2008r2.run_cmd('xcopy D:\\Web-prd\\DataapiBM D:\\Web-prd\\backup\\DataapiBM-%date:~8,2%月%date:~11,2%日%time:~0,2%時%time:~3,2%分%time:~6,2%秒\ /exclude:D:\\Web-prd\\backup\\exclude.txt /ryhs')
rsync_code = win2008r2.run_cmd('C:\\cwRsync\\bin\\rsync.exe -vzrtopg --numeric-ids --progress --port=1873 --password-file=/cygdrive/c/cwRsync/passwd.txt<c:\cwRsync\passwd.txt rsync_user@10.10.10.11::APIDataBM /cygdrive/d/Web-prd/DataapiBM')
app_start = win2008r2.run_cmd('C:\\Windows\\System32\\inetsrv\\appcmd.exe start site DataapiBM')
app = win2008r2.run_cmd('C:\\Windows\\System32\\inetsrv\\appcmd.exe list app | findstr DataapiBM')
print("當前發(fā)布的服務(wù)器IP:")
print(ip.std_out)
print("列出當前應(yīng)用pool站點app")
print(app_list.std_out.decode())
print(app_list.std_err)
print("開始停DataapiBM站點app")
print("Stoping...")
print(app_stop.std_out.decode())
print(app_stop.std_err)
time.sleep(3)
print("開始備份DataapiBM站點程序...")
print(backup.std_out)
print(backup.std_err)
time.sleep(3)
print("開始從代碼倉庫同步DataapiBM最新程序...")
print(rsync_code.std_out.decode())
print(rsync_code.std_err)
time.sleep(5)
print("開始啟動DataapiBM站點服務(wù)")
print("Starting...")
print(app_start.std_out.decode())
print(app_start.std_err)
time.sleep(5)
print(app.std_out.decode())
print(app.std_err)
print("站點服務(wù)DataapiBM已啟動成功,發(fā)布完成,請驗證!")
else:
print('請正確選擇部署環(huán)境!')
配置完成后,點擊“Build with Parameters”,選擇下拉框中對應(yīng)的服務(wù)器節(jié)點進行構(gòu)建。
查看構(gòu)建情況,通過控制臺查看:
控制臺輸出構(gòu)建詳細的log信息,如果構(gòu)建成功,會有個藍色的圈圈,并且控制臺最后輸出“Finished: SUCCESS”信息,表示此Job構(gòu)建成功。相應(yīng)的如果構(gòu)建失敗,控制臺也會輸出報錯信息。
自此,項目自動化構(gòu)建完成。當然其中有些環(huán)節(jié)還需繼續(xù)優(yōu)化。測試比較下來,應(yīng)用發(fā)布這步操作,winRM+Python方式比較靈活方便。
網(wǎng)頁題目:Jenkins+GitLab+Msbuild+Python自
文章分享:http://chinadenli.net/article46/gpgseg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站營銷、、用戶體驗、商城網(wǎng)站、Google、ChatGPT
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)