at backyard

Color my life with the chaos of trouble.

RustのプロジェクトでGitHub Actionsを使う

RustのリポジトリGItHub Actionsを使うための yml ファイルを下記にメモする。

name: Rust CI

on: [push, pull_request]

jobs:
  check:
    name: Check
    runs-on: ubuntu-latest
    steps:
      - name: Checkout sources
        uses: actions/checkout@v2

      - name: Install stable toolchain
        uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: stable
          override: true

      - name: Run cargo check
        uses: actions-rs/cargo@v1
        with:
          command: check

  test:
    name: Tests
    runs-on: ubuntu-latest
    steps:
      - name: Checkout sources
        uses: actions/checkout@v2

      - name: Install stable toolchain
        uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: stable
          override: true

      - name: Run cargo test
        uses: actions-rs/cargo@v1
        with:
          command: test

  lints:
    name: Lints
    runs-on: ubuntu-latest
    steps:
      - name: Checkout sources
        uses: actions/checkout@v2

      - name: Install stable toolchain
        uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: stable
          override: true
          components: rustfmt, clippy

      - name: Run cargo fmt
        uses: actions-rs/cargo@v1
        with:
          command: fmt
          args: --all -- --check

      - name: Run cargo clippy
        uses: actions-rs/cargo@v1
        with:
          command: clippy
          args: -- -D warnings

上記の設定を行うことで、

  • cargo check を用いたチェック
  • cargo fmt を用いたチェック
  • cargo clippy を用いたチェック
  • cargo test を用いたテスト

が行われる。

まだRustのことはあまり良くわかっていないのだが、 actions-rs というRustのための非公式だが広く採用されている(らしい)GItHub Actionsツールキットを用いている。

actions-rs.github.io

なお、上に書いた yml はこの actions-rsのexampleリポジトリにあったサンプルを大いに参考にさせてもらっている。
(というか、ほぼ一緒)

ひとまずこの設定で最低限のチェックはできそうなので備忘録として残しておく。

なお、実際に動かして見るためにかんたんなFizz Buzzのリポジトリを作成している。

github.com

なんていうことのないFizz Buzzである。

ちなみに cargo clippy を使ったら最初に書いていた書き方だと警告が結構出ていたので、記述を途中で修正した。
こういう書き方のほうが良いのか、とRustならではの作法(?)を教えてもらえるのは非常に助かる。