close

  在Jenkins的世界,必須用command 的方式啟動msbuild 程序自動建置Visual Studio.Net with C# 專案,而欲使Jenkins建置後產生的package可以用來部署到我們想要部署的任何環境,則必然得在build專案階段決定Web Config的內容,因此我們可以使用TransformWebConfig的方式動態置換原始的web.config檔。

 

  要完成這個任務必需完成底下4件事:

  1. Visual studio設定環境配置,並定義輸出package的位置。
  2. 在專案中新增TransformWebConfig.config檔案,定義要置換的參數與值。
  3. 修改專案檔建置Target Task以新增web.config置換流程。
  4. Jekins中設定msbuild指令。

以下我們一項一項來逐一檢視每個任務所必需做的事。

 

  1. Visual studio設定環境配置,並定義輸出package的位置

1). 開啟目標專案,然後從功能表選擇[建置]->[組態管理器]

 

2). 開啟環境組態管理視窗,在活動解決方案組態的下拉式選單點一下展開,然後按<新建>新增各環境組態,各組態直接在新建時選擇複製Release組態即可,依此我建置了QA.BETA.STG.AWS-BETA.PROD等環境組態。

 

3). 建置好每一個環境配置選項後,每個環境中,目標專案應該要指定對應的環境名稱,下面以QA環境為例:

 

4). 最後我們可以從功具列中看到已配置好的所有環境。

5). 接下來要到目標專案的.csproj檔中指定所有環境在建置(Build)之後將產生的package放置到bin資料夾底下。

  => 要從Visual Studio打開.csproj檔,首先得卸載專案。(若想直接Edit可以用其他Editor直接開啟檔案,這邊仍以Visual Studio方式示範)。從方案資源管理器選擇目標專案,然後[右鍵->卸載專案]

 

 卸載專案後便可打開專案內容,然後修改檔案中部份內容如下:

  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'QA|AnyCPU' Or '$(Configuration)|$(Platform)' == 'BETA|AnyCPU' Or '$(Configuration)|$(Platform)' == 'STG|AnyCPU' Or '$(Configuration)|$(Platform)' == 'PROD|AnyCPU'">

    <OutputPath>bin\</OutputPath>

 </PropertyGroup>

    以上內容將會指定把各環境package丟到bin folder底下。完成以上修正再存檔把專案加載起來即可。

  1. 在專案中新增TransformWebConfig.config檔案,定義要置換的參數與值

1). 在新增環境組態之後,可以在方案管理中把開專案在web.config按右鍵->添加配置轉換便可以產生用來在各環境中作轉換的預設web.{ENV}.config 檔。

 

在按下配置轉換後 :

 

2). 接著在每個環境config檔中加入欲配置的參數與值,此將在build project時以該config檔的內容取代原始web.config內容。

  • web.BETA.config為例:

<?xml version="1.0" encoding="utf-8"?>

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">

  <appSettings>

    <add key="LIBERAL_API_URL" value="http://example-beta.com.tw/LiberalAPI" xdt:Transform="Replace" xdt:Locator="Match(key)"/>

    <add key="CLP_URL" value="https:// example-beta.com.tw /Dashboard?T={0}" xdt:Transform="Replace" xdt:Locator="Match(key)"/>

    <add key="PIS_API_URL" value="http:// example-beta.com.tw /PIS" xdt:Transform="Replace" xdt:Locator="Match(key)"/>

  </appSettings>

  <system.web></system.web>

</configuration>

 

  1. 修改專案檔建置Target Task以新增web.config置換流程

1). 卸載並編輯目標專案檔內容。

2). 新增底下內容 for Setup the transformation file to use.

<!-- Setup the transformation file to use -->

  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'QA|AnyCPU'">

    <WebConfigReplacement>QA</WebConfigReplacement>

    <Prefer32Bit>false</Prefer32Bit>

  </PropertyGroup>

  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'BETA|AnyCPU'">

    <WebConfigReplacement>BETA</WebConfigReplacement>

    <Prefer32Bit>false</Prefer32Bit>

  </PropertyGroup>

  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'STG|AnyCPU'">

    <WebConfigReplacement>STG</WebConfigReplacement>

    <Prefer32Bit>false</Prefer32Bit>

  </PropertyGroup>

  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'AWS-BETA|AnyCPU'">

    <WebConfigReplacement>AWS-BETA</WebConfigReplacement>

    <Prefer32Bit>false</Prefer32Bit>

  </PropertyGroup>

  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'PROD|AnyCPU'">

    <WebConfigReplacement>PROD</WebConfigReplacement>

    <Prefer32Bit>false</Prefer32Bit>

  </PropertyGroup>

 

3). 建立 TransformWebConfig target 以讓專案在建置時可以直接轉換web.config並以新的內容取代原來的web.config

<UsingTask TaskName="TransformXml" AssemblyFile="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v11.0\Web\Microsoft.Web.Publishing.Tasks.dll" />

  <PropertyGroup>

    <TransformInputFile>$(OutputPath)\Deployment\Web.Temp.config</TransformInputFile>

    <TransformFile>Deployment\Web.$(WebConfigReplacement).config</TransformFile>

    <TransformOutputFile>Web.config</TransformOutputFile>

    <StackTraceEnabled>False</StackTraceEnabled>

  </PropertyGroup>

  <ItemGroup>

    <OriginalWebConfig Include="Web.config" />

    <TempWebConfig Include="$(OutputPath)\Deployment\Web.Temp.config" />

  </ItemGroup>

  <Target Name="TransformWebConfig" Condition="'$(Configuration)|$(Platform)' == 'QA|AnyCPU' Or '$(Configuration)|$(Platform)' == 'BETA|AnyCPU'">

    <!-- Copy our web.config into a temp folder as the 'TransformXml' task has a file lock bug -->

    <Copy SourceFiles="@(OriginalWebConfig)" DestinationFiles="@(TempWebConfig)" />

    <TransformXml Source="$(TransformInputFile)" Transform="$(TransformFile)" Destination="$(TransformOutputFile)" StackTrace="$(StackTraceEnabled)" />

    <Delete Files="@(TempWebConfig)" />

  </Target>

 

4). 最後,因為我們在Jenkins上是指定要build .sln 方案檔,所以預設上當方案build到目標專案時預設會執行底下targets:

  • <Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  • TransformWebConfig target,因此專案web.config便不會被置換,所以必需針對當方案build到目標專案的.csproj檔時必須能夠多執行TransformWebConfig的指令:

 <A>. change DefaultTargets

<Project ToolsVersion="4.0" DefaultTargets="BuildWithConfig" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

 <B>. 新增底下內容指定DefaultTargets要做的事:

<Target Name="BuildWithConfig">

    <CallTarget Targets="Build"/>

    <CallTarget Targets="TransformWebConfig"/>

</Target>

 

  1. Jenkins中設定msbuild指令

最後當然就是在Jenkins中指定要怎麼執行msbuild啦,這樣當每一次專案建置都會在建置專案完成後連web.config也一併根據環境置換了。

 

 

[Reference]

http://stackoverflow.com/questions/11844938/calling-transformwebconfig-on-a-solution-file

 

arrow
arrow

    jackterrylau 發表在 痞客邦 留言(0) 人氣()