0
0

CloudBuildでgolangci-lintを実行する

Posted at

結論

cloudbuild.yamlに以下を追加すれば、実装できます

cloudbuild.yaml
steps:
  # golangci-lintのDockerイメージを指定する
  - name: 'golangci/golangci-lint'
    # デフォルトの設定でgolangci-lintを実行する
    args: ['golangci-lint', 'run']

golangci-lint1とは

golangci-lint is a fast Go linters runner.
It runs linters in parallel, uses caching, supports YAML configuration, integrates with all major IDEs, and includes over a hundred linters.

golangci-lintは高速なGoリンターランナーです。
並列にリンターを実行し、キャッシュを使い、YAML設定をサポートし、すべての主要なIDEと統合し、100以上のリンターを含みます。

Goの静的解析ツールは本当にたくさん存在するのですが、それらをひとまとめにして実行してくれるツールです。 GoのプロジェクトをGitHub ActionsのようなCIでテストする際にはよく使われます。2

golangci-lintの設定

デフォルトでは、以下のlinterが実行されます3

Name Description Presets
errcheck Errcheck is a program for checking for unchecked errors in Go code. These unchecked errors can be critical bugs in some cases. bugs, error
gosimple Linter for Go source code that specializes in simplifying code. style
govet Vet examines Go source code and reports suspicious constructs. It is roughly the same as go vet and uses its passes. bugs, metalinter
ineffassign Detects when assignments to existing variables are not used. unused
staticcheck It's a set of rules from staticcheck. It's not the same thing as the staticcheck binary. The author of staticcheck doesn't support or approve the use of staticcheck as a library inside golangci-lint. bugs, metalinter
unused Checks Go code for unused constants, variables, functions and types. unused

.golangci.ymlなどの設定ファイルを追加することで、linterの有効/無効の設定ができます4

  1. https://github.com/golangci/golangci-lint

  2. mattn. Go言語プログラミングエッセンス エンジニア選書 (p.133). 株式会社技術評論社. Kindle 版.

  3. https://golangci-lint.run/usage/linters/

  4. https://golangci-lint.run/usage/configuration/

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