From 156c798bb9222f94d826b4f8540437ccc275e071 Mon Sep 17 00:00:00 2001 From: wenovus Date: Mon, 7 Aug 2023 16:50:20 -0700 Subject: [PATCH 1/3] Add basic Reusable Go workflow --- .github/workflows/basic_go.yml | 127 +++++++++++++++++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 .github/workflows/basic_go.yml diff --git a/.github/workflows/basic_go.yml b/.github/workflows/basic_go.yml new file mode 100644 index 0000000..1e6c5bc --- /dev/null +++ b/.github/workflows/basic_go.yml @@ -0,0 +1,127 @@ +name: Common Go + +on: + workflow_call: + inputs: + static-analysis-excludes-regex: + type: string + # TODO(wenvous): gofmt is harder to work with since it always recursively + # formats files instead of restricting itself to just listed packages. + # See if there is a way to have the same behaviour without relying on the + # tool. + skip-gofmt: + type: boolean + skip-govet: + type: boolean + skip-staticcheck: + type: boolean + skip-race-tests: + type: boolean + race-tests-excludes-regex: + type: boolean + +jobs: + build_and_test: + name: Build and Test + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + go: ['1.19', '1.20', '1.x'] + + steps: + - name: Set up Go + uses: actions/setup-go@v4 + with: + go-version: ${{ matrix.go }} + id: go + + - name: Check out code + uses: actions/checkout@v3 + + - name: Get dependencies + run: | + go get -v -t -d ./... + + - name: Build packages + run: go build -v ./... + + - name: Run Tests + run: go test -v ./... + + - name: Run race tests + if: ${{ !inputs.skip-race-tests }} + run: | + if [ -z "${{ inputs.race-tests-excludes-regex }}" ]; then + echo "Running for all packages" + go test -race -v ./... + else + echo "Running for non-excluded packages" + go test -race -v $(go list ./... | egrep -v "${{ inputs.race-tests-excludes-regex }}") + fi + + static_analysis: + name: Static Analysis + runs-on: ubuntu-latest + + steps: + - name: Install Go + uses: actions/setup-go@v4 + with: + go-version: '1.x' + id: go + + - name: Check out code + uses: actions/checkout@v3 + + - name: Get dependencies + run: | + go get -v -t -d ./... + + - name: Go Mod should be tidy + run: | + go mod tidy + diff -u <(echo -n) <(git diff) + + - name: Go vet + if: ${{ !cancelled() && !inputs.skip-govet }} + run: | + if [ -z "${{ inputs.static-analysis-excludes-regex }}" ]; then + echo "Running for all packages" + go vet ./... + else + echo "Running for non-excluded packages" + go vet $(go list ./... | egrep -v "${{ inputs.static-analysis-excludes-regex }}") + fi + + - name: Gofmt + if: ${{ !cancelled() && !inputs.skip-gofmt }} + run: | + diff -u <(echo -n) <(gofmt -d -s .) + + - name: Run coverage + if: '!cancelled()' + run: | + go test -v -coverprofile=profile.cov ./... + + - name: Submit coverage + if: '!cancelled()' + uses: shogo82148/actions-goveralls@v1 + with: + path-to-profile: profile.cov + + - name: Install staticcheck + if: '!cancelled()' + run: | + go install honnef.co/go/tools/cmd/staticcheck@latest + + - name: Staticcheck + if: ${{ !cancelled() && !inputs.skip-staticcheck }} + run: | + if [ -z "${{ inputs.static-analysis-excludes-regex }}" ]; then + echo "Running for all packages" + staticcheck ./... + else + echo "Running for non-excluded packages" + staticcheck $(go list ./... | egrep -v ${{ inputs.static-analysis-excludes-regex }}) + fi From 8d2987b8a26f4f3190a7f2e7db60f18489a3624a Mon Sep 17 00:00:00 2001 From: wenovus Date: Mon, 7 Aug 2023 17:06:32 -0700 Subject: [PATCH 2/3] Fix parameter type --- .github/workflows/basic_go.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/basic_go.yml b/.github/workflows/basic_go.yml index 1e6c5bc..7e350b5 100644 --- a/.github/workflows/basic_go.yml +++ b/.github/workflows/basic_go.yml @@ -9,6 +9,8 @@ on: # formats files instead of restricting itself to just listed packages. # See if there is a way to have the same behaviour without relying on the # tool. + race-tests-excludes-regex: + type: string skip-gofmt: type: boolean skip-govet: @@ -17,8 +19,6 @@ on: type: boolean skip-race-tests: type: boolean - race-tests-excludes-regex: - type: boolean jobs: build_and_test: From 97d0e02bb9b9449f8a4df5ce3997840216ff9f5f Mon Sep 17 00:00:00 2001 From: wenovus Date: Tue, 8 Aug 2023 09:28:22 -0700 Subject: [PATCH 3/3] Change coveralls and go get --- .github/workflows/basic_go.yml | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/.github/workflows/basic_go.yml b/.github/workflows/basic_go.yml index 7e350b5..5dda19f 100644 --- a/.github/workflows/basic_go.yml +++ b/.github/workflows/basic_go.yml @@ -41,7 +41,7 @@ jobs: - name: Get dependencies run: | - go get -v -t -d ./... + go mod download - name: Build packages run: go build -v ./... @@ -76,7 +76,7 @@ jobs: - name: Get dependencies run: | - go get -v -t -d ./... + go mod download - name: Go Mod should be tidy run: | @@ -104,11 +104,15 @@ jobs: run: | go test -v -coverprofile=profile.cov ./... + - name: Install goveralls + if: '!cancelled()' + run: go install github.com/mattn/goveralls@latest + - name: Submit coverage if: '!cancelled()' - uses: shogo82148/actions-goveralls@v1 - with: - path-to-profile: profile.cov + env: + COVERALLS_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: goveralls -coverprofile=profile.cov -service=github - name: Install staticcheck if: '!cancelled()'