Share
Embed
Transcript
69 slides
-
About me Name:Masui Masanori Work:Unityでアプリ作ったり無茶振りされたり Twitter:https://twitter.com/Masanori_msl
Blog:http://mslgt.hatenablog.com/ -
CI ツール、または Azure Pipelines 初心者の人 CI ツールおよび Azure
Pipelines に詳しい人 和歌山人 対象者 一緒に頑張りましょう! 色々教えてください_(._.)_ ジョークです。和歌山以外の方も生暖かく見守っていただければ_(._.)_ -
CI について 1 CI (Continuous Integration : 継続的インテグレーション) 共有リポジトリ上のソースコードに追加・削除などの変更が行われたときに、
都度ビルド・テストを実行することで、早期に問題を発見・修正し、 効率的にソフトウェア開発を行うための手法。 決められた条件でビルド・テストを自動で実行する、 といった CI を支援してくれるツールがあり、Azure Pipelines もその一つ。 (俺調べ) -
CI について 2 GitHub Azure Pipelines Push or Pull request
Repository Build & Test Result Watch? Build & Test -
Azure DevOps には Azure Repos という Git ホスティングサービスが ありますが、プライベートリポジトリのみ?だったので 今回は
GitHub を使用することにしました。 Azure Repos? -
開発環境 Windows 10 ver.1803 build ver.17134.472 GitHub Desktop
ver.1.5.0 .NET Core ver.2.2.101 (ASP.NET Core) JetBrains Rider ver.2018.3 Webブラウザー(Edge とか Firefox とか) -
プロジェクト作成 1. ASP.NET Core のプロジェクト作成 する(今回は Empty で作成)。 2. GitHub
にリポジトリを登録する。 3. Azure Pipelines でプロジェクトを作成する。 4. Azure Pipelines (GitHub Apps) をインストールする。 5. Azure Pipelines で Build pipeline を追加する。 6. (おまけ) README.md に Build pipeline のステータスを追加する。 -
1. ASP.NET Core のプロジェクト作成 する IDE または「dotnet new web」で Empty
テンプレートのプロジェクトを作成します。 (画像は JetBrains Rider のもの。Empty テンプレートの理由は生成されるファイルが少ないためです) -
4. Azure Pipelines (GitHub Apps) をインストールする 2 https://github.com/settings/installations/ の Azure
Pipelines > Configure から、 全リポジトリまたは対象のリポジトリへのアクセスを許可します。 -
4. Azure Pipelines (GitHub Apps) をインストールする 3 https://github.com/settings/installations/ の Azure
Pipelines > Configure から、 全リポジトリまたは対象のリポジトリへのアクセスを許可します。 -
4. Azure Pipelines (GitHub Apps) をインストールする 4 これで変更の Push や
Pull request を受けた時に実行された Azure Pipelines の結果が、 GitHub から見られるようになります。 -
5. Azure Pipelines で Build pipeline を追加する 1 作成したプロジェクトに Build
pipeline を追加します。 1. Builds > New pipeline をクリックする。 2. Location で GitHub を選択する。 3. GitHub の認証。 4. Repository 一覧から PipelineSample (対象のリポジトリ)を選択する。 5. Configure で Suggested templates から ASP.NET Core を選択する。 6. Save and run で保存 + 初回ビルド実行。 -
5. Azure Pipelines で Build pipeline を追加する 2 1. Builds
> New pipeline をクリックする -
5. Azure Pipelines で Build pipeline を追加する 3 2. Location
で GitHub を選択する -
5. Azure Pipelines で Build pipeline を追加する 5 4. Repository
一覧から対象のリポジトリを選択する -
5. Azure Pipelines で Build pipeline を追加する 6 5. yaml
のテンプレートを選択する -
5. Azure Pipelines で Build pipeline を追加する 7 6. Save
and run で保存 + 初回ビルド実行 -
5. Azure Pipelines で Build pipeline を追加する 8 6. Save
and run で保存 + 初回ビルド実行 ( azure-pipelines.yml が追加される) -
5. Azure Pipelines で Build pipeline を追加する 9 ※今回 yaml
を直接書く方法を使いましたが(デフォルトっぽく表示されるので)、 特に慣れない内は visual designer(GUI) を使う方がよかったかもしれません。 ググると visual designer を使った情報が多かったためです。 が、 visual designer で作った yaml を見ると( View yaml 見られます)、 かなりガッツリ作られてしまうため、一つ一つ理解する、 という意味では今回の方法の方が良いかもしれません。 -
6. README.md に Build pipeline のステータスを追加する 1 必須ではありませんが、 Build pipeline
のステータスが表示されるようにします。 かっこいいので。 -
6. README.md に Build pipeline のステータスを追加する 2 Sample Markdown の内容を
README.md にコピペします。 -
yaml を見てみる 1 完全に理解したところで出来上がった yaml を見てみます。 # ASP.NET Core #
Build and test ASP.NET Core projects targeting .NET Core. # Add steps that run tests, create a NuGet package, deploy, and more: # https://docs.microsoft.com/azure/devops/pipelines/languages/dotnet-core trigger: - master pool: vmImage: 'Ubuntu-16.04’ variables: buildConfiguration: 'Release’ steps: - script: dotnet build --configuration $(buildConfiguration) displayName: 'dotnet build $(buildConfiguration)' -
yaml を見てみる 2 trigger trigger: - master - features/*
ビルド、テストなどの処理を実行するトリガーとなるブランチを指定します。 ワイルドカードも使用可能。 ↑ master ブランチ、 features/sample のような名前の付いたブランチのコードが変更されると、 Pull request を受けた場合に処理が実行されます。 -
yaml を見てみる 3 pool pool: vmImage: 'Ubuntu-16.04’ ビルド、テストなどの処理を実行する環境上の Agent
を、登録済みの Agent Pool から指定します。 ( Agent については後でもう少し触れます) ↑ 今回は Microsoft 社の提供する Ubuntu 16.04 の VM イメージを使用しています。 (2019/01/08 時点) Windows + Visual Studio の環境もありますが、 ASP.NET Core 2.2 のビルドをするのであればこの環境がエラー無く実行できて良さそうです。 -
yaml を見てみる 4 variables variables: buildConfiguration: 'Release’ steps: -
script: dotnet build --configuration $(buildConfiguration) 変数名: 値 で設定しておくと、Pipeline 実行時に $(変数名) が値に置き換わります。 変更の恐れがあり、かつ複数個所で共通の値を設定している場合に便利ですね。 ↑ dotnet build --configuration Release として実行されます。 -
yaml を見てみる 5 steps steps: - script: dotnet build
--configuration $(buildConfiguration) displayName: 'dotnet build $(buildConfiguration)' ↑ これを実行すると、ビルドのみ行われ、(テストコードがあったとしても)テストは実行されません。 ビルド・テストなどを実行する Pipeline の処理を指定します。 script は複数指定可能で、上から順に実行され、エラーが発生するとその時点で止まります。 -
yaml を見てみる 6 steps displayName 指定するとログが独立したものとして表示されます。 dotnet build
の場合、指定しないと CmdLine としてまとめられるようです。 -
その他 yaml でできることを見てみよう 1 jobs / job jobs: -
job: Build pool: vmImage: 'vs2017-win2016’ steps: - script: dotnet build --configuration $(buildConfiguration) displayName: 'dotnet build $(buildConfiguration)’ - job: Message pool: vmImage: 'Ubuntu-16.04' steps: - script: echo hello world job は複数の steps を一つにまとめます。 jobs は複数の job を一つにまとめます。 -
その他 yaml でできることを見てみよう 2 jobs / job steps に複数
script を書いた場合との違いは、 jobs に登録された job はそれぞれ独立したもの として扱われることです。 そのため、job “Build” の結果に関係なく job “Message” は実行されます。 また job では Agent を指定できるため、 複数の環境で処理を実行できます。 -
その他 yaml でできることを見てみよう 3 template jobs: - template: build_jobs.yml
parameters: buildConfiguration: $(buildConfiguration) jobs や steps を別ファイルに分割することができます。 ※dev.azure.com 上で直接編集できないなどの制限があるため、 たくさんのプロジェクトで共通、 yaml 自体の動作検証が必要ないなど、特定の状況のみに とどめるのが良さそうです。 • azure-pipelines.yml -
その他 yaml でできることを見てみよう 4 template parameters: buildConfiguration: ‘’ jobs:
- job: Build steps: - script: dotnet build --configuration ${{ parameters.buildConfiguration }} displayName: 'dotnet build ${{ parameters.buildConfiguration }}’ - script: dotnet test displayName: 'dotnet test’ - script: echo hello world after build pool: vmImage: 'Ubuntu-16.04' • build_jobs.yml -
Agent について steps で指定したビルド・テストなどの処理を実行する時に、 Pipeline と実行環境との仲介を行います(俺理解)。 Microsoft 社の提供する
Microsoft-hosted agents と、 自分の端末にインストールする Self-hosted agents があります。 各 Agent は Agent pool に登録され、 Pipeline 実行時に呼ばれます。 -
Microsoft-hosted agents Microsoft の提供する VM で動く Agent です。
VM は Pipeline 実行開始時に新規で用意され、すべての処理が終わると破棄されます。 VM にインストールされているソフトウェアは、https://dev.azure.com/ユーザー名 /_settings/agentpools の各 Agent の Details などで一覧できます。 Self-hosted agents を試すより前に、まずはこちらを試して、とのこと。 -
Self-hosted agents 1 自分で準備した環境に https://dev.azure.com/ユーザー名/_settings/agentpools > New agent pool
から Agent を追加し、Download Agent でダウンロード・インストールして 使用します。 特定のバージョンや、 Microsoft-hosted agents の VM でインストールされていないソフトウェア を使用したい場合はこちらを使用します。 ビルド・テストの環境を事前に用意しておくことができるため、正しく管理できればより高速に CIを回すことができる可能性があります。 ただし管理は自分でする必要があります。 -
Self-hosted agents 2 インストールはこの辺りが参考になります。 Deploy an agent on
Windows : https://docs.microsoft.com/en-us/azure/devops/pipelines/agents/v2-windows?view=vsts Continuous integration for Unity 3D projects using Azure Pipelines : https://medium.com/medialesson/continuous-integration-for-unity-3d-projects-using-azure- pipelines-e61ddf64ad79 ※ Edge を使うとなぜか Agent の Token key がコピーできない(2018/12/18 時点では) ので注意 -
処理の流れを考えてみる 登場人物は何となくわかりました(気がする)。 では実際どんな流れでビルドが行われるのでしょうか。 …というのがドキュメント見ててもよくわからなかったので、雑に考えてみました。 1. Azure Pipelines から GitHub を監視して?リポジトリの変更を検出し、Pipeline
の処理を開始。 2. 実行環境となる VM (Ubuntu16.04) を用意し、 Agent を使用可能に( Token ダウンロードとか)。 3. VM 上で GitHub のリポジトリをクローンする。 4. 3 をビルドする( dotnet build )。完了を Agent を通して Azure Pipelines に通知。 5. 全部の処理が完了したら、終了処理( VM の破棄とか)。 -
Azure Pipelines の役割を考えてみる Agent pool による Agent の管理や、 処理開始のトリガーとなる、 GitHub
のリポジトリ 監視(多分)、トリガーが引かれたときに Queue を作成する、といったことをしています。 が、実際にビルドやテストなどの処理をしているのは dotnet CLI など別のソフトウェアです。 そのため、こんな処理を実行したい!と思ったときに、 Azure Pipelines で何とかしようとするよりも、 何でも良いのでとにかくその処理を実現する方法を調べて、それを Azure Pipelines から呼び出す 方法を調べるのが良さそうです。 -
プロジェクトにテストを追加 1 ブランチ features/add_controller を作り、Checkout します。 ASP.NET Core
のプロジェクトに xUnit のプロジェクト( PipelineSample.Test )を追加します。 PipelineSample.Test から ASP.NET Core のプロジェクト( PipelineSample )の参照ができるよう あれこれします。 この辺りのお話はこちらを参照。 【ASP.NET Core】単体テストってみる -導入と Controller のテスト https://mslgt.hatenablog.com/entry/2018/12/20/224613 -
プロジェクトにテストを追加 2 テスト対象のクラスを追加します。 PipelineSample/Controllers/HomeController.cs using Microsoft.AspNetCore.Mvc; namespace PipelineSample.Controllers { public
class HomeController: Controller { [Route("")] [Route("Home")] public string Index() { return "hello world!"; } } } -
プロジェクトにテストを追加 3 テストクラスを追加します。 PipelineSample.Test/Controllers/HomeControllerTest.cs using PipelineSample.Controllers; using Xunit; namespace PipelineSample.Test.Controllers
{ public class HomeControllerTest { private readonly HomeController _controller = new HomeController(); [Fact] public void Index_ReturnStringValue() { Assert.IsType<string>(_controller.Index()); Assert.NotNull(_controller.Index()); } } } -
プロジェクトにテストを追加 4 テストが実行されるよう yaml を変更します(デモの都合上 trigger は master のみ)。 azure-pipelines.yml
trigger: - master pool: vmImage: 'Ubuntu-16.04’ variables: buildConfiguration: 'Release’ steps: - script: dotnet build --configuration $(buildConfiguration) displayName: 'dotnet build $(buildConfiguration)’ - script: dotnet test displayName: 'dotnet test' -
masterへのマージについて 1 Continuous “Integration” という名前から勘違いしていたのですが、 例えば Pull request をマージする、という作業は CI
の定義には含まれていないようです。 (もちろん実際には必ず実行する作業ではあるわけですが) -
masterへのマージについて 2 Azure Pipelines に Pull request を自動でマージする機能は無いようです。 必要な場合、 Git
コマンドを使ってテスト完了後に実行する必要があります。 ただし、そもそも自動でマージすべきか?という部分には疑問もあります。 (プロジェクトによりますが) ※うまく yaml が書けなかった言い訳ではありませんよ!言い訳ではありませんよ! -
Unityのビルドをしたかった話 1 通常 Unity エディター上でビルドしますが、コマンドラインからもビルドできます。 Visual Studio Marketplace
から Unity ビルド用プラグイン?をインストールします。 Azure 上で Unity のビルドができないため、 Self-hosted agents として開発用 PC に Agent を インストールしました。 その他詳しい方法は下記を参照 Unity のプロジェクトを Azure Pipelines でビルドしたくて七転八倒した話 https://mslgt.hatenablog.com/entry/2018/12/18/223443 ともあれ、Unity のプロジェクトをビルドすることはできました。 -
Unityのビルドをしたかった話 4 理由 実はコマンドラインからビルドする場合も、 Unity エディターが使用されます。 自分が Unity
エディターを開いていると、 Azure Pipelines から同じ Unity エディターに アクセスできず、Timeout でエラーとなります。 開発用と全く同じ設定で、別名フォルダーで Unity エディターを用意すると解決するかも(ツラい)。 CI 用にもう一台 Unity エディターが動く PC を用意すると解決するかも(特に個人はツラい)。 -
おわりに ぼっち開発なのとまだ使い始めたばかりということで、正直まだ CI の便利さが分かっていない ような気がします(今後に期待)。 条件的に Microsoft-hosted agent
が使える場合、実行環境の管理がお任せできるので良いですね。 visual designer と yaml で項目が結構違うため混乱ががが Jenkins など他の CI ツールも触ってみつつ、いかにテストなどの反復作業を自動化できるのか? を試していきたいと思います。 -
Reference 1 ▪ CI 継続的インテグレーションとは? – アマゾン ウェブ サービス
https://aws.amazon.com/jp/devops/continuous-integration/ CI – 継続的インテグレーションとは? | tracpath:Works https://tracpath.com/works/devops/continuous-integration/ ▪ Azure Pipelines Create your first pipeline - Azure Pipelines | Microsoft Docs https://docs.microsoft.com/en-us/azure/devops/pipelines/get-started-yaml?view=vsts Build and Release Agents - Azure Pipelines | Microsoft Docs https://docs.microsoft.com/en-us/azure/devops/pipelines/agents/agents?view=vsts Integrate Your GitHub Projects With Azure Pipelines | Azure DevOps Hands-on-Labs https://www.azuredevopslabs.com/labs/azuredevops/github-integration/ Build GitHub repositories - Azure Pipelines | Microsoft Docs https://docs.microsoft.com/en-us/azure/devops/pipelines/repos/github?view=vsts YAML schema - Azure Pipelines | Microsoft Docs https://docs.microsoft.com/en-us/azure/devops/pipelines/yaml-schema?view=vsts&tabs=schema Build pipeline triggers - Azure Pipelines | Microsoft Docs https://docs.microsoft.com/ja- jp/azure/devops/pipelines/build/triggers?view=azdevserver-2019&tabs=yaml Continuous Integration Using Azure DevOps Pipelines And .NET Project https://www.c-sharpcorner.com/article/continuous-integration-using-azure-devops-and-net-project/ Pipeline variables - Azure Pipelines | Microsoft Docs https://docs.microsoft.com/en- us/azure/devops/pipelines/process/variables?view=vsts&tabs=yaml%2Cbatch -
Reference 2 Pipeline variables - Azure Pipelines | Microsoft
Docs https://docs.microsoft.com/en-us/azure/devops/pipelines/process/variables?view=vsts&tabs=yaml%2Cbatch Agents pools - Azure Pipelines | Microsoft Docs https://docs.microsoft.com/en-us/azure/devops/pipelines/agents/pools-queues?view=vsts Microsoft-hosted agents for Azure Pipelines - Azure Pipelines | Microsoft Docs https://docs.microsoft.com/en-us/azure/devops/pipelines/agents/hosted?view=vsts&tabs=yaml Deploy a build and release agent on Windows - Azure Pipelines | Microsoft Docs https://docs.microsoft.com/en-us/azure/devops/pipelines/agents/v2-windows?view=vsts Build pipeline options - Azure Pipelines | Microsoft Docs https://docs.microsoft.com/en-us/azure/devops/pipelines/build/options?view=vsts&tabs=yaml Run Git commands in a script - Azure Pipelines | Microsoft Docs https://docs.microsoft.com/en-us/azure/devops/pipelines/scripts/git-commands?view=vsts&tabs=yaml Shell Script task - Azure Pipelines | Microsoft Docs https://docs.microsoft.com/en-us/azure/devops/pipelines/tasks/utility/shell-script?view=vsts Building multiple branches - Azure Pipelines | Microsoft Docs https://docs.microsoft.com/en-us/azure/devops/pipelines/build/ci-build-git?view=vsts&tabs=yaml Predefined build variables - Azure Pipelines | Microsoft Docs https://docs.microsoft.com/en-us/azure/devops/pipelines/build/variables?view=vsts Workflow using multiple jobs in Azure Pipelines Build and Release - Azure Pipelines | Microsoft Docs https://docs.microsoft.com/en-us/azure/devops/pipelines/process/multiple-phases?tabs=yaml&view=vsts Conditional tasks - Azure Pipelines | Microsoft Docs https://docs.microsoft.com/en-us/azure/devops/pipelines/process/conditions?view=vsts&tabs=yaml -
License Google Noto Fonts http://www.google.com/get/noto/ Copyright(c) Google Inc SIL
Open Font License (https://scripts.sil.org/cms/scripts/page.php?site_id=nrsi&id=OFL )