週五. 11 月 15th, 2024
Github Actions build .net framework MSDeploy to iis

紀錄一下設定Github Actions的過程,目前少數專案使用.net Framework框架的網頁系統,開發完成後需要手動部屬至IIS。這次設定目的是為了讓開發branch進到master分支時,可以自動Build並且Deploy至IIS,這個自動化過程,相信可以減少開發中繁瑣的事。

Github Actions建立workflows

於專案內Actions點選 New workflows。

Github Actions new workflow

加入Msbuild

於搜尋列輸入Msbuild

搜尋MSBuild
選擇MSBuild based projects

這時候Actions已經有基本的Build功能了。

加上msdeploy部屬至IIS

msdeploy用來部屬至IIS,這部分參考Markplace內Build-Test-MSDeploy,只把MSDeply部分複製過來。

以下為參數的區塊,複製過來。

    env:
      PROJECT_DIRECTORY_PATH: "Enjoy"
      IIS_SERVER_COMPUTER_NAME: ${{ secrets.IIS_SERVER_COMPUTER_NAME }}
      IIS_WEBSITE_NAME: ${{ secrets.IIS_WEBSITE_NAME }}
      IIS_SERVER_USERNAME: ${{ secrets.IIS_SERVER_USERNAME }}
      IIS_SERVER_PASSWORD: ${{ secrets.IIS_SERVER_PASSWORD }}
      RECYCLE_APP_NAME: ${{ secrets.RECYCLE_APP_NAME }}
      WEBSITE_CONTENT_PATH: ${{ secrets.WEBSITE_CONTENT_PATH }}

以下為msdeploy的區塊,複製過來。

 - name: Stop Application Pool
      shell: cmd
      run: '"C:/Program Files (x86)/IIS/Microsoft Web Deploy V3/msdeploy.exe" -verb:sync -allowUntrusted -source:recycleApp -dest:recycleApp="${{ env.RECYCLE_APP_NAME }}",recycleMode="StopAppPool",computerName="${{ env.IIS_SERVER_COMPUTER_NAME }}/msdeploy.axd?site=${{ env.IIS_WEBSITE_NAME }}",username="${{ env.IIS_SERVER_USERNAME }}",password="${{ env.IIS_SERVER_PASSWORD }}",AuthType="Basic"'
    - name: Deploy Application
      shell: cmd
      run: '"C:/Program Files (x86)/IIS/Microsoft Web Deploy V3/msdeploy.exe" -verb:sync -allowUntrusted -source:contentPath="%CD%\${{ env.PROJECT_DIRECTORY_PATH }}\bin\Release\netcoreapp3.1\" -dest:contentPath="${{ env.WEBSITE_CONTENT_PATH }}",computerName="${{ env.IIS_SERVER_COMPUTER_NAME }}/msdeploy.axd?site=${{ env.IIS_WEBSITE_NAME }}",username="${{ env.IIS_SERVER_USERNAME }}",password="${{ env.IIS_SERVER_PASSWORD }}",authtype="Basic"'
    - name: Start Application Pool
      shell: cmd
      run: '"C:/Program Files (x86)/IIS/Microsoft Web Deploy V3/msdeploy.exe" -verb:sync -allowUntrusted -source:recycleApp -dest:recycleApp="${{ env.RECYCLE_APP_NAME }}",recycleMode="StartAppPool",computerName="${{ env.IIS_SERVER_COMPUTER_NAME }}/msdeploy.axd?site=${{ env.IIS_WEBSITE_NAME }}",username="${{ env.IIS_SERVER_USERNAME }}",password="${{ env.IIS_SERVER_PASSWORD }}",AuthType="Basic"'   

以下為合併後程式碼

最後格式如下,專案會多一個目錄.github/workflows,之後要增加或修改actions流程,就可以從這邊改。其中參數PROJECT_DIRECTORY_PATH,請改成自己的專案資料夾名稱,Enjoy.sln請改成自己專案名稱。

依據每個人需求不同,可以自己研究一下msdeploy.exe的參數,以及msbuild.exe的參數,做適當的調整。

name: MSBuild

on:
  push:
    branches: [ master ]
 

