0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

GitHub Actions で Go のテスト実行

0
Last updated at Posted at 2024-06-23

CI の一環として、GitHub のデフォルトブランチに PR を作成した時と、マージをした時に UT を行う GitHub Actions を作成します。

ファイル構成

.github
  ├─ actions
  │   └─ go-ut
  |       └─ action.yml
  └─ workflows
      └─ ci.yml
json
  ├─ go.mod
  ├─ go.sum
  ├─ pretty_print.go
  └─ pretty_print_test.go
.gitignore

最終的にはリリースも GitHub Actions から行うため、UT の実行は action.yml に記載しています。
今回は、Go 言語の UT を対象にしており、json フォルダの下にコードおよび、テストコードが置かれています。

Action

.github/actions/go-ut/action.yml
name: Go UT
description: Run go unit test

inputs:
  go-directory:
    required: true
  result-file:
    required: false
    default: result.txt

runs:
  using: 'composite'
  steps:
    - uses: actions/checkout@v4

    - name: Set up Go
      uses: actions/setup-go@v4
      with:
        go-version: '1.21'
        check-latest: true
        cache-dependency-path: |
             json/go.sum

    - name: Build
      shell: bash
      run: go build -v ./...
      working-directory: ${{ inputs.go-directory }}

    - name: Test
      shell: bash
      run: go test -cover ./... > ${{ inputs.result-file }}
      working-directory: ${{ inputs.go-directory }}

    - name: Summary
      shell: bash
      run: |
        echo "### UT summary" >> $GITHUB_STEP_SUMMARY
        while read LINE; do
          echo $LINE >> $GITHUB_STEP_SUMMARY
        done < ${{ inputs.result-file }}
      working-directory: ${{ inputs.go-directory }}

引数には、テスト対象の Go モジュールのディレクトリ(go-directory)と、テスト結果の出力ファイル名(result-file)を定義しています。
また、テスト結果を GitHub Action のサマリーとして出力しています。

Workflow

.github/workflows/ci.yml
name: CI

on:
  push:
    branches: [ "main" ]
  pull_request:
    branches: [ "main" ]

jobs:
  UT:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v3

      - name: Go UT
        uses: ./.github/actions/go-ut
        with:
          go-directory: ./json

実行結果

main ブランチに PR を作成すると UT が実行されます。
スクリーンショット 2024-06-23 19.25.10.png

Details から Action を確認すると Summary にテスト結果が表示されます。
スクリーンショット 2024-06-23 19.13.40.png

依存関係をキャッシュ(2025/04/29 追記)

参考:https://docs.github.com/ja/actions/writing-workflows/choosing-what-your-workflow-does/caching-dependencies-to-speed-up-workflows

GitHub Action に下記の3行を追加し、Go の依存ライブラリのキャッシュを行います。

      uses: actions/setup-go@v4
       with:
         go-version: '1.21'
+        check-latest: true
+        cache-dependency-path: |
+             json/go.sum

キャッシュは go.sum のハッシュをキャッシュキーの一部として使用しており、go.sum が更新されればキャッシュミスと判断され、新しい依存関係がダウンロードされます。

GitHubは、7日間以上アクセスされていないキャッシュを削除します。また、格納できるキャッシュの数に制限はありませんが、リポジトリ内のすべてのキャッシュの合計サイズは制限されています (最大 10 GB)。 リポジトリがその最大サイズに達すると、リポジトリ内の最も古いキャッシュが削除されます。

キャッシュは、Actions ⇒ Caches から確認できます。
image.png

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?