From ab773cb50a874484073a98429e4ffa920d3d6d2e Mon Sep 17 00:00:00 2001 From: Carlos Eduardo Arango Gutierrez Date: Thu, 4 Apr 2024 13:08:50 +0200 Subject: [PATCH] Add GitHub actions Signed-off-by: Carlos Eduardo Arango Gutierrez --- .github/dependabot.yml | 17 +++++-- .github/workflows/golang.yml | 80 ++++++++++++++++++++++++++++++++ .github/workflows/pre-sanity.yml | 22 --------- Makefile | 53 +++++++++------------ versions.mk | 42 +++++++++++++++++ 5 files changed, 156 insertions(+), 58 deletions(-) create mode 100644 .github/workflows/golang.yml delete mode 100644 .github/workflows/pre-sanity.yml create mode 100644 versions.mk diff --git a/.github/dependabot.yml b/.github/dependabot.yml index 45180dc..f2b6fce 100644 --- a/.github/dependabot.yml +++ b/.github/dependabot.yml @@ -9,14 +9,23 @@ updates: schedule: interval: "weekly" day: "sunday" - labels: - - area/dependency - ignore: - - dependency-name: k8s.io/* labels: - dependencies + groups: + k8sio: + patterns: + - k8s.io/* + exclude-patterns: + - k8s.io/klog/* - package-ecosystem: "github-actions" directory: "/" schedule: interval: "daily" + + - package-ecosystem: "github-actions" + target-branch: gh-pages + directory: "/" + schedule: + interval: "weekly" + day: "monday" diff --git a/.github/workflows/golang.yml b/.github/workflows/golang.yml new file mode 100644 index 0000000..60b0df2 --- /dev/null +++ b/.github/workflows/golang.yml @@ -0,0 +1,80 @@ +# Copyright 2024 NVIDIA CORPORATION +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +name: Golang + +on: + pull_request: + branches: + - main + - release-* + push: + branches: + - main + - release-* + +jobs: + check: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + name: Checkout code + - name: Get Golang version + id: vars + run: | + GOLANG_VERSION=$( grep "GOLANG_VERSION ?=" versions.mk ) + echo "GOLANG_VERSION=${GOLANG_VERSION##GOLANG_VERSION ?= }" >> $GITHUB_ENV + - name: Install Go + uses: actions/setup-go@v5 + with: + go-version: ${{ env.GOLANG_VERSION }} + - name: Lint + uses: golangci/golangci-lint-action@v4 + with: + version: latest + args: -v --timeout 5m + skip-cache: true + - name: Check golang modules + run: make check-vendor + test: + name: Unit test + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v4 + - name: Get Golang version + id: vars + run: | + GOLANG_VERSION=$( grep "GOLANG_VERSION ?=" versions.mk ) + echo "GOLANG_VERSION=${GOLANG_VERSION##GOLANG_VERSION ?= }" >> $GITHUB_ENV + - name: Install Go + uses: actions/setup-go@v5 + with: + go-version: ${{ env.GOLANG_VERSION }} + - run: make test + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + name: Checkout code + - name: Get Golang version + id: vars + run: | + GOLANG_VERSION=$( grep "GOLANG_VERSION ?=" versions.mk ) + echo "GOLANG_VERSION=${GOLANG_VERSION##GOLANG_VERSION ?= }" >> $GITHUB_ENV + - name: Install Go + uses: actions/setup-go@v5 + with: + go-version: ${{ env.GOLANG_VERSION }} + - run: make build diff --git a/.github/workflows/pre-sanity.yml b/.github/workflows/pre-sanity.yml deleted file mode 100644 index 15ccaf2..0000000 --- a/.github/workflows/pre-sanity.yml +++ /dev/null @@ -1,22 +0,0 @@ -name: Run pre sanity - -# run this workflow for each commit -on: [pull_request] - -jobs: - build: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v4 - - - name: Build dev image - run: make .build-image - - - name: Build - run: make docker-build - - - name: Tests - run: make docker-coverage - - - name: Checks - run: make docker-check diff --git a/Makefile b/Makefile index bfae86b..248cc55 100644 --- a/Makefile +++ b/Makefile @@ -12,18 +12,10 @@ # See the License for the specific language governing permissions and # limitations under the License. -MODULE := github.com/NVIDIA/go-nvlib +include versions.mk DOCKER ?= docker -GOLANG_VERSION := 1.21.1 - -ifeq ($(IMAGE),) -REGISTRY ?= nvidia -IMAGE=$(REGISTRY)/go-nvlib -endif -IMAGE_TAG ?= $(GOLANG_VERSION) -BUILDIMAGE ?= $(IMAGE):$(IMAGE_TAG)-devel PCI_IDS_URL ?= https://pci-ids.ucw.cz/v2.2/pci.ids TARGETS := binary build all check fmt assert-fmt generate lint vet test coverage @@ -38,6 +30,9 @@ build: all: check build binary check: assert-fmt lint vet +check-vendor: vendor + git diff --quiet HEAD -- go.mod go.sum vendor + # Apply go fmt to the codebase fmt: go list -f '{{.Dir}}' $(MODULE)/... \ @@ -76,32 +71,26 @@ coverage: test update-pcidb: wget $(PCI_IDS_URL) -O $(CURDIR)/pkg/pciids/default_pci.ids -# Generate an image for containerized builds -# Note: This image is local only -.PHONY: .build-image .pull-build-image .push-build-image -.build-image: docker/Dockerfile.devel - if [ "$(SKIP_IMAGE_BUILD)" = "" ]; then \ - $(DOCKER) build \ - --progress=plain \ - --build-arg GOLANG_VERSION="$(GOLANG_VERSION)" \ - --tag $(BUILDIMAGE) \ - -f $(^) \ - docker; \ - fi +build-image: $(DOCKERFILE_DEVEL) + $(DOCKER) build \ + --progress=plain \ + --build-arg GOLANG_VERSION="$(GOLANG_VERSION)" \ + --build-arg CLIENT_GEN_VERSION="$(CLIENT_GEN_VERSION)" \ + --build-arg CONTROLLER_GEN_VERSION="$(CONTROLLER_GEN_VERSION)" \ + --build-arg GOLANGCI_LINT_VERSION="$(GOLANGCI_LINT_VERSION)" \ + --build-arg MOQ_VERSION="$(MOQ_VERSION)" \ + --tag $(BUILDIMAGE) \ + -f $(DOCKERFILE_DEVEL) \ + . -.pull-build-image: - $(DOCKER) pull $(BUILDIMAGE) - -.push-build-image: - $(DOCKER) push $(BUILDIMAGE) - -$(DOCKER_TARGETS): docker-%: .build-image - @echo "Running 'make $(*)' in docker container $(BUILDIMAGE)" +$(DOCKER_TARGETS): docker-%: + @echo "Running 'make $(*)' in container image $(BUILDIMAGE)" $(DOCKER) run \ --rm \ - -e GOCACHE=/tmp/.cache \ - -v $(PWD):$(PWD) \ - -w $(PWD) \ + -e GOCACHE=/tmp/.cache/go \ + -e GOMODCACHE=/tmp/.cache/gomod \ + -v $(PWD):/work \ + -w /work \ --user $$(id -u):$$(id -g) \ $(BUILDIMAGE) \ make $(*) diff --git a/versions.mk b/versions.mk new file mode 100644 index 0000000..f6c83d3 --- /dev/null +++ b/versions.mk @@ -0,0 +1,42 @@ +# Copyright (c) NVIDIA CORPORATION. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +MODULE := github.com/NVIDIA/go-nvlib + +GIT_COMMIT ?= $(shell git describe --match="" --dirty --long --always --abbrev=40 2> /dev/null || echo "") +GIT_TAG ?= $(patsubst v%,%,$(shell git describe --tags 2>/dev/null)) +PARTS := $(subst -, ,$(GIT_TAG)) +VERSION ?= $(word 1,$(PARTS)) + +# vVERSION represents the version with a guaranteed v-prefix +vVERSION := v$(VERSION:v%=%) + +GOLANG_VERSION ?= 1.21.5 + +ifeq ($(IMAGE),) +REGISTRY ?= nvidia +IMAGE=$(REGISTRY)/go-nvlib +endif +IMAGE_TAG ?= $(GOLANG_VERSION) + +# these variables are only needed when building a local image +# by default, the k8s-test-infra image is used +CLIENT_GEN_VERSION ?= v0.26.1 +CONTROLLER_GEN_VERSION ?= v0.9.2 +GOLANGCI_LINT_VERSION ?= v1.52.0 +MOQ_VERSION ?= v0.3.4 + +BUILDIMAGE ?= ghcr.io/nvidia/k8s-test-infra:devel-go$(GOLANG_VERSION) +DOCKERFILE_DEVEL := "images/devel/Dockerfile" +K8S_TEST_INFRA := "https://github.com/NVIDIA/k8s-test-infra.git"