Azure devops YAMLパイプラインを使っているIISに配備する方法


このブログは、仮想マシン上でAzure devops YAMLパイプラインを使用してIISに展開する方法に関する実用的なガイドとして意図されています.Azure devopsについてもっと読みたいならば、devopsの利点を見てみてください.
https://docs.microsoft.com/en-us/azure/devops/pipelines/?view=azure-devops

始める前にいくつか仮定します。

  • に配備して管理するサーバへのアクセスがあります
    PowerShellへのアクセス.
  • 場合は、リモートサーバー上のインターネットにアクセスする必要があります
    展開します.
  • インストールするには管理者権限があります.ネットコアホスティング
    サーバ.
  • あなたは環境を作成し、あなたのrepoにコードをプッシュし、作成することができます
    パイプライン
  • あなたが持っている.NETコアAPIを展開します.
  • 環境の作成

  • パイプラインに移動し、環境を選択します.
  • 選択環境を選択します.
  • 環境の名前を入力し、説明を入力します
    「仮想マシン」を選択し、「次へ」をクリックします.

  • 次に、
  • ドロップダウンして選択するジェネリックプロバイダーを選択します
    オペレーティングシステムとしてのWindows次に、
    コピーアイコンを使用した登録スクリプト.
  • Windowsの管理用PowerShell端末を開きます
    展開するマシンを登録します
    端末とスクリプトを実行します.このステップは通常
    一方.
  • エージェントがダウンロードされるとき、あなたがそうするならば、あなたはダウンロードされます
    したいマシンにタグを追加します.この場合は
    環境中の単一のマシンですが、追加する必要があります
    あなたが複数のマシンを持っているなら、関連タグ
    環境.
  • あなたは、あなたがそれぞれのために解凍したいかどうか尋ねるよう促されます
    タスクは、必要ありません-ので、あなたは言うことができます
  • そして、あなたはエージェントのためにユーザーアカウントを入力するよう促されます
    マシン上で走るあなたはデフォルトとしてそれを残すか、または
    エージェントが実行する新しいサービスアカウント.
    あなたのエージェントの作成が成功すると、Azure devopsに戻って、あなたの仮想マシンが環境のリソースとして追加されるのを見ることができます!

    ビルドステージの作成

  • パイプラインに移動し、パイプラインを選択します.
  • パイプラインを作成し、アプリケーションのソースに接続する
    コード.
  • 選択したパイプラインのステップを選択し、次に選択する
    ASP .ネットコア.

  • そして、あなたはASPのためにベースパイプラインを持っています.NETコアアプリケーションでは、次のコードスニペットを追加することで、ビルド段階を追加できます.
    trigger:
    - master
    
    pool:
      vmImage: ubuntu-latest
    
    variables:
      buildConfiguration: 'Release'
    
      #Replace these variables to suit your application
      projectName: 'WeatherService'
      websiteName: 'WeatherService'
      appPoolName: 'WeatherService'
    
    stages:
    - stage: 'Build'
      displayName: 'Build'
      jobs:
         - job: 
           steps:  
            - task: DotNetCoreCLI@2
              displayName: 'dotnet restore'
              inputs:
                command: 'restore'
                projects: '*.sln'
    
            - task: DotNetCoreCLI@2
              displayName: Build
              inputs:
                command: 'build'
                projects: '*.sln'
                arguments: --configuration Release
    
            - task: DotNetCoreCLI@2
              displayName: Test
              inputs:
                command: test
                projects: '*.sln'
                arguments: '--configuration $(BuildConfiguration)'
    
            - task: DotNetCoreCLI@2
              displayName: 'Publish the project - $(buildConfiguration)'
              inputs:
                command: 'publish'
                projects: '**/*.csproj'
                publishWebProjects: false
                arguments: '--no-build --configuration $(buildConfiguration) --output $(Build.ArtifactStagingDirectory)/$(buildConfiguration)'
                zipAfterPublish: true
    
            - publish: '$(Build.ArtifactStagingDirectory)'
              artifact: drop
    
  • あなたのアプリケーションに合わせて上の変数を置き換えます
    アプリケーションのプール名、ウェブサイトの名前、およびプロジェクト名を置き換える
    プロジェクトの詳細を.
  • をクリックして保存し、実行するパイプラインを構築する
    それはあなたのアプリケーションを作成します.
  • パイプラインの作成

  • パイプラインに行き、パイプラインを選択します.
  • あなたは、左側に作成したパイプラインを見るでしょう
    あなたのパイプラインのハンドサイドは、より多くのオプションを選択し
    エディット.
  • パイプラインがロードされ、次のYAMLを追加できます
    パイプラインへのコード
  • - stage: 'Dev'
      displayName: 'Dev'
      dependsOn: 'Build'
      condition: and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/master'))
      jobs:
       - deployment: Dev
         displayName: Dev
         environment: 
           name: 'Dev'    
           resourceType: VirtualMachine
         variables:
         - name: websitePhysicalPath
           value: '%SystemDrive%\inetpub\wwwroot\$(websiteName)'
    
         strategy:
          runOnce:
            deploy:
              steps: 
              - task: IISWebAppManagementOnMachineGroup@0
                inputs:            
                  IISDeploymentType: 'IISWebsite'
                  ActionIISWebsite: 'CreateOrUpdateWebsite'
                  WebsiteName: '$(websiteName)'
                  WebsitePhysicalPath: '$(websitePhysicalPath)'
                  WebsitePhysicalPathAuth: 'WebsiteUserPassThrough'
                  CreateOrUpdateAppPoolForWebsite: true
                  AppPoolNameForWebsite: '$(appPoolName)'
                  DotNetVersionForWebsite: 'No Managed Code'
                  PipeLineModeForWebsite: 'Integrated'
                  AppPoolIdentityForWebsite: 'ApplicationPoolIdentity'
                  AddBinding: true
                  Bindings: |
                      {
                          bindings:[
                              {
                                  "protocol":"http",
                                  "ipAddress":"",
                                  "hostname":"",
                                  "port":"80",
                                  "sslThumbprint":"",
                                  "sniFlag":false
                              }
                          ]
                      }
              - task: IISWebAppDeploymentOnMachineGroup@0
                inputs:
                  WebSiteName: '$(websiteName)'
                  Package: '$(Pipeline.Workspace)/drop/$(buildConfiguration)/$(projectName).zip'
    
    
    スクリプトを選択して保存して実行すると
    以前に作成した環境にそれを解放します.

    回収する


    スクリプトを実行すると、次のようになります.
    trigger:
    - master
    
    pool:
      vmImage: ubuntu-latest
    
    variables:
      buildConfiguration: 'Release'
      projectName: 'WeatherService'
      websiteName: 'WeatherService'
      appPoolName: 'WeatherService'
    
    stages:
    - stage: 'Build'
      displayName: 'Build'
      jobs:
         - job: 
           steps:  
            - task: DotNetCoreCLI@2
              displayName: 'dotnet restore'
              inputs:
                command: 'restore'
                projects: '*.sln'
    
            - task: DotNetCoreCLI@2
              displayName: Build
              inputs:
                command: 'build'
                projects: '*.sln'
                arguments: --configuration Release
    
            - task: DotNetCoreCLI@2
              displayName: Test
              inputs:
                command: test
                projects: '*.sln'
                arguments: '--configuration $(BuildConfiguration)'
    
            - task: DotNetCoreCLI@2
              displayName: 'Publish the project - $(buildConfiguration)'
              inputs:
                command: 'publish'
                projects: '**/*.csproj'
                publishWebProjects: false
                arguments: '--no-build --configuration $(buildConfiguration) --output $(Build.ArtifactStagingDirectory)/$(buildConfiguration)'
                zipAfterPublish: true
    
            - publish: '$(Build.ArtifactStagingDirectory)'
              artifact: drop
    
    - stage: 'Dev'
      displayName: 'Dev'
      dependsOn: 'Build'
      condition: and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/master'))
      jobs:
       - deployment: Dev
         displayName: Dev
         environment: 
           name: 'Dev'    
           resourceType: VirtualMachine
         variables:
         - name: websitePhysicalPath
           value: '%SystemDrive%\inetpub\wwwroot\$(websiteName)'
    
         strategy:
          runOnce:
            deploy:
              steps: 
              - task: IISWebAppManagementOnMachineGroup@0
                inputs:            
                  IISDeploymentType: 'IISWebsite'
                  ActionIISWebsite: 'CreateOrUpdateWebsite'
                  WebsiteName: '$(websiteName)'
                  WebsitePhysicalPath: '$(websitePhysicalPath)'
                  WebsitePhysicalPathAuth: 'WebsiteUserPassThrough'
                  CreateOrUpdateAppPoolForWebsite: true
                  AppPoolNameForWebsite: '$(appPoolName)'
                  DotNetVersionForWebsite: 'No Managed Code'
                  PipeLineModeForWebsite: 'Integrated'
                  AppPoolIdentityForWebsite: 'ApplicationPoolIdentity'
                  AddBinding: true
                  Bindings: |
                      {
                          bindings:[
                              {
                                  "protocol":"http",
                                  "ipAddress":"",
                                  "hostname":"",
                                  "port":"80",
                                  "sslThumbprint":"",
                                  "sniFlag":false
                              }
                          ]
                      }
              - task: IISWebAppDeploymentOnMachineGroup@0
                inputs:
                  WebSiteName: '$(websiteName)'
                  Package: '$(Pipeline.Workspace)/drop/$(buildConfiguration)/$(projectName).zip'
    
    
    また、パイプラインをDEVと呼ぶ環境を構築する必要があります.

    パイプラインに移動すると、次のようになります.

    この例のリポジトリも見てください.
    https://github.com/Bassonrichard/AzureDevopsIISDEploy

    あなたがこれをリリースするとき、あなたはあなたの解決策の配備の容易さで将来のためにあなたの解決策を準備して、YAMLパイプラインを使っているDEVOPSパイプラインセットアップをします.