jobs:
  build:
    runs-on: windows-latest
    env:
      # Path to the solution file relative to the root of the project.
      SOLUTION_FILE_PATH: .

      # Configuration type to build.
      # You can convert this to a build matrix if you need coverage of multiple configuration types.
      # https://docs.github.com/actions/learn-github-actions/managing-complex-workflows#using-a-build-matrix
      BUILD_CONFIGURATION: Release
      PROJECT_DIRECTORY_PATH: "Enjoy"
      IIS_SERVER_COMPUTER_NAME: ${{ secrets.IIS_SERVER_COMPUTER_NAME }}
      IIS_WEBSITE_NAME: ${{ secrets.IIS_WEBSITE_NAME }}
      IIS_SERVER_USERNAME: ${{ secrets.IIS_SERVER_USERNAME }}
      IIS_SERVER_PASSWORD: ${{ secrets.IIS_SERVER_PASSWORD }}
      RECYCLE_APP_NAME: ${{ secrets.RECYCLE_APP_NAME }}
      WEBSITE_CONTENT_PATH: ${{ secrets.WEBSITE_CONTENT_PATH }}    

    steps:
    - uses: actions/checkout@v2

    - name: Add MSBuild to PATH
      uses: microsoft/[email protected]

    - name: Restore NuGet packages
      working-directory: ${{env.GITHUB_WORKSPACE}}
      run: nuget restore ${{env.SOLUTION_FILE_PATH}}

    - name: Build
      working-directory: ${{env.GITHUB_WORKSPACE}}
      # Add additional options to the MSBuild command line here (like platform or verbosity level).
      # See https://docs.microsoft.com/visualstudio/msbuild/msbuild-command-line-reference
      run: msbuild.exe Enjoy.sln /nologo /nr:false /p:DeployOnBuild=true /p:DeployDefaultTarget=WebPublish /p:WebPublishMethod=FileSystem /p:DeleteExistingFiles=True /p:platform="Any CPU" /p:configuration="Release" /p:PublishUrl="../_build"
      
      
    - name: Stop Application Pool
      shell: cmd
      run: '"C:/Program Files (x86)/IIS/Microsoft Web Deploy V3/msdeploy.exe" -verb:sync -allowUntrusted -source:recycleApp -dest:recycleApp="${{ env.RECYCLE_APP_NAME }}",recycleMode="StopAppPool",computerName="${{ env.IIS_SERVER_COMPUTER_NAME }}/msdeploy.axd?site=${{ env.IIS_WEBSITE_NAME }}",username="${{ env.IIS_SERVER_USERNAME }}",password="${{ env.IIS_SERVER_PASSWORD }}",AuthType="Basic"'
    - name: Deploy Application
      shell: cmd
      run: '"C:/Program Files (x86)/IIS/Microsoft Web Deploy V3/msdeploy.exe" -verb:sync -allowUntrusted -enableRule:DoNotDeleteRule -source:contentPath="%CD%\_build" -dest:contentPath="${{ env.WEBSITE_CONTENT_PATH }}",computerName="${{ env.IIS_SERVER_COMPUTER_NAME }}/msdeploy.axd?site=${{ env.IIS_WEBSITE_NAME }}",username="${{ env.IIS_SERVER_USERNAME }}",password="${{ env.IIS_SERVER_PASSWORD }}",authtype="Basic"'
    - name: Start Application Pool
      shell: cmd
      run: '"C:/Program Files (x86)/IIS/Microsoft Web Deploy V3/msdeploy.exe" -verb:sync -allowUntrusted -source:recycleApp -dest:recycleApp="${{ env.RECYCLE_APP_NAME }}",recycleMode="StartAppPool",computerName="${{ env.IIS_SERVER_COMPUTER_NAME }}/msdeploy.axd?site=${{ env.IIS_WEBSITE_NAME }}",username="${{ env.IIS_SERVER_USERNAME }}",password="${{ env.IIS_SERVER_PASSWORD }}",AuthType="Basic"'   

設定IIS參數

以下為設定參數需要填的格式,需要按照格式填入Actions Secrets,才能夠順利Deploy到IIS。另外Server端IIS的MSDeploy也要進行設定,自行搜尋網路有許多文章,這部分就不討論。

IIS_SERVER_COMPUTER_NAMEhttps://yourserverIP:8172/msdeploy.axd
IIS_SERVER_PASSWORDwindows密碼
IIS_SERVER_USERNAMEwindows帳號
IIS_WEBSITE_NAMEenjoyTest
RECYCLE_APP_NAMEenjoyTest
WEBSITE_CONTENT_PATHC:\inetpub\wwwroot\enjoyTest

Run Actions

到這邊就設定完成了。

設定完成後,Actions會自動Run一次,可到Actions內看結果。往後只要push或是pr進master,就會自動build並且Deploy上IIS囉。