.NET實現自動編譯


前言 

因每次發布版本的時候,都需要打開vs項目,然后進行編譯。如果剛好手里有文件在修改,就需要先簽入之類的。所以想找個可以實現自動編譯的工具。

在網上查詢了不少資料,終於基本上實現了自動編譯的功能。因為自動部署涉及到服務器管理,本人也不熟悉,就不討論了。

 

一、環境准備

1.  我這邊使用的是git,也可以用svn之類的其他版本控制器。git服務器用的是gitlab. 

2. 下載git windows客戶端,在自動編譯服務器上安裝。

3. 下載TortoiseGit  windows客戶端, 安裝。

4. 在任意目錄上右鍵,選擇Git Bash here, 輸入ssh-keygen, 一直默認回車。 之后會在用戶目錄(c:\user\你的系統用戶名\.ssh)下生成2個文件: id_rsa和id_rsa.pub.

5. 打開開始菜單,打開TortioseGit里面的PuTTYGen 。 點擊Conversion下的Import Key, 打開之前生成的id_rsa(不帶后綴的)。復制輸入框中的ssh-rsa那一大串文字到gitlab里面的ssh keys.  點擊Save private key, 保存文件為id_rsa.ppk。

6. 下載jenkins安裝。需要安裝的插件:git plugin, msbuild plugin, credentials binding plugin, Parameterized trigger plugin.

7. 下載nuget.exe.

 

二、編寫msbuild

1.  新建一個空解決方案Lake, 里面有Lake.Web這個web項目。

2. 在解決方案根目錄增加一個文件: build.msbuild。

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build-Release" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <ItemGroup>
        <BuildArtifactsDir Include="BuildArtifacts\" />
        <Solution Include="$(MSBuildThisFileDirectory)Lake.Web\Lake.Web.csproj">
            <Properties>
              Platform=Any CPU;
              Configuration=Release;
              DeployOnBuild=True;
              DeployTarget=Package;
              _PackageTempDir=$(OutDir);
              AutoParameterizationWebConfigConnectionStrings=false;
              UseWPP_CopyWebApplication=true;
              PipelineDependsOnBuild=false;
              OutputPath=..\Published\Lake.Web
            </Properties>
        </Solution>
    </ItemGroup>
    <PropertyGroup>
        <Configuration Condition=" '$(Configuration)' == '' ">Release</Configuration>
        <BuildPlatform Condition=" '$(BuildPlatform)' == '' ">Any CPU</BuildPlatform>
        <OutDir>$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), common.targets))\release\Lake.Web</OutDir>
    </PropertyGroup>
    <Target Name="Build-Release" BeforeTargets="BeforeBuild"
            AfterTargets="AfterBuild"
            DependsOnTargets="Init">
        <MSBuild Projects="@(Solution)" 
                 Targets="Rebuild">
            <Output TaskParameter="TargetOutputs" ItemName="Assemblies"  />
        </MSBuild>
    </Target>
    <Target Name="CopyContentFiles" AfterTargets="Build">
    </Target>
    <Target Name="Clean">
      <RemoveDir Directories ="$(MSBuildThisFileDirectory)..\Published\Lake.Web" />
      <RemoveDir Directories="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), common.targets))\release\Lake.Web" />
    </Target>
    <Target Name="Init" DependsOnTargets="Clean">
        <!-- <MakeDir Directories="@(BuildArtifactsDir)" />  -->
    </Target>
    <Target Name="RunUnitTests" DependsOnTargets="Compile">
        <Exec Command='"@(NUnitConsole)" @(UnitTestsDLL) /xml=@(TestResultsPath)' />
    </Target>
    <Target Name="BeforeBuild">
        <Message Text="%(AssembliesBuiltByChildProjects.Identity)" />
    </Target>
    <Target Name="AfterBuild">
        <Message Text="after build" Importance="high" />
    </Target>
</Project>
msbuild

 這里面有幾個目錄: 

  $([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), common.targets))\release\Lake.Web   這個是表示從當前目錄,一直往上找,找到一個common.targets的文件,最終的的發布文件會在這個目錄里面的release\Lake.web下。

  ..\Published\Lake.Web  這個目錄表示編譯后的輸出目錄。

3. 簽入提交到git.

 

三、配置Jenkins

1.  打開Manager Jenkins/Configure System,  點擊Home directory 下的Advanced...,  其中Workspace Root Directory就是以后所有的git repoistory放置的目錄。

  我這里設置的是: D:\git/${ITEM_FULLNAME} .  

  然后在這個目錄下(d:\git)新建一個空文件: common.targets.

  把之前下載的nuget.exe也復制到這里。

2.  Manager Jenkins/ Global Tool Configuration

  Git-Path to Git executable, 這個是你的git安裝地址: C:\Program Files\Git\bin\git.exe

  MSBuild-> MSBuild installations: Name:Dotnet4.0, Path to MSBuild: C:\Program Files (x86)\MSBuild\14.0\Bin\  注: 這個path是看你安裝的.net版本.

 

四、發布任務

1.  New Item

  Enter an item name, 然后選擇Freestyle project.

2. Git

  Repositoryies: git@192.168.1.8:.net/Lake.git  (git里面可以查看到)

  Credentials: 點擊Add, 在key里面輸入之前id_rsa.ppk內容。

    

 

   Repository browser: gitlab

   version: 6.5

 3. Build: Add build step -> Build a Visual Studio project or solution using MSBuild

   MSBuild Version: 之前MSBuild輸入的Dotnet4.0

  MSBuild Build File:  D:\git\Lake\build.msbuild

4. 如果這個任務依賴於別的任務,在此項目之前,必須編譯另外一個任務,在Add build step里面選擇Trigger/call builds onother projects. 然后輸入另外一個Projects的名稱。 勾選Block until.

5. 還原Nuget包

  Add build step -> Execute Windows batch command.

  輸入: d:\git\nuget.exe restore "D:\git\Lake\Lake.sln"

6. Post-build Actions

  Archive the artifacts(這個主要是為了nuget下載的包,每次編譯之前保留)

  File to archive: packages/**

7. 如果有時候提示git沒有權限,則進入服務,切換Jenkins的運行在為當前用戶。

8. Build Now, 可以看到成功還是失敗。

9. 所有的編譯文件,都在之前build.msbuild里面的release\Lake.web

 

 

五、參考資料

1. 用MSBuild和Jenkins搭建持續集成環境(1)

2. .NET Web Development and Tools Blog

3. Configure NuGet Package Restoration


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM