ci: publish current coverage badges
Gitea/rust_realease/pipeline/head There was a failure building this commit Details

This commit is contained in:
Dandaedre 2026-10-03 03:07:45 +02:00
parent 70a5db9f76
commit e968370299
Signed by: Dandaedre
GPG Key ID: 440C6592E8A26D99
8 changed files with 418 additions and 6 deletions

1
.gitignore vendored
View File

@ -1,5 +1,6 @@
/target/
/dist/
/coverage/
/.venv/
.env
gitea_token.txt

106
Jenkinsfile vendored
View File

@ -6,6 +6,8 @@ pipeline {
environment {
CARGO_TERM_COLOR = 'never'
CARGO_TARPAULIN_VERSION = '0.37.5'
BADGE_PUBLISH_ROOT = '/mnt/assets/coverage'
}
stages {
@ -17,7 +19,41 @@ pipeline {
stage('Cargo Tests') {
steps {
sh 'cargo test --locked --all'
sh '''
set -eu
cargo test --locked --all
mkdir -p coverage
: > "coverage/.tests-passed-${BUILD_NUMBER}"
'''
}
}
stage('Coverage') {
steps {
sh '''
set -eu
installed_tarpaulin=$(cargo tarpaulin --version 2>/dev/null || true)
if [ "${installed_tarpaulin}" != "cargo-tarpaulin-tarpaulin ${CARGO_TARPAULIN_VERSION}" ]; then
cargo install cargo-tarpaulin --locked --version "${CARGO_TARPAULIN_VERSION}"
fi
cargo tarpaulin \
--config tarpaulin.toml \
--out Json \
--output-dir coverage
mv coverage/tarpaulin-report.json coverage/summary.json
coverage_percent=$(sed -n \
's/^.*],"coverage":\([0-9][0-9.]*\),"covered":[0-9][0-9]*,"coverable":[0-9][0-9]*}$/\1/p' \
coverage/summary.json)
scripts/render-coverage-badge.sh \
"${coverage_percent}" \
coverage/coverage.svg
rm coverage/summary.json
: > "coverage/.coverage-ready-${BUILD_NUMBER}"
'''
}
}
}
@ -27,8 +63,8 @@ pipeline {
publishChecks(
conclusion: 'SUCCESS',
name: 'Rust CI',
summary: 'Cargo tests passed.',
text: 'Rust tests passed without cross compilation.',
summary: 'Cargo tests, coverage, and badge generation passed.',
text: 'Rust tests and the enforced line-coverage gate passed without cross compilation; current badges were generated.',
title: 'CI Successful'
)
}
@ -37,8 +73,8 @@ pipeline {
publishChecks(
conclusion: 'FAILURE',
name: 'Rust CI',
summary: 'Cargo tests failed.',
text: 'Check the Jenkins log for the Cargo test failure.',
summary: 'Cargo tests, coverage, or badge generation failed.',
text: 'Check the Jenkins log for the Rust test, coverage, or badge-generation failure.',
title: 'CI Failed'
)
}
@ -52,5 +88,65 @@ pipeline {
title: 'CI Unstable'
)
}
cleanup {
sh '''
set -eu
mkdir -p coverage
test_status=failing
if [ -f "coverage/.tests-passed-${BUILD_NUMBER}" ]; then
test_status=passing
fi
scripts/render-test-badge.sh \
"${test_status}" \
coverage/test.svg
if [ ! -f "coverage/.coverage-ready-${BUILD_NUMBER}" ]; then
scripts/render-coverage-badge.sh - coverage/coverage.svg
fi
package_version=$(awk '
$0 == "[package]" { in_package = 1; next }
substr($0, 1, 1) == "[" { in_package = 0 }
in_package && $1 == "version" && $2 == "=" {
gsub(/^"|"$/, "", $3)
print $3
exit
}
' Cargo.toml)
if [ -z "${package_version}" ]; then
echo "could not determine package version" >&2
exit 2
fi
scripts/render-version-badge.sh \
"${package_version}" \
coverage/version.svg
rm -f \
coverage/summary.json \
coverage/tarpaulin-report.json \
"coverage/.tests-passed-${BUILD_NUMBER}" \
"coverage/.coverage-ready-${BUILD_NUMBER}"
'''
archiveArtifacts(
allowEmptyArchive: false,
artifacts: 'coverage/coverage.svg,coverage/test.svg,coverage/version.svg',
fingerprint: true
)
sh '''
set -eu
if [ "${BRANCH_NAME:-}" = "main" ]; then
scripts/publish-badges.sh \
"${BADGE_PUBLISH_ROOT}" \
coverage \
test \
version
fi
'''
}
}
}

