1

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
[...]
lgl1
  • 31
  • 4
  • what happens if you run `goimports --help` outside of `pre-commit`? I suspect you haven't set up `PATH` properly and this isn't a `pre-commit` problem – anthony sottile Sep 19 '22 at 12:19
  • @AnthonySottile, well, indeed it's not a pre-commit issue, since on local machine it wokrs fine. The problem appears when it started on Github Actions execution environment. Do you suggest to set-up somehow path for execution environment? Anyway, goimports --help fails with the same error after being added to the action step chain (returning "goimports: command not found") – lgl1 Sep 19 '22 at 14:59
  • I'd then try and figure out why the `PATH` isn't set properly -- that should be pretty easy to search and there's an intentional and supported way to make that happen in github actions. if you can't find the answer on your own feel free to comment here again but it should be relatively straightforward – anthony sottile Sep 19 '22 at 16:45
  • @AnthonySottile, thanks for all hints. This statement finally helped: run: echo "PATH=$PATH:/home/runner/go/bin" >> $GITHUB_ENV – lgl1 Sep 22 '22 at 08:40
  • there's one better -- `GITHUB_PATH` – anthony sottile Sep 22 '22 at 12:16

1 Answers1

2

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
lgl1
  • 31
  • 4