Skip to content

Commit

Permalink
[maint] add docker buildx support (#661)
Browse files Browse the repository at this point in the history
  • Loading branch information
enver-bisevac authored and Harness committed Oct 6, 2023
1 parent edadd5e commit 1c762c6
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 34 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Expand Up @@ -9,7 +9,7 @@
"type": "go",
"request": "launch",
"mode": "auto",
"buildFlags": "-tags=sqlite",
"buildFlags": "-tags=gogit",
"program": "cmd/gitness",
"args": ["server", "../../.local.env"]
}
Expand Down
26 changes: 17 additions & 9 deletions Dockerfile
@@ -1,7 +1,7 @@
# ---------------------------------------------------------#
# Build web image #
# ---------------------------------------------------------#
FROM node:16 as web
FROM --platform=$BUILDPLATFORM node:16 as web

WORKDIR /usr/src/app

Expand All @@ -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
Expand All @@ -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
Expand Down
13 changes: 1 addition & 12 deletions Makefile
Expand Up @@ -8,21 +8,13 @@ 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
endif

.DEFAULT_GOAL := all

ifeq ($(BUILD_TAGS),)
BUILD_TAGS := sqlite
endif

BUILD_TAGS := $(BUILD_TAGS),gogit

###############################################################################
#
# Initialization
Expand All @@ -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
Expand Down
3 changes: 0 additions & 3 deletions cmd/gitness/driver_pq.go
Expand Up @@ -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 (
Expand Down
4 changes: 2 additions & 2 deletions cmd/gitness/driver_sqlite.go
Expand Up @@ -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

Expand Down
Expand Up @@ -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

Expand Down
10 changes: 8 additions & 2 deletions store/database/util_sqlite.go
Expand Up @@ -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"
)
Expand All @@ -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
}
3 changes: 0 additions & 3 deletions web/dist.go
Expand Up @@ -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

Expand Down

0 comments on commit 1c762c6

Please sign in to comment.