View File

@ -1,5 +1,9 @@
# rust_realease
![version](https://assets.dandaedre.com/coverage/idle-org/rust_realease/main/version.svg)
![tests](https://assets.dandaedre.com/coverage/idle-org/rust_realease/main/test.svg)
![test coverage](https://assets.dandaedre.com/coverage/idle-org/rust_realease/main/coverage.svg)
`rust_realease` builds and publishes the binaries from a Rust package to a Gitea release. Give it a local project directory or a Git URL. It inspects Cargo metadata, checks the version tag, builds the selected targets with Cross, creates cargo-binstall-compatible `.tgz` archives, directly downloadable executables, or both, writes `SHA256SUMS`, uploads the assets, and verifies release downloads. It can also prepare a project for release with explicit commit, tag, and push options.
## Install
@ -139,6 +143,31 @@ cargo clippy --all-targets --all-features -- -D warnings
cargo test --all
```
Jenkins runs Cargo tests only. Cross builds and live Gitea releases are separate release checks. The small [target_test_project fixture](https://proxy.dandaedre.com/gitea/idle-org/target_test_project) is maintained in its own repository for fast target matrix experiments. Clone it separately when needed; it is not part of this checkout.
Jenkins runs Cargo tests and enforces a 65% line-coverage floor. Run the exact
coverage command locally with:
```bash
cargo tarpaulin --config tarpaulin.toml --out Json --output-dir coverage
```
The current suite measures 66.12% (1,046/1,582 lines). `Cargo.toml` is the
authoritative version source. Jenkins temporarily renames Tarpaulin's JSON to
`coverage/summary.json`, extracts and validates the overall percentage, renders
badges locally, then removes the summary. It archives only
`coverage/version.svg`, `coverage/test.svg`, and `coverage/coverage.svg`, with
fingerprinting. Successful `main` builds publish those files atomically under
`/mnt/assets/coverage/idle-org/rust_realease/main/`, serving:
- `https://assets.dandaedre.com/coverage/idle-org/rust_realease/main/version.svg`
- `https://assets.dandaedre.com/coverage/idle-org/rust_realease/main/test.svg`
- `https://assets.dandaedre.com/coverage/idle-org/rust_realease/main/coverage.svg`
The asset server must mount published assets read-only, disable directory
listings, serve SVG with a restrictive content security policy, and revalidate
these mutable URLs or use a short cache lifetime. Cross builds and live Gitea
releases remain separate release checks. The small
[target_test_project fixture](https://proxy.dandaedre.com/gitea/idle-org/target_test_project)
is maintained in its own repository for fast target matrix experiments. Clone
it separately when needed; it is not part of this checkout.
With `cargo-binstall` installed, run the opt-in isolated install fixture for all three targets with `cargo test real_binstall_installs_local_release_fixture -- --ignored`.

83
scripts/publish-badges.sh Executable file
View File

@ -0,0 +1,83 @@
#!/bin/sh
set -eu
usage() {
echo "usage: $0 <badge-publication-root> <badge> [badge...]" >&2
echo "badges: coverage test version" >&2
exit 2
}
[ "$#" -ge 2 ] || usage
publication_root=$1
shift
case "$publication_root" in
/*) ;;
*)
echo "badge publication root must be an absolute path: $publication_root" >&2
exit 2
;;
esac
if [ ! -d "$publication_root" ] || [ ! -w "$publication_root" ]; then
echo "badge publication root must be an existing writable directory: $publication_root" >&2
exit 2
fi
selected=' '
for badge in "$@"; do
case "$badge" in
coverage|test|version) ;;
*)
echo "unsupported badge: $badge" >&2
exit 2
;;
esac
case "$selected" in
*" $badge "*)
echo "badge selected more than once: $badge" >&2
exit 2
;;
esac
selected="$selected$badge "
source="coverage/$badge.svg"
if [ ! -f "$source" ] || [ ! -s "$source" ] || [ -L "$source" ]; then
echo "required badge must be a regular nonempty file, not a symlink: $source" >&2
exit 2
fi
done
owner_root="$publication_root/idle-org"
project_root="$owner_root/rust_realease"
badge_root="$project_root/main"
for directory in "$owner_root" "$project_root"; do
if [ -L "$directory" ] || { [ -e "$directory" ] && [ ! -d "$directory" ]; }; then
echo "badge destination component is not a directory: $directory" >&2
exit 2
fi
mkdir -p "$directory"
chmod 0755 "$directory"
done
if [ -L "$badge_root" ]; then
unlink "$badge_root"
elif [ -e "$badge_root" ] && [ ! -d "$badge_root" ]; then
echo "badge destination is not a directory: $badge_root" >&2
exit 2
fi
mkdir -p "$badge_root"
chmod 0755 "$badge_root"
temporary=
trap 'if [ -n "$temporary" ]; then rm -f "$temporary"; fi' EXIT HUP INT TERM
for badge in "$@"; do
temporary=$(mktemp "$badge_root/.$badge.svg.XXXXXX")
cp "coverage/$badge.svg" "$temporary"
chmod 0644 "$temporary"
mv -f "$temporary" "$badge_root/$badge.svg"
temporary=
echo "published $badge badge to $badge_root/$badge.svg"
done
trap - EXIT HUP INT TERM

View File

@ -0,0 +1,69 @@
#!/bin/sh
set -eu
usage() {
echo "usage: $0 <line-coverage-percent> <output.svg>" >&2
exit 2
}
[ "$#" -eq 2 ] || usage
percent=$1
output=$2
if [ "$percent" = "-" ]; then
rounded=-
color='#9f9f9f'
else
if ! awk -v value="$percent" 'BEGIN {
valid = value ~ /^[0-9]+([.][0-9]+)?$/ && value >= 0 && value <= 100
exit(valid ? 0 : 1)
}'; then
echo "coverage percentage must be - or a number from 0 through 100: $percent" >&2
exit 2
fi
rounded=$(awk -v value="$percent" 'BEGIN { printf "%.0f", value }')
if [ "$rounded" -ge 90 ]; then
color='#4c1'
elif [ "$rounded" -ge 70 ]; then
color='#dfb317'
else
color='#e05d44'
fi
fi
output_dir=${output%/*}
if [ "$output_dir" != "$output" ]; then
mkdir -p "$output_dir"
fi
temporary="${output}.tmp.$$"
trap 'rm -f "$temporary"' EXIT HUP INT TERM
sed \
-e "s/@PERCENT@/${rounded}/g" \
-e "s/@COLOR@/${color}/g" \
>"$temporary" <<'EOF'
<svg xmlns="http://www.w3.org/2000/svg" width="112" height="20" role="img" aria-label="coverage: @PERCENT@%">
<title>coverage: @PERCENT@%</title>
<linearGradient id="s" x2="0" y2="100%">
<stop offset="0" stop-color="#fff" stop-opacity=".7"/>
<stop offset=".1" stop-color="#aaa" stop-opacity=".1"/>
<stop offset=".9" stop-opacity=".3"/>
<stop offset="1" stop-opacity=".5"/>
</linearGradient>
<clipPath id="r"><rect width="112" height="20" rx="3"/></clipPath>
<g clip-path="url(#r)">
<rect width="75" height="20" fill="#555"/>
<rect x="75" width="37" height="20" fill="@COLOR@"/>
<rect width="112" height="20" fill="url(#s)"/>
</g>
<g fill="#fff" text-anchor="middle" dominant-baseline="middle" font-family="Verdana,Geneva,DejaVu Sans,sans-serif" font-size="11">
<text x="37.5" y="10">coverage</text>
<text x="93.5" y="10">@PERCENT@%</text>
</g>
</svg>
EOF
mv "$temporary" "$output"
trap - EXIT HUP INT TERM

56
scripts/render-test-badge.sh Executable file
View File

@ -0,0 +1,56 @@
#!/bin/sh
set -eu
usage() {
echo "usage: $0 <passing|failing> <output.svg>" >&2
exit 2
}
[ "$#" -eq 2 ] || usage
status=$1
output=$2
case "$status" in
passing) color='#4c1' ;;
failing) color='#e05d44' ;;
*)
echo "test badge status must be passing or failing: $status" >&2
exit 2
;;
esac
output_dir=${output%/*}
if [ "$output_dir" != "$output" ]; then
mkdir -p "$output_dir"
fi
temporary="${output}.tmp.$$"
trap 'rm -f "$temporary"' EXIT HUP INT TERM
sed \
-e "s/@STATUS@/${status}/g" \
-e "s/@COLOR@/${color}/g" \
>"$temporary" <<'EOF'
<svg xmlns="http://www.w3.org/2000/svg" width="100" height="20" role="img" aria-label="tests: @STATUS@">
<title>tests: @STATUS@</title>
<linearGradient id="s" x2="0" y2="100%">
<stop offset="0" stop-color="#fff" stop-opacity=".7"/>
<stop offset=".1" stop-color="#aaa" stop-opacity=".1"/>
<stop offset=".9" stop-opacity=".3"/>
<stop offset="1" stop-opacity=".5"/>
</linearGradient>
<clipPath id="r"><rect width="100" height="20" rx="3"/></clipPath>
<g clip-path="url(#r)">
<rect width="45" height="20" fill="#555"/>
<rect x="45" width="55" height="20" fill="@COLOR@"/>
<rect width="100" height="20" fill="url(#s)"/>
</g>
<g fill="#fff" text-anchor="middle" dominant-baseline="middle" font-family="Verdana,Geneva,DejaVu Sans,sans-serif" font-size="11">
<text x="22.5" y="10">tests</text>
<text x="72.5" y="10">@STATUS@</text>
</g>
</svg>
EOF
mv "$temporary" "$output"
trap - EXIT HUP INT TERM

65
scripts/render-version-badge.sh Executable file
View File

@ -0,0 +1,65 @@
#!/bin/sh
set -eu
usage() {
echo "usage: $0 <package-version> <output.svg>" >&2
exit 2
}
[ "$#" -eq 2 ] || usage
version=$1
output=$2
case "$version" in
''|*[!0-9A-Za-z.+-]*)
echo "package version contains unsupported badge characters: $version" >&2
exit 2
;;
esac
value_width=$(awk -v value="$version" 'BEGIN {
width = length(value) * 7 + 10
print (width < 45 ? 45 : width)
}')
badge_width=$((59 + value_width))
value_center=$(awk -v width="$value_width" 'BEGIN {
printf "%.1f", 59 + width / 2
}')
output_dir=${output%/*}
if [ "$output_dir" != "$output" ]; then
mkdir -p "$output_dir"
fi
temporary="${output}.tmp.$$"
trap 'rm -f "$temporary"' EXIT HUP INT TERM
sed \
-e "s/@VERSION@/${version}/g" \
-e "s/@BADGE_WIDTH@/${badge_width}/g" \
-e "s/@VALUE_WIDTH@/${value_width}/g" \
-e "s/@VALUE_CENTER@/${value_center}/g" \
>"$temporary" <<'EOF'
<svg xmlns="http://www.w3.org/2000/svg" width="@BADGE_WIDTH@" height="20" role="img" aria-label="version: @VERSION@">
<title>version: @VERSION@</title>
<linearGradient id="s" x2="0" y2="100%">
<stop offset="0" stop-color="#fff" stop-opacity=".7"/>
<stop offset=".1" stop-color="#aaa" stop-opacity=".1"/>
<stop offset=".9" stop-opacity=".3"/>
<stop offset="1" stop-opacity=".5"/>
</linearGradient>
<clipPath id="r"><rect width="@BADGE_WIDTH@" height="20" rx="3"/></clipPath>
<g clip-path="url(#r)">
<rect width="59" height="20" fill="#555"/>
<rect x="59" width="@VALUE_WIDTH@" height="20" fill="#007ec6"/>
<rect width="@BADGE_WIDTH@" height="20" fill="url(#s)"/>
</g>
<g fill="#fff" text-anchor="middle" dominant-baseline="middle" font-family="Verdana,Geneva,DejaVu Sans,sans-serif" font-size="11">
<text x="29.5" y="10">version</text>
<text x="@VALUE_CENTER@" y="10">@VERSION@</text>
</g>
</svg>
EOF
mv "$temporary" "$output"
trap - EXIT HUP INT TERM

13
tarpaulin.toml Normal file
View File

@ -0,0 +1,13 @@
[coverage]
all-features = true
engine = "Llvm"
fail-under = 65.0
locked = true
run-types = ["Tests"]
workspace = true
[report]
out = ["Stdout"]
# This floor stays below the measured suite result without excluding production
# code or manufacturing tests for process-only and platform failure paths.