简体   繁体   English

在 github 动作中为 golang 运行 pre-commit.com 脚本

[英]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.

相关问题 Golang Pre commit hook 抛出错误 https://github.com/dnephin/pre-commit-golang - Golang Pre commit hook throwing error https://github.com/dnephin/pre-commit-golang 使用github.com/julienschmidt/httprouter在Golang中使用参数时提供静态CSS和Java脚本 - Serving static css and java script while using parameters in Golang using github.com/julienschmidt/httprouter GitHub 操作:在 wihdows 上运行 golint - GitHub Actions: Run golint on wihdows 在golang中实现github.com/jlaffaye/ftp - implement github.com/jlaffaye/ftp in golang 如何运行从github下载的golang项目 - how to run golang project downloaded from github 使用 golang (github.com/cosmtrek/air) 在 docker 中热重载不起作用 - Hot Reload not working in docker with golang (github.com/cosmtrek/air) golang Google oauth2-无法获取用户信息(库:https://github.com/golang/oauth2) - golang google oauth2 - not able to get user info (library : https://github.com/golang/oauth2) 在 golang 程序中运行交互式 shell 脚本 - Run interactive shell script in golang program 运行 golang repo 的预提交挂钩时出错 [命名文件必须是 .go 文件:./...] - getting error when running pre-commit hook for golang repo [named files must be .go files: ./...] 如何在Golang中运行外部Python脚本? - How to run external Python script in Golang?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM