Skip to content

Commit e976501

Browse files
feat: add govulncheck gh workflows
Signed-off-by: AvineshTripathi <avineshtripathi1@gmail.com>
1 parent 4c42b79 commit e976501

File tree

4 files changed

+100
-1
lines changed

4 files changed

+100
-1
lines changed

.github/workflows/govulncheck.yml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
name: govulncheck
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
pull_request:
8+
9+
jobs:
10+
govulncheck:
11+
name: Run on Ubuntu
12+
runs-on: ubuntu-latest
13+
steps:
14+
- name: Clone the code
15+
uses: actions/checkout@v4
16+
with:
17+
# Fetch full history so git worktree can check out the base branch.
18+
fetch-depth: 0
19+
20+
- name: Setup Go
21+
uses: actions/setup-go@v5
22+
with:
23+
go-version-file: go.mod
24+
25+
- name: Install govulncheck
26+
run: go install golang.org/x/vuln/cmd/govulncheck@v1.1.4
27+
28+
- name: Run govulncheck
29+
# NRC_VERIFY_GIT_BRANCH tells the script which branch to use as the
30+
# base for comparison.
31+
env:
32+
NRC_VERIFY_GIT_BRANCH: ${{ github.base_ref || 'main' }}
33+
run: hack/verify-govulncheck.sh

Makefile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ CONTROLLER_GEN_BIN := controller-gen
6161
CONTROLLER_GEN := $(abspath $(TOOLS_BIN_DIR)/$(CONTROLLER_GEN_BIN)-$(CONTROLLER_GEN_VER))
6262
CONTROLLER_GEN_PKG := sigs.k8s.io/controller-tools/cmd/controller-gen
6363

64+
GOVULNCHECK_VER := v1.1.4
65+
GOVULNCHECK_BIN := govulncheck
66+
GOVULNCHECK := $(abspath $(TOOLS_BIN_DIR)/$(GOVULNCHECK_BIN)-$(GOVULNCHECK_VER))
67+
GOVULNCHECK_PKG := golang.org/x/vuln/cmd/govulncheck
68+
6469
# Image URL to use all building/pushing image targets
6570
IMG_PREFIX ?= controller
6671
IMG_TAG ?= latest
@@ -161,6 +166,10 @@ lint-api-fix: $(GOLANGCI_LINT_KAL)
161166
lint-config: $(GOLANGCI_LINT) ## Verify golangci-lint linter configuration
162167
$(GOLANGCI_LINT) config verify
163168

169+
.PHONY: govulncheck
170+
govulncheck: $(GOVULNCHECK) ## Run govulncheck to detect known vulnerabilities.
171+
$(GOVULNCHECK) -scan package ./...
172+
164173
.PHONY: verify
165174
verify: ## Run all verification scripts.
166175
./hack/verify-all.sh
@@ -450,6 +459,9 @@ $(GOLANGCI_LINT): # Build golangci-lint from tools folder.
450459
$(GOLANGCI_LINT_KAL): $(GOLANGCI_LINT) # Build golangci-lint-kal from custom configuration.
451460
cd $(TOOLS_DIR); $(GOLANGCI_LINT) custom
452461

462+
$(GOVULNCHECK): # Build govulncheck from tools folder.
463+
GOBIN=$(TOOLS_BIN_DIR) $(GO_INSTALL) $(GOVULNCHECK_PKG) $(GOVULNCHECK_BIN) $(GOVULNCHECK_VER)
464+
453465

454466
## --------------------------------------
455467
## Documentation

hack/verify-all.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@ set -o pipefail
2020

2121
# Run all verification scripts
2222
hack/verify-boilerplate.sh
23-
hack/verify-links.sh
23+
hack/verify-links.sh
24+
hack/verify-govulncheck.sh

hack/verify-govulncheck.sh

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/usr/bin/env bash
2+
3+
# Copyright The Kubernetes Authors.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
set -o errexit
18+
set -o nounset
19+
set -o pipefail
20+
21+
GOVULNCHECK_VERSION="${GOVULNCHECK_VERSION:-v1.1.4}"
22+
23+
# Install govulncheck if not already present.
24+
if ! command -v govulncheck &>/dev/null; then
25+
echo "Installing govulncheck@${GOVULNCHECK_VERSION}..."
26+
go install "golang.org/x/vuln/cmd/govulncheck@${GOVULNCHECK_VERSION}"
27+
fi
28+
29+
# NRC_VERIFY_GIT_BRANCH is populated in verify CI jobs (e.g. GITHUB_BASE_REF).
30+
BRANCH="${NRC_VERIFY_GIT_BRANCH:-main}"
31+
32+
# Create a temp directory and clean it up on exit.
33+
TMPDIR="$(mktemp -d)"
34+
trap 'rm -rf "${TMPDIR}"' EXIT
35+
36+
WORKTREE="${TMPDIR}/worktree"
37+
38+
echo "Creating worktree for base branch '${BRANCH}'..."
39+
git worktree add -f -q "${WORKTREE}" "${BRANCH}"
40+
trap 'git worktree remove -f "${WORKTREE}"; rm -rf "${TMPDIR}"' EXIT
41+
42+
echo "Running govulncheck on HEAD (PR branch)..."
43+
govulncheck -scan package ./... > "${TMPDIR}/head.txt" || true
44+
45+
echo "Running govulncheck on base branch '${BRANCH}'..."
46+
pushd "${WORKTREE}" >/dev/null
47+
govulncheck -scan package ./... > "${TMPDIR}/pr-base.txt" || true
48+
popd >/dev/null
49+
50+
echo -e "\n=== HEAD (PR branch) ===\n$(cat "${TMPDIR}/head.txt")"
51+
echo -e "\n=== BASE (${BRANCH}) ===\n$(cat "${TMPDIR}/pr-base.txt")"
52+
53+
diff -s -u --ignore-all-space "${TMPDIR}/pr-base.txt" "${TMPDIR}/head.txt" || true

0 commit comments

Comments
 (0)