Skip to content

Update workflow to trigger on push events to main branch #117

Update workflow to trigger on push events to main branch

Update workflow to trigger on push events to main branch #117

Workflow file for this run

name: go-tests
on:
pull_request:
branches: [ main ]
push:
branches: [ main ]
env:
TEST_RESULTS: /tmp/test-results
jobs:
go-tests:
runs-on: ubuntu-latest
strategy:
matrix:
go-version: ['stable', 'oldstable']
steps:
- name: Setup go
uses: actions/setup-go@4a3601121dd01d1626a1e23e37211e3254c1c06c # v6.4.0
with:
go-version: ${{ matrix.go-version }}
- name: Checkout code
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
- name: Create test directory
run: |
mkdir -p ${{ env.TEST_RESULTS }}
- name: Download go modules
run: go mod download
- name: Cache / restore go modules
uses: actions/cache@668228422ae6a00e4ad889ee87cd7109ec5666a7 # v5.0.4
with:
path: |
~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
# Check go fmt output because it does not report non-zero when there are fmt changes
- name: Run gofmt
run: |
go fmt ./...
files=$(go fmt ./...)
if [ -n "$files" ]; then
echo "The following file(s) do not conform to go fmt:"
echo "$files"
exit 1
fi
- name: Run golangci-lint
uses: golangci/golangci-lint-action@1e7e51e771db61008b38414a730f564565cf7c20
# Install gotestsum with go get for 1.15.3; otherwise default to go install
- name: Install gotestsum
run: |
GTS="gotest.tools/gotestsum@v1.8.2"
# We use the same error message prefix in either failure case, so just define it once here.
ERROR="Failed to install $GTS"
# First try to 'go install', if that fails try 'go get'...
go install "$GTS" || go get "$GTS" || { echo "$ERROR: both 'go install' and 'go get' failed"; exit 1; }
# Check that the gotestsum command was actually installed in the path...
command -v gotestsum > /dev/null 2>&1 || { echo "$ERROR: gotestsum command not installed"; exit 1; }
echo "OK: Command 'gotestsum' installed ($GTS)"
- name: Run go tests
run: |
PACKAGE_NAMES=$(go list ./...)
gotestsum --format=short-verbose --junitfile $TEST_RESULTS/gotestsum-report.xml -- -p 2 -cover -coverprofile=coverage.out $PACKAGE_NAMES
# Save coverage report parts
- name: Upload and save artifacts
uses: actions/upload-artifact@bbbca2ddaa5d8feaa63e36b76fdaad77386f024f
with:
name: Test Results-${{matrix.go-version}}
path: ${{ env.TEST_RESULTS }}
- name: Upload coverage report
uses: actions/upload-artifact@bbbca2ddaa5d8feaa63e36b76fdaad77386f024f
with:
path: coverage.out
name: Coverage-report-${{matrix.go-version}}
- name: Display coverage report
run: go tool cover -func=coverage.out