4

GitHub Actions 配置 golangci-lint

 2 years ago
source link: https://blog.jiahonzheng.com/post/github-actions-golangci-lint/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

GitHub Actions 配置 golangci-lint

· 2022-01-09 · # Golang

代码规范能够让项目代码风格一致,并发现潜在的缺陷,提高研发效率。本篇博客将介绍 Golang 代码检查工具 golangci-lint ,及其在 GitHub Actions 上的配置。

golangci-lint

golangci-lint 是一款快速、高效的代码检查工具,支持自定义的检查规则,被广泛应用在各大开源项目中,如 KubernetesBeego 等。

demo.svg

Linter

Linter 是 golangci-lint 中很重要的一个概念,具体的代码检查规则是由 Linter 去实现的。通过执行 golangci-lint linters 命令,我们可以查看所有可支持的 Linter ,以及它们的启用状态。此外,官方也提供了 Linter 的支持列表及其说明。

202201091717210.png

默认情况下,golangci-lint 会去识别根目录下的 .golangci.yml 文件,从中载入 Linter 的各项配置,以下是我常用的 Linter 配置:

linters:
  disable-all: true
  enable:
    - deadcode
    - dupl
    - errcheck
    - godot
    - goimports
    - gosimple
    - govet
    - ineffassign
    - misspell
    - revive
    - staticcheck
    - structcheck
    - typecheck
    - unused
    - varcheck

GitHub Actions

GitHub Actions 是 GitHub 于 2018 年推出的持续集成服务,其定义了以下四种术语:

  • workflow:指一次持续集成的过程。
  • job:一个 workflow 可以完成一个或多个 job 任务。
  • step:一个 job 由一个或多个 step 步骤构成。
  • action:一个 step 可以依次执行一个或多个 action 命令。

配置 Workflow

GitHub Actions 的配置文件,存在代码仓库的 .github/workflows 目录,采用 YAML 格式,后缀名为 .yml 。在该配置文件中,我们可以直接使用其他开发人员在 Actions 市场 上发布的 Action,这能够大大降低我们开发 Actions 的成本。golangci 团队在市场上提供了名为 golangci-lint-action@v2 的 Action ,我们可以直接使用它!

复制以下内容至 .github/workflows/golangci-lint.yml 文件,即可让我们的持续集成接入 golangci-lint 代码检查工具。

name: golangci-lint

on: [push, pull_request]

jobs:
  golangci:
    name: lint
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: golangci-lint
        uses: golangci/golangci-lint-action@v2
        with:
          version: latest

在上述配置中,我们定义在 pushpull_request 事件发生时,就触发 golangci-lint 的持续集成。更多 CI 的触发事件,可查看官方文档


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK