[英]run pre-commit.com script for golang in github actions
I'm trying to run pre-commit.com script with some hooks related to golang in github actions.我正在尝试运行 pre-commit.com 脚本,其中包含 github 操作中与 golang 相关的一些钩子。 Seems like the testing environment lack of some tools to execute go-imports and golangci-lint.
似乎测试环境缺少一些工具来执行 go-imports 和 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: .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: github 动作文件配置:
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 Gihub Action Output:所有 go 环境设置步骤已成功完成
Details of pre-commit/action@v2.0.2: 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
[...]
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)所以,问题是在 go 工具安装后,.../go/bin 目录没有被添加到执行环境中的 $PATH (所以 goimports 和 golangci-lint 对于 BASH 不可见)
($PATH is itself being wrapped in the $GITHUB_ENV due to github actions specific.) (由于 github 操作特定,$PATH 本身被包装在 $GITHUB_ENV 中。)
This statement prior to pre-commit action execution can resolve the issue (see full code in the end):在 pre-commit action 执行之前的这个语句可以解决这个问题(见最后的完整代码):
run: echo "PATH=$PATH:/home/runner/go/bin" >> $GITHUB_ENV
Thanks for @Anthony Sottile in the comments to original question感谢@Anthony Sottile 在对原始问题的评论中
Github Action settings code: Github 动作设置代码:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.