name: vociferate/coverage-badge description: > Generate coverage report artefacts, publish them to object storage, and expose report URLs for workflow summaries. inputs: coverage-profile: description: Path to the Go coverage profile file. required: false default: coverage.out coverage-html: description: Output path for the rendered HTML coverage report. required: false default: coverage.html coverage-badge: description: Output path for the generated SVG badge. required: false default: coverage-badge.svg coverage-summary: description: Output path for the generated coverage summary JSON. required: false default: coverage-summary.json artefact-bucket-name: description: S3 bucket name for published coverage artefacts. required: true artefact-bucket-endpoint: description: Endpoint URL used for S3-compatible uploads. required: true branch-name: description: Branch name used in the publication path. required: false default: '' repository-name: description: Repository name used in the publication path. required: false default: '' summary-file: description: Optional file path to append markdown summary output. required: false default: '' outputs: total: description: Computed coverage percentage. value: ${{ steps.generate.outputs.total }} report-url: description: Browser-facing URL for the published coverage report. value: ${{ steps.upload.outputs.report_url }} badge-url: description: Browser-facing URL for the published coverage badge. value: ${{ steps.upload.outputs.badge_url }} runs: using: composite steps: - name: Install AWS CLI v2 uses: ankurk91/install-aws-cli-action@v1 - name: Verify AWS CLI shell: bash run: aws --version - name: Generate coverage artefacts id: generate shell: bash env: COVERAGE_PROFILE: ${{ inputs.coverage-profile }} COVERAGE_HTML: ${{ inputs.coverage-html }} COVERAGE_BADGE: ${{ inputs.coverage-badge }} COVERAGE_SUMMARY: ${{ inputs.coverage-summary }} run: | set -euo pipefail go tool cover -html="$COVERAGE_PROFILE" -o "$COVERAGE_HTML" total="$(go tool cover -func="$COVERAGE_PROFILE" | awk '/^total:/ {sub(/%/, "", $3); print $3}')" printf '{\n "total": "%s"\n}\n' "$total" > "$COVERAGE_SUMMARY" printf 'total=%s\n' "$total" >> "$GITHUB_OUTPUT" color="$(awk -v total="$total" 'BEGIN { if (total >= 80) print "brightgreen"; else if (total >= 70) print "green"; else if (total >= 60) print "yellowgreen"; else if (total >= 50) print "yellow"; else print "red"; }')" cat > "$COVERAGE_BADGE" < coverage coverage ${total}% ${total}% EOF - name: Upload coverage artefacts id: upload shell: bash env: ARTEFACT_BUCKET_NAME: ${{ inputs.artefact-bucket-name }} ARTEFACT_BUCKET_ENDPONT: ${{ inputs.artefact-bucket-endpoint }} INPUT_BRANCH_NAME: ${{ inputs.branch-name }} INPUT_REPOSITORY_NAME: ${{ inputs.repository-name }} COVERAGE_HTML: ${{ inputs.coverage-html }} COVERAGE_BADGE: ${{ inputs.coverage-badge }} COVERAGE_SUMMARY: ${{ inputs.coverage-summary }} run: | set -euo pipefail aws configure set default.s3.addressing_style path branch_name="$(printf '%s' "$INPUT_BRANCH_NAME" | sed 's/^[[:space:]]\+//; s/[[:space:]]\+$//')" if [[ -z "$branch_name" ]]; then branch_name="${GITHUB_REF_NAME}" fi repo_name="$(printf '%s' "$INPUT_REPOSITORY_NAME" | sed 's/^[[:space:]]\+//; s/[[:space:]]\+$//')" if [[ -z "$repo_name" ]]; then repo_name="${GITHUB_REPOSITORY##*/}" fi prefix="${repo_name}/branch/${branch_name}" display_endpoint="${ARTEFACT_BUCKET_ENDPONT#https://}" display_endpoint="${display_endpoint#http://}" report_url="//${display_endpoint%/}/${ARTEFACT_BUCKET_NAME}/${prefix}/coverage.html" badge_url="//${display_endpoint%/}/${ARTEFACT_BUCKET_NAME}/${prefix}/coverage-badge.svg" aws --endpoint-url "${ARTEFACT_BUCKET_ENDPONT}" s3 cp "$COVERAGE_HTML" "s3://${ARTEFACT_BUCKET_NAME}/${prefix}/coverage.html" --content-type text/html aws --endpoint-url "${ARTEFACT_BUCKET_ENDPONT}" s3 cp "$COVERAGE_BADGE" "s3://${ARTEFACT_BUCKET_NAME}/${prefix}/coverage-badge.svg" --content-type image/svg+xml aws --endpoint-url "${ARTEFACT_BUCKET_ENDPONT}" s3 cp "$COVERAGE_SUMMARY" "s3://${ARTEFACT_BUCKET_NAME}/${prefix}/coverage-summary.json" --content-type application/json printf 'report_url=%s\n' "$report_url" >> "$GITHUB_OUTPUT" printf 'badge_url=%s\n' "$badge_url" >> "$GITHUB_OUTPUT" - name: Append coverage summary if: ${{ inputs.summary-file != '' }} shell: bash env: SUMMARY_FILE: ${{ inputs.summary-file }} TOTAL: ${{ steps.generate.outputs.total }} REPORT_URL: ${{ steps.upload.outputs.report_url }} BADGE_URL: ${{ steps.upload.outputs.badge_url }} run: | set -euo pipefail { echo '## Coverage' echo echo "- Total: \`${TOTAL}%\`" echo "- Report: ${REPORT_URL}" echo "- Badge: ${BADGE_URL}" } >> "$SUMMARY_FILE"