Skip to content

Commit 950873d

Browse files
authored
🌱 Derive the reported Scorecard version from go.mod (#1718)
The Makefile hardcoded the Scorecard version and commit the action reports, so every Scorecard bump had to edit them by hand. The v5.6.0 candidate bump missed it, and the image has been reporting v5.5.0 while running newer code. scripts/version-ldflags reads the github.com/ossf/scorecard/v5 requirement from go.mod and its commit from the module proxy, falling back to the commit in a pseudo-version, and fails the build if either is missing rather than building without a version. Assisted-by: LLM Signed-off-by: Stephen Augustus <[email protected]>
1 parent 2ce8ee4 commit 950873d

2 files changed

Lines changed: 45 additions & 5 deletions

File tree

‎Makefile‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
# NOTE: Keep this in sync with go.mod for ossf/scorecard.
2-
LDFLAGS=-X sigs.k8s.io/release-utils/version.gitVersion=v5.5.0 -X sigs.k8s.io/release-utils/version.gitCommit=c395761df6afe1a69e476bc60a013a94bcbc153f -w -extldflags \"-static\"
3-
41
build: ## Runs go build on repo
5-
# Run go build and generate scorecard executable
6-
CGO_ENABLED=0 go build -o scorecard-action -trimpath -a -tags netgo -buildvcs=false -ldflags '$(LDFLAGS)'
2+
# Run go build and generate scorecard executable. The Scorecard version it
3+
# reports comes from go.mod, via scripts/version-ldflags.
4+
version_ldflags=$$(./scripts/version-ldflags) && \
5+
CGO_ENABLED=0 go build -o scorecard-action -trimpath -a -tags netgo -buildvcs=false -ldflags "$$version_ldflags -w -extldflags \"-static\""

‎scripts/version-ldflags‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/usr/bin/env bash
2+
# Copyright 2026 OpenSSF Scorecard Authors
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
# Prints the -ldflags that set the Scorecard version and commit the action
17+
# reports, derived from the github.com/ossf/scorecard/v5 requirement in go.mod.
18+
set -euo pipefail
19+
20+
module=github.com/ossf/scorecard/v5
21+
22+
version=$(go list -m -f '{{.Version}}' "${module}")
23+
if [[ -z "${version}" ]]; then
24+
echo "version-ldflags: no version for ${module} in go.mod" >&2
25+
exit 1
26+
fi
27+
28+
# The module proxy reports the commit for both tags and pseudo-versions. A
29+
# module cache populated before that was recorded can omit it, so fall back to
30+
# the commit encoded in a pseudo-version.
31+
commit=$(go mod download -json "${module}@${version}" |
32+
sed -n 's/^[[:space:]]*"Hash": "\([0-9a-f]\{40\}\)",\{0,1\}$/\1/p' | head -n 1)
33+
if [[ -z "${commit}" && "${version}" =~ -([0-9a-f]{12})$ ]]; then
34+
commit="${BASH_REMATCH[1]}"
35+
fi
36+
if [[ -z "${commit}" ]]; then
37+
echo "version-ldflags: no commit for ${module}@${version}" >&2
38+
exit 1
39+
fi
40+
41+
echo "-X sigs.k8s.io/release-utils/version.gitVersion=${version} -X sigs.k8s.io/release-utils/version.gitCommit=${commit}"

0 commit comments

Comments
 (0)