1
2

ローカルとGitHub Actionsでのgolangci-lint 設定方法

Posted at

はじめに

Go言語のプロジェクトにおいて、一貫したコード品質を維持することは重要です。この記事では、ローカル環境とGitHub ActionsのCI/CDパイプラインで**golangci-lint**を統一的に使用する方法について解説します。これにより、コードレビューの効率化と品質向上を図ることができます。

golangci-lintとは

golangci-lintは、Go言語のための強力なlintツールです。複数のlinterを統合し、カスタマイズ可能な設定が可能です。(Introduction | golangci-lint

ローカル環境での設定

  1. golangci-lint をインストールします
    • go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
  2. リポジトリのルートに.golangci.ymlを作成します。

.golangci.yml 設定例

linters-settings:
  govet:
    check-shadowing: true
  golint:
    min-confidence: 0.8
linters:
  enable:
    - govet
    - golint
    - unused
    - gocritic
    - stylecheck
    - errcheck

GitHub Actionsでの設定例

GitHub Actionsを使用して、プルリクエスト時に自動的にlintを実行します。.github/workflows/lint.ymlを作成し、以下の内容を記述します。

.github/workflows/lint.yml

name: lint

on:
  pull_request:
    branches:
      - main

jobs:
  lint:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v3

      - name: Set up Go
        uses: actions/setup-go@v3
        with:
          go-version: 1.21

      - name: Run golangci-lint
        uses: golangci/golangci-lint-action@v2
        with:
          version: latest
          args: ./...

おわりに

golangci-lintをローカルとGitHub Actionsで統一的に使用することで、Goプロジェクトのコード品質を一貫して高く保つことができます。この設定を通じて、開発者はより効率的にコードを書き、レビューすることができるようになります。

1
2
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
1
2