欢迎各位小伙伴来到编程网,相聚于此都是缘哈哈哈!今天我给大家带来《在 github actions 中运行 golang 的 pre-commit.com 脚本》,这篇文章主要讲到等等知识,如果你对Golang相关的知识非常感兴趣或者正在自学,都可以关注我,我会持续更新相关文章!当然,有什么建议也欢迎在评论留言提出!一起学习!
我正在尝试使用 github actions 中与 golang 相关的一些钩子来运行 pre-commit.com 脚本。似乎测试环境缺少一些执行 go-imports 和 golangci-lint 的工具。 我已经在预提交步骤之前添加了在环境中设置所需工具的步骤,但这没有帮助。
.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操作文件配置:
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/[email protected]
gihub 操作输出: 所有 go 环境设置步骤均已成功完成
预提交/[电子邮件受保护]的详细信息:
[...]
[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
[...]
正确答案
所以,问题是在安装 go 工具后,.../go/bin 目录没有被添加到执行环境中的 $path 中(因此 goimports 和 golangci-lint 对于 bash 不可见)
(由于 github 操作特定,$path 本身被包装在 $github_env 中。)
在执行预提交操作之前执行此语句可以解决问题(请参阅最后的完整代码):
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/[email protected]
文中关于的知识介绍,希望对你的学习有所帮助!若是受益匪浅,那就动动鼠标收藏这篇《在 github actions 中运行 golang 的 pre-commit.com 脚本》文章吧,也可关注编程网公众号了解相关技术文章。