.NET MAUI Advent Calendar 2025 2日目の記事です。
概要
本記事は、タイトル通り GitHub Actions を用いて、.NET MAUI の自動ビルドをすることを目的としています。
Android と iOS の署名は含んでいません。署名の方法については、記事最後の参考資料を参照してください。
GitHub Actions で自動ビルド
1. 実行条件の設定
まずはじめに、GitHub Actions が実行される条件を決めます。
name: .NET MAUI Build
on:
push:
branches:
- main
pull_request:
branches:
- main
workflow_dispatch:
今回は、main ブランチに push または PR されたとき、または手動で実行したとき (workflow_dispatch) に実行するように設定しました。
2. 環境設定
続いて、ビルド環境の準備を行います。
jobs:
build:
runs-on: ${{ (contains(matrix.tfm, 'maccatalyst') && 'macos-latest') || 'windows-latest' }}
strategy:
matrix:
tfm: [net10.0-android, net10.0-ios, net10.0-maccatalyst, net10.0-windows10.0.19041.0]
fail-fast: false
macOS アプリケーションは、macOS 上でしかビルドすることができないので、macos-latest を利用します。それ以外は、windows-latest でビルドできます。(Android 向けのビルドの際に ubuntu-latest を用いることでコスト削減できます!)
matrix を用いて、4種類のビルドを並列して処理します。
steps:
- name: Checkout
uses: actions/checkout@v6
チェックアウトです。
- name: Setup .NET
uses: actions/setup-dotnet@v5
with:
dotnet-version: 10.0.x
.NET を準備します。バージョンは現在最新バージョンの .NET 10 です。
- if: matrix.tfm == 'net10.0-maccatalyst'
name: Setup Xcode latest
uses: maxim-lobanov/setup-xcode@v1
with:
xcode-version: '26.1.1'
現在、macOS では XCode 26.1 以上が必要なのですが、macos-latest では XCode 16.4 が標準で選択されてしまい、以下のエラーが出ます:
/Users/runner/.dotnet/packs/Microsoft.MacCatalyst.Sdk.net10.0_26.1/26.1.10494/targets/Xamarin.Shared.Sdk.targets(2371,3): error : This version of .NET for MacCatalyst (26.1.10494) requires Xcode 26.1. The current version of Xcode is 16.4. Either install Xcode 26.1, or use a different version of .NET for MacCatalyst. See https://aka.ms/xcode-requirement for more information. [/Users/runner/work//_/src/_/_.csproj::TargetFramework=net10.0-maccatalyst]
このエラーを回避するため、明示的に 26.1.1 の使用を指定しています。
- name: Install MAUI Workloads
run: |
dotnet workload install android --ignore-failed-sources
dotnet workload install maui --ignore-failed-sources
最後に、workload のインストールを追加します。maui-android などの maui-* 系のすべてが必要ですが、maui に含まれているので大丈夫です。
これで、環境設定は終了です。
3. ビルド
ビルドです。
- name: Restore Dependencies
run: dotnet restore path/to/YourProject/A.csproj
- name: Build MAUI App
run: dotnet build path/to/YourProject/A.csproj -c Release -f ${{ matrix.tfm }} --no-restore
依存関係の修復を行い、-c Release でリリース、-f ${{ matrix.tfm }} でビルドターゲットを指定してビルドするだけです。
path/to/YourProject/A.csproj のところは、自分のプロジェクトのディレクトリに変更してください。
基本的には、これで終了です。
4. 完成品をアップロード
ビルド結果をダウンロードできるようにします。(任意)
- if: matrix.tfm == 'net10.0-android'
name: Android Artifact
uses: actions/upload-artifact@v5
with:
name: android-ci-build
path: path/to/YourProject/bin/Release/net10.0-android/*Signed.a*
- if: matrix.tfm == 'net10.0-ios'
name: iOS Artifact
uses: actions/upload-artifact@v5
with:
name: ios-ci-build
path: path/to/YourProject/bin/Release/net10.0-ios/
- if: matrix.tfm == 'net10.0-maccatalyst'
name: MacCatalyst Artifact
uses: actions/upload-artifact@v5
with:
name: macos-ci-build
path: path/to/YourProject/bin/Release/net10.0-maccatalyst/**/*.app/
- if: matrix.tfm == 'net10.0-windows10.0.19041.0'
name: Windows Artifact
uses: actions/upload-artifact@v5
with:
name: windows-ci-build
path: path/to/YourProject/bin/Release/net10.0-windows*/**/
長く見えますが、ターゲットによってアップロード時の名前やターゲットディレクトリを変えているだけです。
CI/CD が実行されたあと、Artifacts として GitHub に保存されます。ビルド結果をダウンロードできます。
以上です。
感想
1度設定しておくと、自動で動いてくれるので便利です!
.NET のバージョン (net10.0) や、最後のアップロード部分などは環境変数 (env:) としてまとめたほうが処理しやすいかもしれないです。
参考記事
- .NET Blog - Getting Started with DevOps and .NET MAUI
- https://maxmannstein.com/index.php/2024/10/20/publish-your-maui-app-with-github-actions/
- https://medium.com/@oscar.jargren/building-net-maui-android-apps-on-linux-with-azure-devops-20e8aa766cf2 (Azure DevOps 向け)
- https://blog.jbs.co.jp/entry/2022/11/08/090000 (Android、iOS 署名/日本語)
- https://zenn.dev/gomita/scraps/d637d0feee536f (iOS 署名/日本語)
- https://thewissen.io/making-maui-cd-pipeline/ (iOS 署名)