Skip to content
This repository was archived by the owner on Nov 14, 2024. It is now read-only.

Commit 05bf03c

Browse files
authored
chore: run automatic build and test (#13)
1 parent 18da19e commit 05bf03c

File tree

4 files changed

+264
-0
lines changed

4 files changed

+264
-0
lines changed

.github/workflows/build.yaml

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
name: Build
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
8+
pull_request:
9+
branches:
10+
- main
11+
12+
workflow_dispatch:
13+
14+
permissions:
15+
actions: none
16+
checks: none
17+
contents: read
18+
deployments: none
19+
issues: none
20+
packages: none
21+
pull-requests: none
22+
repository-projects: none
23+
security-events: none
24+
statuses: none
25+
26+
jobs:
27+
build:
28+
name: Build
29+
runs-on: ubuntu-20.04
30+
steps:
31+
- name: Cancel Previous Runs
32+
if: github.event_type == 'pull_request'
33+
uses: styfle/cancel-workflow-action@0.9.1
34+
35+
- name: Checkout
36+
uses: actions/checkout@v2
37+
38+
- name: Install Go
39+
uses: actions/setup-go@v2
40+
with:
41+
go-version: '^1.16.7'
42+
43+
- name: Install dependencies
44+
run: ./scripts/install_deps.sh
45+
46+
- name: Lint
47+
run: make lint
48+
49+
- name: Tests
50+
run: ./scripts/test_go.sh

scripts/install_deps.sh

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#!/usr/bin/env bash
2+
#
3+
# This script installs dependencies to /usr/local/bin.
4+
5+
set -euo pipefail
6+
PROJECT_ROOT=$(git rev-parse --show-toplevel)
7+
cd "$PROJECT_ROOT"
8+
source "./scripts/lib.sh"
9+
10+
TMPDIR=$(mktemp -d)
11+
TMPBIN="${TMPDIR}/bin"
12+
BINDIR="/usr/local/bin"
13+
14+
curl_flags=(
15+
--silent
16+
--show-error
17+
--location
18+
)
19+
20+
# Install Go programs
21+
export GOPATH="$TMPDIR/go"
22+
23+
run_trace false mkdir --parents "$GOPATH"
24+
25+
# goveralls collects code coverage metrics from tests
26+
# and sends to Coveralls
27+
run_trace false go install github.com/mattn/goveralls@v0.0.9
28+
29+
# Install binaries only
30+
run_trace false sudo install --mode=0755 --target-directory="$BINDIR" "$GOPATH/bin/*"
31+
32+
# Install packages via apt where available
33+
run_trace false sudo apt-get install --no-install-recommends --yes \
34+
shellcheck
35+
36+
# Extract binaries as non-root user, then sudo install
37+
run_trace false mkdir --parents "$TMPBIN"
38+
39+
# gotestsum makes test output more readable
40+
GOTESTSUM_VERSION="1.7.0"
41+
run_trace false curl "${curl_flags[@]}" "https://github.com/gotestyourself/gotestsum/releases/download/v${GOTESTSUM_VERSION}/gotestsum_${GOTESTSUM_VERSION}_linux_amd64.tar.gz" \| \
42+
tar --extract --gzip --directory="$TMPBIN" --file=- gotestsum
43+
44+
# golangci-lint to lint Go code with multiple tools
45+
GOLANGCI_LINT_VERSION="1.42.1"
46+
run_trace false curl "${curl_flags[@]}" "https://github.com/golangci/golangci-lint/releases/download/v${GOLANGCI_LINT_VERSION}/golangci-lint-${GOLANGCI_LINT_VERSION}-linux-amd64.tar.gz" \| \
47+
tar --extract --gzip --directory="$TMPBIN" --file=- --strip-components=1 "golangci-lint-${GOLANGCI_LINT_VERSION}-linux-amd64/golangci-lint"
48+
49+
# goreleaser to compile, cross-compile, and release binaries
50+
GORELEASER_VERSION="0.178.0"
51+
run_trace false curl "${curl_flags[@]}" "https://github.com/goreleaser/goreleaser/releases/download/v${GORELEASER_VERSION}/goreleaser_Linux_x86_64.tar.gz" \| \
52+
tar --extract --gzip --directory="$TMPBIN" --file=- "goreleaser"
53+
54+
run_trace false sudo install --mode=0755 --target-directory="$BINDIR" "$TMPBIN/*"
55+
56+
run_trace false command -v \
57+
golangci-lint \
58+
goreleaser \
59+
gotestsum
60+
61+
run_trace false sudo rm --verbose --recursive --force "$TMPDIR"

scripts/lib.sh

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#!/usr/bin/env bash
2+
3+
set -euo pipefail
4+
5+
# Emit a message to stderr and exit.
6+
#
7+
# This prints the arguments to stderr before exiting.
8+
#
9+
# Example:
10+
# error "Missing flag abc"
11+
# program-failure-info | error
12+
function error() {
13+
set +x
14+
echo
15+
echo "$@" "$(cat)" >&2
16+
echo
17+
exit 1
18+
}
19+
20+
# Check if dependencies are available.
21+
#
22+
# If any dependencies are missing, an error message will be printed to
23+
# stderr and the program will exit, running traps on EXIT beforehand.
24+
#
25+
# Example:
26+
# check_dependencies git bash node
27+
check_dependencies() {
28+
local missing=false
29+
for command in "$@"; do
30+
if ! command -v "$command" &> /dev/null; then
31+
echo "$0: script requires '$command', but it is not in your PATH" >&2
32+
missing=true
33+
fi
34+
done
35+
36+
if [ "$missing" = true ]; then
37+
exit 1
38+
fi
39+
}
40+
41+
# Indent output by (indent) levels
42+
#
43+
# Example:
44+
# echo "example" | indent 2
45+
# cat file.txt | indent
46+
function indent() {
47+
local indentSize=2
48+
local indent=1
49+
if [ -n "${1:-}" ]; then
50+
indent="$1"
51+
fi
52+
pr --omit-header --indent=$((indent * indentSize))
53+
}
54+
55+
# Run a command, with tracing.
56+
#
57+
# This prints a command to stderr for tracing, in a format similar to
58+
# the bash xtrace option (i.e. set -x, set -o xtrace), then runs it using
59+
# eval if the first argument (dry run) is false.
60+
#
61+
# If dry run is true, the command and arguments will be printed, but
62+
# not executed.
63+
#
64+
# Example:
65+
# run_trace $DRY_RUN rm -rf /
66+
# run_trace false echo "abc" \| indent
67+
function run_trace() {
68+
local args=("$@")
69+
local dry_run="${args[0]}"
70+
args=("${args[@]:1}")
71+
72+
# If we're running in GitHub Actions, use the special syntax
73+
# to start/end a group for each traced command
74+
if [[ -n "${GITHUB_ACTIONS:-}" && "$dry_run" = false ]]; then
75+
echo "::group::Run ${args[*]}" >&2
76+
else
77+
echo "+ ${args[*]}" >&2
78+
fi
79+
80+
local exit_status=0
81+
if [ "$dry_run" = false ]; then
82+
eval "${args[@]}"
83+
84+
# Save the exit status code from eval, otherwise it will be
85+
# obscured by future commands.
86+
exit_status="$?"
87+
fi
88+
89+
if [[ -n "${GITHUB_ACTIONS:-}" && "$dry_run" = false ]]; then
90+
echo "::endgroup::" >&2
91+
fi
92+
93+
return "$exit_status"
94+
}

scripts/test_go.sh

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#!/usr/bin/env bash
2+
#
3+
# Run unit and integration tests for Go code
4+
5+
set -euo pipefail
6+
PROJECT_ROOT="$(git rev-parse --show-toplevel)"
7+
cd "$PROJECT_ROOT"
8+
source "./scripts/lib.sh"
9+
10+
echo "--- Running go test"
11+
export FORCE_COLOR=true
12+
13+
test_args=(
14+
-v
15+
-failfast
16+
"${TEST_ARGS:-}"
17+
)
18+
19+
REPORTDIR="/tmp/testreports"
20+
mkdir -p "$REPORTDIR"
21+
TESTREPORT_JSON="$REPORTDIR/test_go.json"
22+
TESTREPORT_XML="$REPORTDIR/test_go.xml"
23+
COVERAGE="$REPORTDIR/test_go.coverage"
24+
25+
test_args+=(
26+
"-covermode=set"
27+
"-coverprofile=$COVERAGE"
28+
)
29+
30+
# Allow failures to ensure that we can upload coverage
31+
set +e
32+
33+
run_trace false gotestsum \
34+
--debug \
35+
--jsonfile="$TESTREPORT_JSON" \
36+
--junitfile="$TESTREPORT_XML" \
37+
--hide-summary=skipped \
38+
--packages="./..." \
39+
-- "${test_args[@]}"
40+
test_status=$?
41+
42+
# The following steps should never fail, and if they do,
43+
# we want to know about it, so fail the build
44+
set -e
45+
46+
threshold="5s"
47+
echo "--- ಠ_ಠ The following tests took longer than $threshold to complete:"
48+
run_trace false gotestsum tool slowest \
49+
--jsonfile="$TESTREPORT_JSON" \
50+
--threshold="$threshold"
51+
52+
# From time to time, Coveralls seems to have an issue on their end, so
53+
# make a best-effort attempt to upload coverage but ignore failures
54+
set +e
55+
echo "--- Uploading test coverage report to Coveralls..."
56+
run_trace false goveralls -service=github -coverprofile="$COVERAGE"
57+
set -e
58+
59+
exit $test_status

0 commit comments

Comments
 (0)