....
jobs:
setup:
name: Setup
runs-on: [self-hosted, Linux, X64]
steps:
- name: Set up Go 1.17
uses: actions/setup-go#v2
with:
go-version: 1.17
id: Go
- name: work around permission issue
run: git config --global --add safe.directory /__w/****-***/****-***
- name: Checkout code into go module directory
uses: actions/checkout#v2
- name: Make Directory
run: mkdir build
- name: Build
run: |
CGO_ENABLED=0 go build -ldflags "-linkmode external" -o main
- name: Upload Build
uses: actions/upload-artifact#v2
with:
name: binary
path: main
scans :
needs: setup
name: all scans
runs-on: [self-hosted, Linux, X64]
steps:
- name: Download build for build and test
uses: actions/download-artifact#v2
with:
name: build
- name: Coverity Scan
env:
AUTH_DATA: ${{ secrets.COVERITY_KEY_FILE }}
run: |
export PATH=$PATH:/opt/coverity/coverity-base/bin
mkdir coverity
cov-build --dir coverity go build main.go
cov-analyze --dir coverity --strip-path=`pwd`
touch auth_key_file
chmod 600 auth_key_file
echo $( printf "%s" "$AUTH_DATA" ) > auth_key_file
cov-commit-defects --dir coverity --url `suspicious_url` --auth-key-file auth_key_file --stream ****-overrides
- name: Upload Coverity
uses: actions/upload-artifact#v2
with:
name: coverity
path: coverity
I have this workflow file for my Golang project. I want to run the Coverity scan. But when I am running this it's throwing an error :
I have set up go in setup step. I don't know why it is telling that it did not found the go command. Please help me with this.
Go may not be in the path of the process executing that command. You may need to modify the $PATH variable to include the path to your go binary.
Looks like you're only updating the $PATH in scans, but this ought to be in setup I think:
export PATH=$PATH:/opt/coverity/coverity-base/bin
Related
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
I use goreleaser to build in github action.
Because my main.go in ./cmd/tes_cli, it always show error in github action.
repo does not contain a main function.
I check the original document, it seems "builds" could works. my configuration could not add builds
name: Release Go project
on:
push:
tags:
- "*" # triggers only if push new tag version, like `0.8.4` or else
jobs:
build:
name: GoReleaser build
runs-on: ubuntu-latest
steps:
- name: Check out code into the Go module directory
uses: actions/checkout#v2
with:
fetch-depth: 0 # See: https://goreleaser.com/ci/actions/
- name: Set up Go 1.14
uses: actions/setup-go#v2
with:
go-version: 1.14
id: go
- name: Run GoReleaser
uses: goreleaser/goreleaser-action#master
with:
version: latest
args: release --rm-dist
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Add an extra configuration file to resolve it.
Refer https://github.com/kkdai/disqus-importor-go/pull/4/files
args: release -f .goreleaser.yml --rm-dist
link to another config file.
# .goreleaser.yml
project_name: import_disqus_cli
builds:
- env: [CGO_ENABLED=0]
goos:
- linux
- windows
- darwin
goarch:
- amd64
- arm64
id: "import_disqus_cli"
dir: .
main: ./cmd/test_cli
Anyone able to get Go Release Binary GitHub Action working? which is supposed to
Automate publishing Go build artifacts for GitHub releases through GitHub Actions
The readme looks rather simple, but I've tried all my imaginations to get it working but not avail. Similar questions has been asked in its issue tracks but got no answer.
Somebody help please.
BTW, while searching for the answer, I came upon this commit logs, which is quite interesting/amusing to read. I.e., it seems to be quite a battle to get it working, but the author gave up eventually (no any releases from his/her latest commits/tags)
Conclusion:
Turns out that my project does not have go mod and there were issues in Go Release which stops it from working. It was then fixed by this and this.
I actually wrote my own release workflow for generating Go binaries.
The only non-obvious steps from my point of view are:
I have a release note generation step where I include a list of non-merge commits since the last release tag.
I use a matrix build for GOOS and GOARCH pairs and do some Bash string manipulation in the "Get OS and arch info" step.
The nice thing about softprops/action-gh-release is that you can keep adding artifacts to the same release as long as the workflow run is triggered by a push to the same tag.
on:
push:
# Sequence of patterns matched against refs/tags
tags:
- 'v*' # Push events to matching v*, i.e. v1.0, v20.15.10
name: Latest Release
defaults:
run:
shell: bash
jobs:
lint:
name: Lint files
runs-on: 'ubuntu-latest'
steps:
- uses: actions/checkout#v2
- uses: actions/setup-go#v2
with:
go-version: '1.16.3'
- name: golangci-lint
uses: golangci/golangci-lint-action#v2.5.2
with:
version: latest
test:
name: Run tests
runs-on: 'ubuntu-latest'
needs: lint
steps:
- uses: actions/checkout#v2
- uses: actions/setup-go#v2
with:
go-version: '1.16.3'
- run: go test -v -cover
release:
name: Create Release
runs-on: 'ubuntu-latest'
needs: test
strategy:
matrix:
# List of GOOS and GOARCH pairs from `go tool dist list`
goosarch:
- 'aix/ppc64'
# etc
steps:
- name: Checkout code
uses: actions/checkout#v2
with:
fetch-depth: 0
- uses: actions/setup-go#v2
with:
go-version: '1.16.3'
- name: Get OS and arch info
run: |
GOOSARCH=${{matrix.goosarch}}
GOOS=${GOOSARCH%/*}
GOARCH=${GOOSARCH#*/}
BINARY_NAME=${{github.repository}}-$GOOS-$GOARCH
echo "BINARY_NAME=$BINARY_NAME" >> $GITHUB_ENV
echo "GOOS=$GOOS" >> $GITHUB_ENV
echo "GOARCH=$GOARCH" >> $GITHUB_ENV
- name: Build
run: |
go build -o "$BINARY_NAME" -v
- name: Release Notes
run:
git log $(git describe HEAD~ --tags --abbrev=0)..HEAD --pretty='format:* %h %s%n * %an <%ae>' --no-merges >> ".github/RELEASE-TEMPLATE.md"
- name: Release with Notes
uses: softprops/action-gh-release#v1
with:
body_path: ".github/RELEASE-TEMPLATE.md"
draft: true
files: ${{env.BINARY_NAME}}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
I am trying to automatically build and test my go code on pushes to the master branch of my GitHub repository using GitHub actions.
The basic configuration to do so works perfectly fine:
name: Build & Test
on:
push:
branches:
- master
jobs:
test:
## We want to define a strategy for our job
strategy:
## this will contain a matrix of all of the combinations
## we wish to test again:
matrix:
go-version: [1.14.x] #[1.12.x, 1.13.x, 1.14.x]
platform: [ubuntu-latest] #[ubuntu-latest, macos-latest, windows-latest]
## Defines the platform for each test run
runs-on: ${{ matrix.platform }}
## the steps that will be run through for each version and platform
## combination
steps:
## sets up go based on the version
- name: Install Go
uses: actions/setup-go#v2
with:
go-version: ${{ matrix.go-version }}
env:
GOPATH: /home/runner/work/project
GO111MODULE: "on"
## runs a build
- name: Build
run: go build src/
env:
GOPATH: /home/runner/work/project
GO111MODULE: "on"
## runs go test ./...
- name: Test
run: go test ./...
env:
GOPATH: /home/runner/work/project
GO111MODULE: "on"
However, the repository has another (private) repository as direct dependency, so I need to clone this as well after checking out the main repository.
- name: Check out dependency
uses: actions/checkout#v2
with:
repository: mydependency
token: ${{ secrets.GIT_ACCESS_TOKEN }}
path: src/dependency
Then it will not find the dependency as it is not accessible from GOROOT.
src/main.go:20:2: package dependency is not in GOROOT (/opt/hostedtoolcache/go/1.14.15/x64/src/dependency)
The issue here is that the path is not accessible within the GitHub action, so I cannot clone the repository to this specific path.
I of course can specify the GOROOT as env variable for setup-go, build and test in such a way that the dependency is found, but then it will not find native go packages anymore.
env:
GOPATH: /home/runner/work/project/go
GOROOT: /home/runner/work/project
GO111MODULE: "on"
package bufio is not in GOROOT (/home/runner/work/project/src/bufio)
So, I managed to solve the issue myself. The checkout#v2 action does only allow relative paths, however, we can just clone the dependency manually.
- name: Checkout dependencies
run: |
git clone https://${{ secrets.GIT_ACCESS_TOKEN }}#github.com/myorg/dependency.git ${GOROOT}/src/dependency
In this way, it will also work with different Go versions yielding in a different GOROOT.
The full pipeline steps:
steps:
## sets up go based on the version
- name: Install Go
uses: actions/setup-go#v2
with:
go-version: ${{ matrix.go-version }}
env:
GO111MODULE: "on"
- name: Checkout dependencies
run: |
git clone https://${{ secrets.GIT_ACCESS_TOKEN }}#github.com/myorg/dependency.git
## checks out our code locally so we can work with the files
- name: Checkout code
uses: actions/checkout#v2
## runs a build
- name: Build
run: go build src
## runs go test ./...
- name: Test
run: go test ./...
Try to use GitHub Actions but getting error when trying to run executable build. I can find executable when doing ls and also changing mod of file executable but didn't got anything. Attached log/output of GitHub action.
Here is my github action workflow file
on:
push:
branches:
- master
name: Build For platforms
jobs:
build:
# We want to define a strategy for our job
strategy:
# this will contain a matrix of all of the combinations
# we wish to test again:
matrix:
go-version: [1.14.x]
platform: [ubuntu-latest]
# Defines the platform for each test run
runs-on: ${{ matrix.platform }}
# the steps that will be run through for each version and platform
# combination
steps:
# sets up go based on the version
- name: Install Go
uses: actions/setup-go#v2
with:
go-version: ${{ matrix.go-version }}
# checks out our code locally so we can work with the files
- name: Checkout code
uses: actions/checkout#v2
- name: Download modules
run: go mod download
- name: Build
run: go build -o executable main.go
- name: Chmod
run: chmod +x executable
- name: List Files
run: ls && pwd
- name: Run
run: ./executable