Skip to content

reuse workload

reuse workload #627

Workflow file for this run

name: Auto Golang Lint And Unitest
env:
COVERAGE_REPORT_PATH: output/test/coverage.out
UNITEST_REPORT_PATH: output/test/unitestreport.json
on:
pull_request: {}
push:
branches:
- main
- release-*
workflow_dispatch:
inputs:
ref:
description: 'branch, sha, tag'
required: true
default: main
workflow_call:
inputs:
ref:
required: true
type: string
permissions: write-all
# concurrency:
# group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.sha }}
# cancel-in-progress: true
jobs:
filter_changes:
name: Deduce required tests from code changes
runs-on: ubuntu-latest
outputs:
check: ${{ env.RUN_CHECK }}
ref: ${{ env.RUN_REF }}
swagger: ${{ env.RUN_SWAGGER }}
crd: ${{ env.RUN_CRD_CHECK }}
steps:
- name: Check Go Code Changes
uses: dorny/paths-filter@v2.11.1
if: ${{ github.event_name == 'pull_request' }}
id: filter_pr
with:
base: ${{ github.event.pull_request.base.sha }}
ref: ${{ github.event.pull_request.head.sha }}
filters: |
src:
- .github/workflows/lint-golang.yaml
- '**/*.go'
- 'go.mod'
- 'go.sum'
- name: Result
id: result
run: |
if ${{ github.event_name == 'push' }} ; then
echo "trigger by push"
echo "RUN_CHECK=true" >> $GITHUB_ENV
echo "RUN_REF=${{ github.sha }}" >> $GITHUB_ENV
echo "RUN_SWAGGER=true" >> $GITHUB_ENV
echo "RUN_CRD_CHECK=true" >> $GITHUB_ENV
elif ${{ github.event_name == 'pull_request' }} ; then
echo "trigger by pull_request"
flag=${{ steps.filter_pr.outputs.src }}
echo "RUN_CHECK=${flag}" >> $GITHUB_ENV
ref=${{ github.event.pull_request.head.sha }}
echo "RUN_REF=${ref}" >> $GITHUB_ENV
echo "RUN_SWAGGER=${flag}" >> $GITHUB_ENV
echo "RUN_CRD_CHECK=${flag}" >> $GITHUB_ENV
elif ${{ inputs.ref != '' }} ; then
echo "trigger by workflow_call"
echo "RUN_CHECK=true" >> $GITHUB_ENV
echo "RUN_SWAGGER=true" >> $GITHUB_ENV
echo "RUN_CRD_CHECK=true" >> $GITHUB_ENV
echo "RUN_REF=${{ inputs.ref }}" >> $GITHUB_ENV
elif ${{ github.event_name == 'workflow_dispatch' }} ; then
echo "trigger by workflow_dispatch"
echo "RUN_CHECK=true" >> $GITHUB_ENV
echo "RUN_SWAGGER=true" >> $GITHUB_ENV
echo "RUN_CRD_CHECK=true" >> $GITHUB_ENV
echo "RUN_REF=${{ github.event.inputs.ref }}" >> $GITHUB_ENV
else
echo "error, unexpected event "
exit 1
fi
lint-code:
needs: filter_changes
runs-on: ubuntu-latest
steps:
- name: Install Go
uses: actions/setup-go@v4
with:
go-version: '1.20.0'
- name: Checkout code
uses: actions/checkout@v4
with:
persist-credentials: false
ref: ${{ needs.filter_changes.outputs.ref }}
- name: check swagger sdk
if: ${{ needs.filter_changes.outputs.swagger == 'true' }}
run: |
make validate_openapi_sdk
- name: check crd sdk
if: ${{ needs.filter_changes.outputs.crd == 'true' }}
run: |
make validate_crd_sdk
lint-golang:
needs: filter_changes
if: ${{ needs.filter_changes.outputs.check == 'true' }}
runs-on: ubuntu-latest
steps:
- name: Install Go
uses: actions/setup-go@v4
with:
go-version: '1.20.0'
- name: Checkout code
uses: actions/checkout@v4
with:
persist-credentials: false
ref: ${{ needs.filter_changes.outputs.ref }}
- name: Check module vendoring
run: |
go mod tidy
go mod vendor
if ! test -z "$(git status --porcelain)"; then
echo "please run 'go mod tidy && go mod vendor', and submit your changes"
exit 1
fi
# ================ lint
- name: Run golangci-lint
id: golangci_lint
continue-on-error: true
uses: golangci/golangci-lint-action@v3
with:
version: latest
- name: Check Make lint-golang
id: lintgolang
continue-on-error: true
run: |
make lint_golang_everything
- name: Result
run: |
result=${{ steps.golangci_lint.outcome }}
[ "${result}"x == "failure"x ] && echo "step golangci_lint failed" && exit 1
result=${{ steps.lintgolang.outcome }}
[ "${result}"x == "failure"x ] && echo "step lintgolang failed" && exit 3
echo "all succeed"
exit 0
unitest:
needs: filter_changes
if: ${{ needs.filter_changes.outputs.check == 'true' }}
runs-on: ubuntu-latest
steps:
- name: Install Go
uses: actions/setup-go@v4
with:
go-version: '1.20.0'
- name: Checkout code
uses: actions/checkout@v4
with:
persist-credentials: false
ref: ${{ needs.filter_changes.outputs.ref }}
# ================= unitest
- name: Run unitest
id: unitest
continue-on-error: true
run: |
make unitest_tests
- name: Upload Coverage Artifact
if: ${{ steps.unitest.outcome == 'failure' }}
uses: actions/upload-artifact@v3.1.3
with:
name: coverage.out
path: ${{ env.COVERAGE_REPORT_PATH }}
retention-days: 1
- name: Upload Report Artifact
if: ${{ steps.unitest.outcome == 'failure' }}
uses: actions/upload-artifact@v3.1.3
with:
name: unitestreport.json
path: ${{ env.UNITEST_REPORT_PATH }}
retention-days: 1
# ============= upload coverage report
- name: Upload to Codecov
if: ${{ steps.unitest.outcome != 'failure' }}
uses: codecov/codecov-action@v3.1.4
with:
directory: './'
files: '${{ env.COVERAGE_REPORT_PATH }}'
flags: unittests
name: my-codecov-umbrella
fail_ci_if_error: true
verbose: true
token: ${{ secrets.CODECOV_TOKEN }}
- name: Result
if: ${{ steps.unitest.outcome == 'failure' }}
run: |
echo "unitest failed"
exit 1