GitHub ActionsでRustを複数環境でビルドする
はじめに
この記事では、「GitHub ActionsでRustを複数環境でビルドする方法」について書いています。
RustプロジェクトをGitHub Actionsを使って良い感じにCI/CD出来るようにしたいと考え、色々試行錯誤した中でのこと(その中で今回はRustを複数環境(例えばWindows用とLinux用で、等)でビルドする)を記事にしました。
自分に向けた備忘録でもありますが、「GitHub ActionsでRustを複数環境(LinuxやWindowsなど)でビルドしたい」な人の一助になれば嬉しいです。
本題
早速ですが、Rustを複数環境(LinuxやWindowsなど)でビルドするワークフロー(yml)は以下の通りです。
name: multi-target_build on: workflow_dispatch: env: PROJECT_NAME: proj_name # ここにはRustプロジェクトの名前を入れてください jobs: multi-target_build: strategy: matrix: target: - x86_64-unknown-linux-musl - x86_64-pc-windows-msvc - x86_64-apple-darwin include: - target: x86_64-unknown-linux-musl os: ubuntu-latest - target: x86_64-pc-windows-msvc os: windows-latest - target: x86_64-apple-darwin os: macos-latest runs-on: ${{ matrix.os }} steps: - name: Checkout code uses: actions/checkout@v2 - name: Install musl tools if : matrix.target == 'x86_64-unknown-linux-musl' run: | sudo apt install -qq -y musl-tools --no-install-recommends - name: Setup Rust toolchain uses: actions-rs/toolchain@v1 with: profile: minimal toolchain: stable target: ${{ matrix.target }} override: true - name: Build uses: actions-rs/cargo@v1 with: use-cross: true command: build args: --release --target=${{ matrix.target }} - name: Check release Directory for Linux if: matrix.target == 'x86_64-unknown-linux-musl' run: | ls target/${{ matrix.target }}/release/ - name: Check release Directory for windows if: matrix.target == 'x86_64-pc-windows-msvc' run: | dir target/${{ matrix.target }}/release/ - name: Check release Directory for macOS if: matrix.target == 'x86_64-apple-darwin' run: | ls target/${{ matrix.target }}/release/
上のコードは、手動実行をトリガーとし、Linux、Windows、Mac-OSの3種類の環境でビルドし実行ファイルを作成しています。
実際に上の内容でymlファイルを作成してアクションを実行し、実行結果のname: Check release Directory for ...
の項を開くと、releaseフォルダに実行ファイルがあることが確認できると思います。
matrix:
以下の部分でビルドのターゲットとビルド環境と指定しており、runs-on: ${{ matrix.os }}
で、matrix
で指定したビルド環境で処理が実行されます。複数指定されていれば複数の環境で処理を実行してくれます。
なお、一番下に3つあるname: Check release Directory for ...
の処理は、実行ファイルが生成されているか確認するための処理で、今回ビルドが上手くいっていること確認したくて加えただけで、この部分の記述がなくてもビルドに問題はありません。
上の内容でymlファイルを作成し、アクションを実行すると、
おわりに
「GitHub ActionsでRustを複数環境でビルドする方法」について解説しました。
自動で複数の環境向けに自動ビルドできる方法を知っておくと、リリース作業などで更に効率化することが出来るかもしれません
さいごに、記事を書く上で参考にしたサイトのリンクを以下に掲載します。合わせて読んでいただくと良いかと思います。
https://dev.classmethod.jp/articles/publish-crossbuilt-binary-with-githubactions/