先日の記事で、Azure DevOps の Multi-Stage Pipelines を使って Azure Web Apps にデプロイする内容を記載しました。
gooner.hateblo.jp
今回は、Azure Functions 向けのパイプラインを構築します。Azure Web Apps との違いは少ないので、相違点のみを記載します。
パイプラインの全体設計
先日の記事と同様に、パイプラインは、自動デプロイされる開発環境向け(Commit Stage)と承認デプロイされる本番環境向け(Production Stage)を作ります。YAML を Build と Release を分けて、可変部をパラメータで切り替えできるテンプレートを作ることで再利用性を向上させます。
ビルド用パイプライン
まず、ビルド用の build-pipelines.yml です。
parameters: imageName: '' buildConfiguration: '' projects: '' testProjects: '' jobs: - job: Build pool: vmImage: ${{parameters.imageName}} steps: - task: DotNetCoreCLI@2 displayName: 'Build' inputs: command: 'build' projects: ${{parameters.projects}} arguments: '--configuration ${{parameters.buildConfiguration}}' - task: DotNetCoreCLI@2 displayName: 'Test' inputs: command: 'test' projects: ${{parameters.testProjects}} arguments: '--configuration ${{parameters.buildConfiguration}}' - task: DotNetCoreCLI@2 displayName: 'Publish' inputs: command: 'publish' publishWebProjects: false arguments: '--configuration ${{parameters.buildConfiguration}} --output $(System.DefaultWorkingDirectory)/publish' zipAfterPublish: true - publish: publish displayName: 'Publish artifact' artifact: functionApp
Azure Web Apps との違いは、publishWebProjects
の値を ture
から false
に変更した部分のみです。
リリース用パイプライン
次に、リリース用の release-pipelines.yml です。
parameters: imageName: '' azureSubscription: '' functionAppsName: '' functionAppsType: '' functionAppsSlotName: 'production' environment: '' keyVaultEndpoint: '' jobs: - deployment: Deploy_Azure_Functions displayName: 'Release' pool: vmImage: ${{parameters.imageName}} environment: ${{parameters.environment}} strategy: runOnce: deploy: steps: - task: AzureFunctionApp@1 displayName: 'Deploy to Azure Functions' inputs: azureSubscription: ${{parameters.azureSubscription}} appType: ${{parameters.functionAppsType}} appName: ${{parameters.functionAppsName}} slotName: ${{parameters.functionAppsSlotName}} package: '$(Pipeline.Workspace)/**/*.zip' deploymentMethod: runFromPackage appSettings: -KeyVault:Endpoint ${{parameters.keyVaultEndpoint}}
Azure Web Apps との違いは、Azure Function App task の AzureFunctionApp@1
を使って、デプロイを行っている部分のみです。
docs.microsoft.com
まとめ
パイプライン起動用の azure-pipelines.yml と azure-pipelines-production.yml については、変更する必要はありません。
あとは、Variable groups を使って、パイプラインごとにデプロイ先の Azure Functions の名前を渡してあげれば、OKです。
こちらから、パイプライン YAML の全体を確認できます。
github.com