Make the build failure when code coverage is less than x% - go

I'm working with github actions and in my tests I need to make myt build fail when my code coverage percentage is less than 80%. I looked up some github actions in github marketplace but dont find anything. Can I do it ? I'm linking my workflow file if it migth help
---
name: lint build and test
on:
push:
branches: [master]
pull_request:
branches: [master]
jobs:
build-and-test:
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout#v2
- name: Set up Go
uses: actions/setup-go#v2
with:
go-version: 1.15
env:
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
- name: Super-Linter
uses: github/super-linter#v3.14.0
env:
VALIDATE_GO: false
VALIDATE_JSCPD: false
VALIDATE_ALL_CODEBASE: true
DEFAULT_BRANCH: master
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: golangci-lint
uses: golangci/golangci-lint-action#v2
with:
version: v1.29
- name: Build
run: go build -o apiDateTime -v ./...
- name: Test
run: go test ./... -coverprofile cover.out -covermode atomic
- name: Coverage result
run: go tool cover -func cover.out

I would replace invocation of go test with an invocation of a shell script as explained here.
The shell script would look something like this
!#/bin/sh
set -e -u
go test ./... -coverprofile cover.out -covermode atomic
perc=`go tool cover -func=cover.out | tail -n 1 | sed -Ee 's!^[^[:digit:]]+([[:digit:]]+(\.[[:digit:]]+)?)%$!\1!'`
res=`echo "$perc >= 80.0" | bc`
test "$res" -eq 1 && exit 0
echo "Insufficient coverage: $perc" >&2
exit 1
Where:
The encantation involving sed extracts the coverage percentage (see here).
The next line asks the calculator to compare the percentage with your configured threshold.
Then the next line makes the script exit successfully if the test passed.
The rest of the script blows up if the coverage requirements were not met.
This script expects the bc tool is installed in that ubuntu-latest package (which I don't know).
If it isn't, the whole thing can be scripted in any language available in the image—such as Perl or Python.

Related

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

Build command not found. 'go'

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

How to use Go Release Binary GitHub Action

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 }}

Can you have multiple working directories with github actions?

So I have a repo with multiple directories for multiple go projects. Is there a way to run github actions on multiple working directories so I don't have to have redundant yaml with github actions? To automate an error check with golang, I currently have:
errcheck:
name: Errcheck
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#master
- name: check
uses: grandcolline/golang-github-actions#v1.1.0
working-directory: ./app1
with:
run: errcheck
token: ${{ secrets.GITHUB_TOKEN }}
But I'd like to have:
errcheck:
name: Errcheck
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#master
- name: check
uses: grandcolline/golang-github-actions#v1.1.0
working-directory: [./app1, ./app2]
with:
run: errcheck
token: ${{ secrets.GITHUB_TOKEN }}
In order to run something in more than one working directory, I believe you have two options:
Option 1: Matrix
Use GitHub Action's jobs.<job_id>.strategy.matrix option. This will create multiple jobs, each with its own matrix (directory) value.
Here is a sample workflow:
name: Test
on:
push: { branches: master }
jobs:
test:
name: Matrix test
runs-on: ubuntu-latest
strategy:
matrix: { dir: ['some-dir', 'other-dir'] }
steps:
- name: Checkout code
uses: actions/checkout#v2
- name: Do something with the matrix value
working-directory: ${{ matrix.dir }}
run: pwd
Running this will create two jobs:
Option 2: Custom Shell Script
If the matrix option is not suitable for your needs, a simple shell script that loops through and tests all your nested applications (directories) might be appropriate. In this case, you ignore the working-direcoty directive in the workflow YAML, and let the script cd to each of them.
For example:
#!/usr/bin/env bash
dirs=( some-dir other-dir )
for dir in "${dirs[#]}"; do
pushd "$dir"
pwd # Do something more significant here
popd
done

GitHub Action unable to execute build executable

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

Resources