在 GitHub Actions 中运行 pre-commit.com 的 golang 脚本。

huangapple go评论85阅读模式
英文:

run pre-commit.com script for golang in github actions

问题

我正在尝试在 GitHub Actions 中使用与 golang 相关的一些钩子来运行 pre-commit.com 脚本。似乎测试环境缺少执行 go-imports 和 golangci-lint 的一些工具。
我已经在 pre-commit 步骤之前添加了设置所需工具的步骤,但没有起作用。

.pre-commit-config.yaml:

repos:
- repo: https://github.com/dnephin/pre-commit-golang
  rev: v0.5.0
  hooks:
    - id: go-imports
    - id: golangci-lint
    - id: go-unit-tests

GitHub Action 文件配置:

name: pre-commit

on:
  pull_request:
  push:
    branches: [main]
jobs:
  pre-commit:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
    - uses: actions/setup-python@v2
    - uses: actions/setup-go@v3
    - run: go install golang.org/x/tools/cmd/goimports@latest
    - run: curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.49.0
    - uses: pre-commit/action@v2.0.2

GitHub Action 输出:
所有的 go 环境设置步骤都成功完成

pre-commit/action@v2.0.2 的详细信息:

[...]
[INFO] 这可能需要几分钟时间...
go imports...............................................................失败
- 钩子 ID: go-imports
- 退出代码: 127

/home/runner/.cache/pre-commit/repow0byklud/run-go-imports.sh: 第 8 行: goimports: 找不到命令

golangci-lint............................................................失败
- 钩子 ID: golangci-lint
- 退出代码: 127

/home/runner/.cache/pre-commit/repow0byklud/run-golangci-lint.sh: 第 2 行: exec: golangci-lint: 找不到命令

go-unit-tests............................................................通过
[...]
英文:

I'm trying to run pre-commit.com script with some hooks related to golang in github actions. Seems like the testing environment lack of some tools to execute go-imports and golangci-lint.
I've added steps for setting up required tools in the environment prior to pre-commit step, but it doesn't help.

.pre-commit-config.yaml:

repos:
- repo: https://github.com/dnephin/pre-commit-golang
  rev: v0.5.0
  hooks:
    - id: go-imports
    - id: golangci-lint
    - id: go-unit-tests

github action file config:

name: pre-commit

on:
  pull_request:
  push:
    branches: [main]
jobs:
  pre-commit:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
    - uses: actions/setup-python@v2
    - uses: actions/setup-go@v3
    - run: go install golang.org/x/tools/cmd/goimports@latest
    - run: curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.49.0
    - uses: pre-commit/action@v2.0.2

Gihub Action Output:
all go invironments set-up steps completed successfully

Details of pre-commit/action@v2.0.2:

[...]
[INFO] This may take a few minutes...
go imports...............................................................Failed
- hook id: go-imports
- exit code: 127

/home/runner/.cache/pre-commit/repow0byklud/run-go-imports.sh: line 8: goimports: command not found

golangci-lint............................................................Failed
- hook id: golangci-lint
- exit code: 127

/home/runner/.cache/pre-commit/repow0byklud/run-golangci-lint.sh: 2: exec: golangci-lint: not found

go-unit-tests............................................................Passed
[...]

答案1

得分: 2

所以,问题是在安装go工具后,/go/bin目录没有被添加到执行环境的$PATH中(因此BASH中看不到goimports和golangci-lint)。

(由于GitHub操作的特殊性,$PATH本身被包装在$GITHUB_ENV中。)

在执行pre-commit操作之前,可以使用以下语句解决该问题(完整代码见末尾):

run: echo "PATH=$PATH:/home/runner/go/bin" >> $GITHUB_ENV

感谢@Anthony Sottile在原问题的评论中提供的解决方案。

GitHub Action设置代码:

name: pre-commit

on:
  pull_request:
  push:
    branches: [main]
jobs:
  pre-commit:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
    - uses: actions/setup-python@v2
    - uses: actions/setup-go@v3
    - run: go install golang.org/x/tools/cmd/goimports@latest
    - run: curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -  -b $(go env GOPATH)/bin v1.49.0
    - run: echo "PATH=$PATH:/home/runner/go/bin" >> $GITHUB_ENV
    - uses: pre-commit/action@v2.0.2
英文:

So, the issue was that .../go/bin directory are not being added to $PATH in the execution environment after go tools installation (so goimports and golangci-lint are not visible for BASH)

($PATH is itself being wrapped in the $GITHUB_ENV due to github actions specific.)

This statement prior to pre-commit action execution can resolve the issue (see full code in the end):

run: echo "PATH=$PATH:/home/runner/go/bin" >> $GITHUB_ENV

Thanks for @Anthony Sottile in the comments to original question

Github Action settings code:

name: pre-commit

on:
  pull_request:
  push:
    branches: [main]
jobs:
  pre-commit:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
    - uses: actions/setup-python@v2
    - uses: actions/setup-go@v3
    - run: go install golang.org/x/tools/cmd/goimports@latest
    - run: curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -  -b $(go env GOPATH)/bin v1.49.0
    - run: echo "PATH=$PATH:/home/runner/go/bin" >> $GITHUB_ENV
    - uses: pre-commit/action@v2.0.2

huangapple
  • 本文由 发表于 2022年9月19日 14:46:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/73769421.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定