![azblob://2022/11/11/eyecatch/2020-07-22-azure-functions-azure-devops-ci-cd-000.png](https://prtechblogfd-crhzb5g6hkhqafdm.z01.azurefd.net/drupalimages/2022/11/11/eyecatch/2020-07-22-azure-functions-azure-devops-ci-cd-000.png)
こんにちは、最近久々にFunctionsを触ってる石川です。
今回はAzure Functionsをいい感じに開発したくなったので記事を書いています。(CIといいつつテストしてない部分はゆるしてください)
まずは今回組み立てる(恐ろしく雑な)概要図をペタリ
![](https://prtechblogfd-crhzb5g6hkhqafdm.z01.azurefd.net/drupalimages/beforemigration/2020-07-22-azure-functions-azure-devops-ci-cd-001-1024x576.png)
GitHubにホストしているリポジトリの変更をAzure DevOpsのPipelinesで監視、developブランチに変更があったら自動でAzure Functionsへデプロイを行うといったものです。単純。
まずはビルドパイプラインの方を設定していきます。
![](https://prtechblogfd-crhzb5g6hkhqafdm.z01.azurefd.net/drupalimages/beforemigration/2020-07-22-azure-functions-azure-devops-ci-cd-002.png)
New pipelinesからリポジトリを選択して、Starter pipelineを選択。
![](https://prtechblogfd-crhzb5g6hkhqafdm.z01.azurefd.net/drupalimages/beforemigration/2020-07-22-azure-functions-azure-devops-ci-cd-003-1024x515.png)
こんな感じでテンプレートのymlが表示されるのでガリガリ書いていきます。
今回書くymlで行いたいことは以下のような感じ。
- dotnet restore
- dotnet publish
- ビルド成果物をzipで固める
- artifactsに置く
では早速と行きたいところですがパイプラインが動き出す契機の設定をします。
trigger:
- develop
pr: none
triggerで監視するブランチを指定します。pr: none はPR作成時に動き出さないようにする魔法の言葉です。
では早速、dotnetコマンドから捌いていきましょう。
yml編集画面にはアシスタントがありますのでそちらを活用します。
![](https://prtechblogfd-crhzb5g6hkhqafdm.z01.azurefd.net/drupalimages/beforemigration/2020-07-22-azure-functions-azure-devops-ci-cd-004-1024x571.png)
赤枠の部分をクリックすると現れますので使い倒していきます。
![](https://prtechblogfd-crhzb5g6hkhqafdm.z01.azurefd.net/drupalimages/beforemigration/2020-07-22-azure-functions-azure-devops-ci-cd-005.png)
先頭にある .NET Core を選択します。
![](https://prtechblogfd-crhzb5g6hkhqafdm.z01.azurefd.net/drupalimages/beforemigration/2020-07-22-azure-functions-azure-devops-ci-cd-006-1.png)
dotnetコマンドがいい感じに使えるのでこれで dotnet publish まで一気に出力させちゃいます。
できたものがこちら
steps:
- task: DotNetCoreCLI@2
inputs:
command: 'restore'
projects: '**/*.csproj'
feedsToUse: 'select'
- task: DotNetCoreCLI@2
inputs:
command: 'publish'
publishWebProjects: false
arguments: '--no-restore -o ./publish'
zipAfterPublish: false
restore した後にビルドして ./publish に成果物を出力するという処理です。
あとはこれを zip にして artifacts に置くと完成です。
![](https://prtechblogfd-crhzb5g6hkhqafdm.z01.azurefd.net/drupalimages/beforemigration/2020-07-22-azure-functions-azure-pipelines-ci-cd-007.png)
こちらもアシスタントがあるので使っていきます。
![](https://prtechblogfd-crhzb5g6hkhqafdm.z01.azurefd.net/drupalimages/beforemigration/2020-07-22-azure-functions-azure-pipelines-ci-cd-008.png)
選択するとこんな感じ。
今回は成果物を ./publish に置いてますので Root folder or file to archive に $(System.DefaultWorkingDirectory)/publish を指定します。
そして生成されるymlがこちら
- task: ArchiveFiles@2
inputs:
rootFolderOrFile: '$(System.DefaultWorkingDirectory)/publish'
includeRootFolder: false
archiveType: 'zip'
archiveFile: '$(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip'
replaceExistingArchive: true
Prepend root folder name to archive paths は チェックを外しておきます。
同じ要領でリリースパイプラインで利用するためにartifactsに置く部分も設定します。
![](https://prtechblogfd-crhzb5g6hkhqafdm.z01.azurefd.net/drupalimages/beforemigration/2020-07-22-azure-functions-azure-devops-ci-cd-009-1.png)
publishで検索すると見つかるのでそれを選択。
各設定値はデフォルトのままでOKです。
生成されるymlはこちら
- task: PublishBuildArtifacts@1
inputs:
PathtoPublish: '$(Build.ArtifactStagingDirectory)'
ArtifactName: 'drop'
publishLocation: 'Container'
あとはSave and Runを押して実行しましょう!
![](https://prtechblogfd-crhzb5g6hkhqafdm.z01.azurefd.net/drupalimages/beforemigration/2020-07-22-azure-functions-azure-devops-ci-cd-011.png)
最後まで走ればOK!!!
ということでビルドパイプラインが完成しました。次回はリリースパイプラインを作っていきます。
連休明け予定………!
それでは。