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

我正在尝试使用 github 操作中与 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/action@v2.0.2

Gihub 操作输出: 所有 go invironments 设置步骤已成功完成


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

[...]


千巷猫影
浏览 225回答 1
1回答

皈依舞

所以,问题是在安装 go 工具后,.../go/bin 目录没有被添加到执行环境中的 $PATH(所以 goimports 和 golangci-lint 对于 BASH 是不可见的)($PATH 本身被包装在 $GITHUB_ENV 中,因为特定于 github 操作。)在预提交操作执行之前的这个声明可以解决这个问题(最后查看完整代码):run: echo "PATH=$PATH:/home/runner/go/bin" >> $GITHUB_ENV感谢@Anthony Sottile 在对原始问题的评论中Github Action设置代码:name: pre-commiton:  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
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go