編譯打包的流程圖如下:
Paste_Image.png
在jenkins服務器上創建目錄
- D:\build_code\demo目錄,用于存放編譯后的代碼
- D:\Packages\upload目錄,用于存放代碼壓縮文件
- D:\Packages\old目錄,用于存放歷史代碼壓縮文件
- D:\scripts目錄,用于存放腳本文件
- D:\tools目錄,用于存放工具
在jenkins上創建Build_demo任務
Paste_Image.png
配置源碼管理
Paste_Image.png
配置構建
Paste_Image.png
cmd命令:
"C:\Program Files (x86)\MSBuild\14.0\Bin\MSBuild.exe" webdemo.sln /t:clean /t:rebuild /p:Configuration=release /p:WebProjectOutputDir=d:\build_code\demo /p:OutputPath=d:\build_code\demo\bin
配置打包程序的腳本
Paste_Image.png
$datetime=Get-Date -Format 'yyyyMMddHHmmss' #定義日期
Move-Item D:\Packages\upload\* D:\Packages\old\ #備份上次發布的文件
D:\tools\7z.exe a D:\Packages\upload\demo-$datetime.7z D:\build_code\demo\* 壓縮編譯代碼
配置發送壓縮文件到后端服務器的腳本
Paste_Image.png
[string]$xmldocpath = "D:\scripts\config.xml" #讀取xml配置文件
$xmlDoc = New-Object "system.xml.xmldocument"
$xmlDoc.Load($xmldocpath)
$nodeList=$xmlDoc.GetElementsByTagName("Server");
foreach($node in $nodeList){
$childNodes = $node.ChildNodes
#Predefine necessary information
$ComputerName = $childNodes.Item(0).InnerXml.ToString() # 遠端地址
$Username = $childNodes.Item(1).InnerXml.ToString() # 用戶名
$Password = $childNodes.Item(2).InnerXml.ToString() # 密碼
#創建用戶認證
$SecurePassWord = ConvertTo-SecureString -AsPlainText $Password -Force
$Cred = New-Object -TypeName "System.Management.Automation.PSCredential" -ArgumentList $Username, $SecurePassWord
#創建session
$Session = New-PSSession -ComputerName $ComputerName -credential $Cred
#拷貝文件到后端服務器
Copy-Item "D:\Packages\upload\demo-*.7z" -Destination "\\$ComputerName\d$\Packages\online"
#關閉session
Remove-PSSession -Session $Session
Write-Host $ComputerName '主機 【執行完畢】'
}
D:\scriptes\config.xml 內容
<?xml version="1.0" encoding="utf-8"?>
<Servers>
<Server>
<RemoteHost>192.168.77.140</RemoteHost>
<RemoteLoginName>administrator</RemoteLoginName>
<RemotePwd>123456</RemotePwd>
</Server>
<Server>
<RemoteHost>192.168.77.142</RemoteHost>
<RemoteLoginName>administrator</RemoteLoginName>
<RemotePwd>123456</RemotePwd>
</Server>
</Servers>
如果想要發送完成后郵件通知,可以配置
Paste_Image.png
msbuild參數解釋
webdemo.sln:
工程文件路徑
/t:clean:
編譯前清理上次編譯的文件
/t:rebuild:
采用重新編譯
/p:Configuration=release:
release模式編譯,還有debug模式
/p:WebProjectOutputDir:
web項目文件輸出目錄
/p:OutputPath:
dll文件輸出目錄
一些其他得參數:
PublishProfile:
在用VS執行發布操作時,會生成這個文件。里面指定了發布操作的各種配置參數,比如發布路徑,基于X86/X64平臺等
SolutionDir:
解決方案文件夾
VisualStudioVersion:
安裝VisualStudio時會安裝一些SDK,這個參數告訴MSBuild該去哪里找這些SDK。由于我安裝的是VisualStudio 2013,因此此處填寫了14.0。如果不想每次都填寫這個參數,也可以在csproj里面搜索這個參數名稱,填寫一個默認值。
Platform=x86:
編譯程序所使用得
TargetFrameworkVersion=v3.5:
指定編譯的.net版本號