GitHub Actionsを使ってRustのCI環境を構築してみた

はじめに

GitHub Actionsを使ってRustのCI環境を構築してみたので、 その内容を、自分への備忘録も兼ねてその内容ををまとめた記事です。

環境

リポジトリ

primality_test(大元のリポジトリ)
  ├ .github
  │   └ workflows
  │        └ rust_ci.yml <- GitHub Actionsで実施する内容を書く
  ├ src
  │   └ main.rs
  ├ .gitignore
  ├ Cargo.toml
  └ README.md

コード

まずはrust_ci.ymlの中身です。 とは言っても、actions-rsのexampleリポジトリにあったサンプルそのままです。

github.com

ちなみに、actions-rsとは非公式ではありますがRust向けのアクション集です。

name: Rust Continuous-Integration

on: [push, pull_request]

jobs:
  check:
    name: Check
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: stable
          override: true
      - uses: actions-rs/cargo@v1
        with:
          command: check

  test:
    name: Test Suite
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: stable
          override: true
      - uses: actions-rs/cargo@v1
        with:
          command: test

  fmt:
    name: Rustfmt
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: stable
          override: true
      - run: rustup component add rustfmt
      - uses: actions-rs/cargo@v1
        with:
          command: fmt
          args: --all -- --check

  clippy:
    name: Clippy
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: stable
          override: true
      - run: rustup component add clippy
      - uses: actions-rs/cargo@v1
        with:
          command: clippy
          args: -- -D warnings

さて、続いてはRustのコードですが、サンプルなので素数判定をテストコード付きで書いているだけです。 ちなみに、GitHub Acitonでどう反応するか見たいため、敢えてアクション実行時に失敗するように書いています。

fn is_prime(num: i32) -> bool {
    if num <= 1 {
        return false;
    }

    if num == 2 {
        return true;
    }

    if num % 2 == 0 {
        return false;
    }
    for i in 3..=(num as f32).sqrt() as i32 {
        if num % i == 0 {
            return false;
        }
    }
    return true;
}

fn main() {
    let num = 100;
    println!("{}, is prime? -> {}", num, is_prime(num));
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn test_is_prime() {
        let expects = [
            false, false, true, true, false, true, false, true, false, false,
        ];
        for i in 0..expects.len() {
            assert_eq!(expects[i], is_prime(i as i32));
        }
    }
}

これで準備が完了です。これでGitHubにpushすると、Actionが実行されます。

GitHubのActionのタブを覗くと、以下のような画面になっているかと思います。 ビルドやテストは成功してますが、最後の「Clippy」で失敗になっております。 これは上で書いた「敢えてアクション実行時に失敗するように書いた」という部分で、returnの記述が不要なのに書かれているということで(これがあることでビルドが通らないわけではないですが)失敗しており、意図通りの結果になっています。

さいごに

  • GitHub Actionsを使ってRustのCI環境を構築してみました。
  • 自動でビルドからテスト、ワーニングのチェックまでやってくれるのはとても快適ですね
  • CIの方法をより勉強して、どんどん快適で高速な開発ができる手段を身につけていきたいです。