Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build: Use golangci action and make parallel. #3160

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 36 additions & 6 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,34 @@ permissions:
contents: read

jobs:
resolve-modules:
name: Resolve Go modules
runs-on: ubuntu-latest
outputs:
matrix: ${{ steps.set-matrix.outputs.matrix }}
steps:
- name: Check out source
uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 # v3.5.3
- id: set-matrix
run: |
echo "Resolving modules in $(pwd)" && \
MODPATHS=$(find . -mindepth 2 -type f -name go.mod -printf '{"workdir":"%h"},') && \
echo "matrix={\"include\":[${MODPATHS%,}]}" >> $GITHUB_OUTPUT
build:
name: Go CI
runs-on: ubuntu-latest
strategy:
matrix:
go: ["1.19", "1.20"]
steps:
- name: Check out source
uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 # v3.5.3
- name: Set up Go
uses: actions/setup-go@6edd4406fa81c3da01a34fa6f6343087c207a568 #v3.5.0
uses: actions/setup-go@fac708d6674e30b6ba41289acaab6d4b75aa0753 # v4.0.1
with:
go-version: ${{ matrix.go }}
- name: Check out source
uses: actions/checkout@8e5e7e5ab8b370d6c329ec480221332ada57f0ab #v3.5.2
- name: Install Linters
run: "curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.53.1"
- name: Use test and module cache
uses: actions/cache@88522ab9f39a2ea568f7027eddc7d8d8bc9d59c8 #v3.3.1
uses: actions/cache@88522ab9f39a2ea568f7027eddc7d8d8bc9d59c8 # v3.3.1
with:
path: |
~/.cache/go-build
Expand All @@ -35,3 +46,22 @@ jobs:
- name: Test
run: |
sh ./run_tests.sh
lint:
name: Lint
needs: resolve-modules
runs-on: ubuntu-latest
strategy:
matrix: ${{ fromJson(needs.resolve-modules.outputs.matrix ) }}
steps:
- name: Check out source
uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 # v3.5.3
- name: Set up Go
uses: actions/setup-go@fac708d6674e30b6ba41289acaab6d4b75aa0753 # v4.0.1
with:
go-version: "1.20"
- name: golangci-lint
uses: golangci/golangci-lint-action@639cd343e1d3b897ff35927a75193d57cfcba299 #v3.6.0
with:
install-mode: "goinstall"
version: 2dcd82f331c9e834f283075b23ef289435be9354 # v1.53.3
working-directory: ${{ matrix.workdir }}
32 changes: 32 additions & 0 deletions lint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/bin/sh

set -ex

# The script uses golangci-lint (github.com/golangci/golangci-lint) to run all
# linters defined by the configuration in .golangci.yml on every module in the
# repository.

go version

# loop all modules
ROOTPKG=$(go list)
ROOTPKGPATTERN=$(echo $ROOTPKG | sed 's,\\,\\\\,g' | sed 's,/,\\/,g')
MODPATHS=$(go list -m all | grep "^$ROOTPKGPATTERN" | cut -d' ' -f1)
for module in $MODPATHS; do
echo "==> lint ${module}"

# determine module directory
MODNAME=$(echo $module | sed -E -e "s/^$ROOTPKGPATTERN//" \
-e 's,^/,,' -e 's,/v[0-9]+$,,')
if [ -z "$MODNAME" ]; then
MODNAME=.
fi

# run commands in the module directory as a subshell
(
cd $MODNAME

# run linters
golangci-lint run
)
done
38 changes: 6 additions & 32 deletions run_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,11 @@

set -ex

# The script does automatic checking on a Go package and its sub-packages,
# including:
# 1. gofmt (https://golang.org/cmd/gofmt/)
# 2. gosimple (https://github.com/dominikh/go-simple)
# 3. unconvert (https://github.com/mdempsky/unconvert)
# 4. ineffassign (https://github.com/gordonklaus/ineffassign)
# 5. go vet (https://golang.org/cmd/vet)
# 6. misspell (https://github.com/client9/misspell)

# golangci-lint (github.com/golangci/golangci-lint) is used to run each
# static checker.
# This script runs the tests for all packages in all Go modules in the
# repository.
#
# It will also run the linters for all Go modules in the repository when not
# running as a GitHub action.

go version

Expand All @@ -21,27 +15,7 @@ echo "==> test all modules"
ROOTPKG=$(go list)
go test -short -tags rpctest $ROOTPKG/...

# loop all modules
ROOTPKGPATTERN=$(echo $ROOTPKG | sed 's,\\,\\\\,g' | sed 's,/,\\/,g')
MODPATHS=$(go list -m all | grep "^$ROOTPKGPATTERN" | cut -d' ' -f1)
for module in $MODPATHS; do
echo "==> lint ${module}"

# determine module directory
MODNAME=$(echo $module | sed -E -e "s/^$ROOTPKGPATTERN//" \
-e 's,^/,,' -e 's,/v[0-9]+$,,')
if [ -z "$MODNAME" ]; then
MODNAME=.
fi

# run commands in the module directory as a subshell
(
cd $MODNAME

# run linters
golangci-lint run
)
done
[ -z "$GITHUB_ACTIONS" ] && ./lint.sh

echo "------------------------------------------"
echo "Tests completed successfully!"
Loading