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

chore: Make Go Pipeline better and safer #89

Merged
merged 1 commit into from
Nov 15, 2023
Merged
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
51 changes: 26 additions & 25 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: 1.21
go-version: '1.21'
kehoecj marked this conversation as resolved.
Show resolved Hide resolved

- name: Download
- name: Download dependencies
run: go mod download

lint:
Expand All @@ -34,13 +34,13 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: 1.21
go-version: '1.21'

- name: Static Analysis
run: go vet ./...

- name: Check Formatting
run: if [ "$(gofmt -s -l -e . | wc -l)" -gt 0 ]; then exit 1; fi
run: test -z "$(gofmt -s -l -e .)"
kehoecj marked this conversation as resolved.
Show resolved Hide resolved

build:
needs: download
Expand All @@ -51,10 +51,12 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: 1.21
go-version: '1.21'

- name: Build
run: CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags='-w -s -extldflags "-static"' -tags netgo -o validator cmd/validator/validator.go
run: |
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 \
go build -ldflags='-w -s -extldflags "-static"' -tags netgo -o validator cmd/validator/validator.go
kehoecj marked this conversation as resolved.
Show resolved Hide resolved

test:
needs: download
Expand All @@ -69,29 +71,28 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: 1.21
go-version: '1.21'

- name: Unit test
run: go test -v -cover -coverprofile coverage.out ./...

- name: Check coverage
id: check-coverage
env:
COVERAGE_THRESHOLD: 95
kehoecj marked this conversation as resolved.
Show resolved Hide resolved
run: |
# Validate that the coverage is above or at the required threshold
echo "Checking if test coverage is above threshold ..."
echo "Coverage threshold: $COVERAGE_THRESHOLD %"
totalCoverage=`go tool cover -func coverage.out | grep total | grep -Eo '[0-9]+\.[0-9]+'`
echo "Current test coverage : $totalCoverage %"
if (( $(echo "$COVERAGE_THRESHOLD <= $totalCoverage" | bc -l) )); then
echo "Coverage OK"
echo "Coverage threshold: ${COVERAGE_THRESHOLD} %"
totalCoverage=$(go tool cover -func coverage.out | grep 'total' | grep -Eo '[0-9]+\.[0-9]+')
echo "Current test coverage : ${totalCoverage} %"
if (( $(echo "${COVERAGE_THRESHOLD} <= ${totalCoverage}" | bc -l) )); then
echo "Coverage OK"
else
echo "Current test coverage is below threshold"
exit 1
fi

echo "total_coverage=$totalCoverage" >> "$GITHUB_OUTPUT"
env:
COVERAGE_THRESHOLD: 95
echo "total_coverage=${totalCoverage}" >> "${GITHUB_OUTPUT}"

- name: Create badge img tag and apply to README files
id: generate-badge
Expand All @@ -100,31 +101,31 @@ jobs:
# Badge will always be green because of coverage threshold check
# so we just have to populate the total coverage
totalCoverage=${{ steps.check-coverage.outputs.total_coverage }}
BADGE_URL=https://img.shields.io/badge/Coverage-$totalCoverage%25-brightgreen
BADGE_IMG_TAG="<img id=\"cov\" src=\"$BADGE_URL\" alt=\"Code Coverage\" />"
BADGE_URL="https://img.shields.io/badge/Coverage-${totalCoverage}%25-brightgreen"
BADGE_IMG_TAG="<img id=\"cov\" src=\"${BADGE_URL}\" alt=\"Code Coverage\">"

# Update README.md and index.md
for markdown_file in README.md index.md; do
sed -i "/id=\"cov\"/c\\${BADGE_IMG_TAG}" $markdown_file
sed -i "/id=\"cov\"/c\\${BADGE_IMG_TAG}" "${markdown_file}"
done

# Check to see if files were updated
if [[ `git status --porcelain` ]]; then
echo "badge_updates=true" >> "$GITHUB_OUTPUT"
if git diff --quiet; then
kehoecj marked this conversation as resolved.
Show resolved Hide resolved
echo "badge_updates=true" >> "${GITHUB_OUTPUT}"
else
echo "badge_updates=false" >> "$GITHUB_OUTPUT"
echo "badge_updates=false" >> "${GITHUB_OUTPUT}"
fi

- name: Commit changes
if: steps.generate-badge.outputs.badge_updates == 'true' && github.event_name != 'pull_request'
if: steps.generate-badge.outputs.badge_updates == 'true' && github.event_name == 'push'
kehoecj marked this conversation as resolved.
Show resolved Hide resolved
run: |
git config --local user.email "[email protected]"
git config --local user.name "GitHub Action"
git add README.md index.md
git add -- README.md index.md
kehoecj marked this conversation as resolved.
Show resolved Hide resolved
git commit -m "chore: Updated coverage badge."

- name: Push changes
if: steps.generate-badge.outputs.badge_updates == 'true' && github.event_name != 'pull_request'
if: steps.generate-badge.outputs.badge_updates == 'true' && github.event_name == 'push'
uses: ad-m/github-push-action@master
with:
github_token: ${{ github.token }}
Expand Down