|
| 1 | +name: Build |
| 2 | + |
| 3 | +permissions: |
| 4 | + contents: write |
| 5 | + |
| 6 | +on: |
| 7 | + push: |
| 8 | + branches: [main] |
| 9 | + pull_request: |
| 10 | + branches: [main] |
| 11 | + |
| 12 | +jobs: |
| 13 | + build: |
| 14 | + runs-on: ubuntu-latest |
| 15 | + strategy: |
| 16 | + fail-fast: false |
| 17 | + matrix: |
| 18 | + go: |
| 19 | + - "stable" |
| 20 | + steps: |
| 21 | + - uses: actions/checkout@v4 |
| 22 | + with: |
| 23 | + fetch-depth: 0 |
| 24 | + fetch-tags: true |
| 25 | + persist-credentials: true |
| 26 | + |
| 27 | + - name: Set up Go |
| 28 | + uses: actions/setup-go@v5 |
| 29 | + with: |
| 30 | + go-version: ${{ matrix.go }} |
| 31 | + |
| 32 | + - name: Go generate |
| 33 | + run: go generate ./... |
| 34 | + |
| 35 | + - name: Test |
| 36 | + run: go test -tags debug -bench=. -coverprofile=coverage.out ./... |
| 37 | + |
| 38 | + - name: Run Gosec Security Scanner |
| 39 | + uses: securego/gosec@master |
| 40 | + with: |
| 41 | + args: ./... |
| 42 | + |
| 43 | + - name: Build |
| 44 | + run: go build . |
| 45 | + |
| 46 | + - name: Go report card |
| 47 | + uses: creekorful/[email protected] |
| 48 | + |
| 49 | + # Generate code coverage badge and push it to the 'coverage' branch. |
| 50 | + # Reference it in your README.md like this: |
| 51 | + # |
| 52 | + # [](#) |
| 53 | + # |
| 54 | + # If you have a detailed HTML report of the coverage (here called report.html), replace the '#' with: |
| 55 | + # |
| 56 | + # https://htmlpreview.github.io/?https://github.com/USERNAME/REPO/blob/coverage/BRANCH/report.html |
| 57 | + # |
| 58 | + # You need to have given write permissions for the for the workflow: |
| 59 | + # |
| 60 | + # permissions: |
| 61 | + # contents: write |
| 62 | + # |
| 63 | + # To create the 'coverage' branch for the repository (replace 'main' with the branch you want to publish for): |
| 64 | + # |
| 65 | + # git checkout main |
| 66 | + # git checkout --orphan coverage && git rm --cached $(git ls-files) && echo '# Coverage branch' > README.md |
| 67 | + # git add README.md && git commit -m 'Add README.md' && git push origin coverage |
| 68 | + # git checkout --force main |
| 69 | + - name: Extract branch name |
| 70 | + run: echo "BRANCH=$(echo ${GITHUB_REF#refs/heads/})" >> $GITHUB_OUTPUT |
| 71 | + id: extract_branch |
| 72 | + - uses: actions/checkout@v3 |
| 73 | + with: |
| 74 | + ref: coverage |
| 75 | + path: coverage |
| 76 | + - name: Ensure directories for this branch exist |
| 77 | + env: |
| 78 | + BRANCH: ${{ steps.extract_branch.outputs.branch }} |
| 79 | + run: mkdir -p coverage/${BRANCH} |
| 80 | + - name: Extract code coverage |
| 81 | + id: coverage |
| 82 | + env: |
| 83 | + BRANCH: ${{ steps.extract_branch.outputs.branch }} |
| 84 | + # Change this to extract the coverage percentage (without the percent sign) for your language into COVERAGE. |
| 85 | + # Here we are using a 'coverage.out' file generated by 'go test -coverprofile=coverage.out ./...' |
| 86 | + # You may also generate a HTML report and place at 'coverage/${BRANCH}/report.html'. |
| 87 | + run: | |
| 88 | + echo "COVERAGE=$(go tool cover -func=coverage.out | tail -n 1 | tr -s '\t' | cut -f 3 | rev | cut -c2- | rev)" >> $GITHUB_OUTPUT |
| 89 | + go tool cover -html=coverage.out -o=coverage/${BRANCH}/report.html |
| 90 | + - name: Generate the badge SVG image |
| 91 | + |
| 92 | + with: |
| 93 | + label: 'coverage' |
| 94 | + status: ${{ steps.coverage.outputs.coverage }}% |
| 95 | + path: coverage/${{ steps.extract_branch.outputs.branch }}/badge.svg |
| 96 | + color: ${{ |
| 97 | + steps.coverage.outputs.coverage > 95 && 'green' || |
| 98 | + steps.coverage.outputs.coverage > 90 && 'yellow,green,green' || |
| 99 | + steps.coverage.outputs.coverage > 80 && 'yellow,green' || |
| 100 | + steps.coverage.outputs.coverage > 70 && 'yellow' || |
| 101 | + steps.coverage.outputs.coverage > 60 && 'orange,yellow' || |
| 102 | + steps.coverage.outputs.coverage > 50 && 'orange' || |
| 103 | + steps.coverage.outputs.coverage > 40 && 'red,orange' || |
| 104 | + steps.coverage.outputs.coverage > 30 && 'red,red,orange' || |
| 105 | + steps.coverage.outputs.coverage > 20 && 'red,red,red,orange' || |
| 106 | + 'red' }} |
| 107 | + - name: Commit coverage files |
| 108 | + env: |
| 109 | + BRANCH: ${{ steps.extract_branch.outputs.branch }} |
| 110 | + run: | |
| 111 | + pushd coverage |
| 112 | + git config --local user.email "[email protected]" |
| 113 | + git config --local user.name "GitHub Action" |
| 114 | + test ! -f "${BRANCH}/badge.svg" || git add "${BRANCH}/badge.svg" |
| 115 | + test ! -f "${BRANCH}/report.html" || git add "${BRANCH}/report.html" |
| 116 | + test -z "$(git status --porcelain)" || git commit -m "update" |
| 117 | + git push |
| 118 | + popd |
0 commit comments