LoginSignup
0
0

More than 3 years have passed since last update.

GitHub Actionsについて何もわからないやつがなんちゃってCIを設定するまで(Spring Boot)

Last updated at Posted at 2021-04-10

はじめに

久しぶりに書くので短めに。
GitHub Actionsが出て1年以上になるが、周りがすごいすごいという割に自分で使ったことがなかったので触ってみた。

環境

  • Java 11
  • IntelliJ IDEA 2020.3.2 (Community Edition)
    • Build #IC-203.7148.57, built on January 26, 2021

設定

以降の内容は下記リポジトリに置いた。
https://github.com/Ikeponias/SpringBootCI

以下の実装とテストコードを書いて、これをCIで実行するようにしてみた。実装とテストは適当なのでご容赦ください。

TestController.java
@RestController
@RequestMapping("/test")
public class TestController {

    @RequestMapping(method = RequestMethod.GET)
    public String getMessage() {
        return "Hello World!";
    }
}
TestControllerTest.java
    @Test
    public void successTest() throws Exception {
        MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.get("/test"))
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andReturn();
        Assertions.assertEquals(mvcResult.getResponse().getContentAsString(), "Hello World!");
    }

    @Test
    public void failedTest() throws Exception {
        MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.get("/test"))
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andReturn();
        Assertions.assertEquals(mvcResult.getResponse().getContentAsString(), "Hello World");
    }

GitHubリポジトリのタブ中にActionsという欄がいつの間にやらできている。
無題.png
これを押してみるとGitHub Actionsの初期設定画面が出てくる。
無題.png
ふむふむ、デプロイするためのワークフローサンプルもあるのは良い。
今回は本当に簡単にテストしたいだけなので、Suggestされている Simple workflow を選択。
すると以下のような画面が表示された。
なるほど、このymlを編集することでCIの設定ができるわけか。
JenkinsにおけるJenkinsfileと思っておけば良いだろうか。
無題.png
今回はpush時にテストを実行してほしかったので、プルリクエスト時の設定を削除し、Run tests として mvn test を追加してみた

# This is a basic workflow to help you get started with Actions

name: CI

# Controls when the action will run. 
on: [push]

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  # This workflow contains a single job called "build"
  build:
    # The type of runner that the job will run on
    runs-on: ubuntu-latest

    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
      # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
      - uses: actions/checkout@v2

      # Runs a single command using the runners shell
      - name: Run a one-line script
        run: echo Hello, world!

      # Runs a set of commands using the runners shell
      - name: Run a multi-line script
        run: |
          echo Add other actions to build,
          echo test, and deploy your project.

      - name: Run tests
        run:
          mvn test

いきなり出てきた actions/checkout@v2 ってナンダコレと思ったが https://github.com/actions/checkout のことのようだ。
具体的な構文や設定方法は公式のドキュメントを参照するのが良い。
特に今回はJavaのバージョンなど設定していないが、その場合pomの値が使われるのだろうか(わからん)。
https://docs.github.com/ja/actions

実行結果

テストがすべて実行成功すると下記のようになることが確認できた。
無題.png
テストが失敗すると下記のようになることが確認できた。
無題.png
ついでにGitHubに登録してあるメールアドレスにジョブ結果が届いていることも確認できた。
無題.png

感想

初めてでも20分くらいで設定できたので、Jenkinsなどと比べると導入コストが非常に低い。
簡単にCIを回したいとかであればこれは非常に楽でありがたいと思う。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0