简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM