From 1c762c6802b391ef2f3346886629bb0c1d514e24 Mon Sep 17 00:00:00 2001 From: Enver Bisevac Date: Fri, 6 Oct 2023 22:53:48 +0000 Subject: [PATCH] [maint] add docker buildx support (#661) --- .vscode/launch.json | 2 +- Dockerfile | 26 ++++++++++++------- Makefile | 13 +--------- cmd/gitness/driver_pq.go | 3 --- cmd/gitness/driver_sqlite.go | 4 +-- .../{util_pq.go => util_no_sqlite.go} | 4 +-- store/database/util_sqlite.go | 10 +++++-- web/dist.go | 3 --- 8 files changed, 31 insertions(+), 34 deletions(-) rename store/database/{util_pq.go => util_no_sqlite.go} (95%) diff --git a/.vscode/launch.json b/.vscode/launch.json index c49acb16ad..2d47006e70 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -9,7 +9,7 @@ "type": "go", "request": "launch", "mode": "auto", - "buildFlags": "-tags=sqlite", + "buildFlags": "-tags=gogit", "program": "cmd/gitness", "args": ["server", "../../.local.env"] } diff --git a/Dockerfile b/Dockerfile index a96845b7ef..36220a0479 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,7 +1,7 @@ # ---------------------------------------------------------# # Build web image # # ---------------------------------------------------------# -FROM node:16 as web +FROM --platform=$BUILDPLATFORM node:16 as web WORKDIR /usr/src/app @@ -18,14 +18,13 @@ RUN yarn && yarn build && yarn cache clean # ---------------------------------------------------------# # Build gitness image # # ---------------------------------------------------------# -FROM golang:1.19-alpine as builder +FROM --platform=$BUILDPLATFORM golang:1.19-alpine as builder RUN apk update \ && apk add --no-cache protoc build-base git # Setup workig dir WORKDIR /app - RUN git config --global --add safe.directory '/app' # Get dependancies - will also be cached if we won't change mod/sum @@ -45,22 +44,31 @@ ARG GIT_COMMIT ARG GITNESS_VERSION_MAJOR ARG GITNESS_VERSION_MINOR ARG GITNESS_VERSION_PATCH -ARG BUILD_TAGS +ARG TARGETOS TARGETARCH + +RUN if [ "$TARGETARCH" = "arm64" ]; then \ + wget -P ~ https://musl.cc/aarch64-linux-musl-cross.tgz && \ + tar -xvf ~/aarch64-linux-musl-cross.tgz -C ~ ; \ + fi # set required build flags -RUN CGO_ENABLED=1 \ - BUILD_TAGS=${BUILD_TAGS} \ - make build +RUN --mount=type=cache,target=/root/.cache/go-build \ + --mount=type=cache,target=/go/pkg \ + if [ "$TARGETARCH" = "arm64" ]; then CC=~/aarch64-linux-musl-cross/bin/aarch64-linux-musl-gcc; fi && \ + LDFLAGS="-X github.com/harness/gitness/version.GitCommit=${GIT_COMMIT} -X github.com/harness/gitness/version.major=${GITNESS_VERSION_MAJOR} -X github.com/harness/gitness/version.minor=${GITNESS_VERSION_MINOR} -X github.com/harness/gitness/version.patch=${GITNESS_VERSION_PATCH} -extldflags '-static'" && \ + CGO_ENABLED=1 \ + GOOS=$TARGETOS GOARCH=$TARGETARCH \ + CC=$CC go build -tags=gogit -ldflags="$LDFLAGS" -o ./gitness ./cmd/gitness ### Pull CA Certs -FROM alpine:latest as cert-image +FROM --platform=$BUILDPLATFORM alpine:latest as cert-image RUN apk --update add ca-certificates # ---------------------------------------------------------# # Create final image # # ---------------------------------------------------------# -FROM alpine/git:2.40.1 as final +FROM --platform=$BUILDPLATFORM alpine/git:2.40.1 as final # setup app dir and its content WORKDIR /app diff --git a/Makefile b/Makefile index b8d7175922..9ef2be281f 100644 --- a/Makefile +++ b/Makefile @@ -8,8 +8,6 @@ endif tools = $(addprefix $(GOBIN)/, golangci-lint goimports govulncheck protoc-gen-go protoc-gen-go-grpc gci) deps = $(addprefix $(GOBIN)/, wire dbmate) -LDFLAGS = "-X github.com/harness/gitness/version.GitCommit=${GIT_COMMIT} -X github.com/harness/gitness/version.major=${GITNESS_VERSION_MAJOR} -X github.com/harness/gitness/version.minor=${GITNESS_VERSION_MINOR} -X github.com/harness/gitness/version.patch=${GITNESS_VERSION_PATCH}" - ifneq (,$(wildcard ./.local.env)) include ./.local.env export @@ -17,12 +15,6 @@ endif .DEFAULT_GOAL := all -ifeq ($(BUILD_TAGS),) - BUILD_TAGS := sqlite -endif - -BUILD_TAGS := $(BUILD_TAGS),gogit - ############################################################################### # # Initialization @@ -48,16 +40,13 @@ tools: $(tools) ## Install tools required for the build build: generate ## Build the all-in-one gitness binary @echo "Building Gitness Server" - go build -tags=${BUILD_TAGS} -ldflags=${LDFLAGS} -o ./gitness ./cmd/gitness + go build -tags=gogit -o ./gitness ./cmd/gitness test: generate ## Run the go tests @echo "Running tests" go test -v -coverprofile=coverage.out ./... go tool cover -html=coverage.out -run: dep ## Run the gitness binary from source - @go run -race -ldflags=${LDFLAGS} ./cmd/gitness - ############################################################################### # # Code Formatting and linting diff --git a/cmd/gitness/driver_pq.go b/cmd/gitness/driver_pq.go index 6b5c095e8d..b67d8a185e 100644 --- a/cmd/gitness/driver_pq.go +++ b/cmd/gitness/driver_pq.go @@ -12,9 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -//go:build pq -// +build pq - package main import ( diff --git a/cmd/gitness/driver_sqlite.go b/cmd/gitness/driver_sqlite.go index 994d3c2c87..7202eebf89 100644 --- a/cmd/gitness/driver_sqlite.go +++ b/cmd/gitness/driver_sqlite.go @@ -12,8 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. -//go:build sqlite -// +build sqlite +//go:build !nosqlite +// +build !nosqlite package main diff --git a/store/database/util_pq.go b/store/database/util_no_sqlite.go similarity index 95% rename from store/database/util_pq.go rename to store/database/util_no_sqlite.go index 1337a14a30..3405713e06 100644 --- a/store/database/util_pq.go +++ b/store/database/util_no_sqlite.go @@ -12,8 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. -//go:build pq -// +build pq +//go:build nosqlite +// +build nosqlite package database diff --git a/store/database/util_sqlite.go b/store/database/util_sqlite.go index a796fb5484..62fe04bbab 100644 --- a/store/database/util_sqlite.go +++ b/store/database/util_sqlite.go @@ -12,12 +12,13 @@ // See the License for the specific language governing permissions and // limitations under the License. -//go:build !pq -// +build !pq +//go:build !nosqlite +// +build !nosqlite package database import ( + "github.com/lib/pq" "github.com/mattn/go-sqlite3" "github.com/pkg/errors" ) @@ -29,5 +30,10 @@ func isSQLUniqueConstraintError(original error) bool { errors.Is(sqliteErr.ExtendedCode, sqlite3.ErrConstraintPrimaryKey) } + var pqErr *pq.Error + if errors.As(original, &pqErr) { + return pqErr.Code == "23505" // unique_violation + } + return false } diff --git a/web/dist.go b/web/dist.go index 9ea68bd84d..29ddfe28ea 100644 --- a/web/dist.go +++ b/web/dist.go @@ -12,9 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -//go:build !proxy -// +build !proxy - // Package dist embeds the static web server content. package web