Skip to content
This repository was archived by the owner on Dec 16, 2022. It is now read-only.

Commit dd0c896

Browse files
committed
add Dockerfiles to build libgit2-only images
Signed-off-by: Sanskar Jaiswal <jaiswalsanskar078@gmail.com>
1 parent c46f1ac commit dd0c896

File tree

4 files changed

+232
-6
lines changed

4 files changed

+232
-6
lines changed

Dockerfile.libgit2-only

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# This Dockerfile tests the hack/Makefile output against git2go.
2+
ARG BASE_VARIANT=alpine
3+
ARG GO_VERSION=1.17
4+
ARG XX_VERSION=1.1.0
5+
6+
FROM --platform=$BUILDPLATFORM tonistiigi/xx:${XX_VERSION} AS xx
7+
8+
FROM --platform=$BUILDPLATFORM ${BASE_VARIANT} AS build-base
9+
10+
RUN apk add --no-cache \
11+
bash \
12+
curl \
13+
build-base \
14+
linux-headers \
15+
perl \
16+
cmake \
17+
pkgconfig \
18+
gcc \
19+
musl-dev \
20+
clang \
21+
lld
22+
23+
COPY --from=xx / /
24+
25+
FROM build-base AS build-cross
26+
27+
ARG TARGETPLATFORM
28+
29+
RUN xx-apk add --no-cache \
30+
build-base \
31+
pkgconfig \
32+
gcc \
33+
musl-dev \
34+
clang \
35+
lld \
36+
llvm \
37+
linux-headers
38+
39+
WORKDIR /build
40+
COPY hack/static.sh .
41+
42+
ENV CC=xx-clang
43+
ENV CXX=xx-clang++
44+
45+
RUN ./static.sh build_libgit2_only
46+
47+
# trimmed removes all non necessary files (i.e. openssl binary).
48+
FROM build-cross AS trimmed
49+
50+
ARG TARGETPLATFORM
51+
RUN mkdir -p /trimmed/usr/local/$(xx-info triple)/ && \
52+
mkdir -p /trimmed/usr/local/$(xx-info triple)/share
53+
54+
RUN cp -r /usr/local/$(xx-info triple)/lib/ /trimmed/usr/local/$(xx-info triple)/ && \
55+
cp -r /usr/local/$(xx-info triple)/include/ /trimmed/usr/local/$(xx-info triple)/
56+
57+
FROM scratch as libs-arm64
58+
COPY --from=trimmed /trimmed/ /
59+
60+
FROM scratch as libs-amd64
61+
COPY --from=trimmed /trimmed/ /
62+
63+
FROM scratch as libs-armv7
64+
COPY --from=trimmed /trimmed/ /
65+
66+
FROM libs-$TARGETARCH$TARGETVARIANT as libs

Dockerfile.test

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ RUN export LIBRARY_PATH="/usr/local/$(xx-info triple)/lib:/usr/local/$(xx-info t
5757
RUN ./static.sh build_libssh2
5858
RUN ./static.sh build_libgit2
5959

60-
6160
# trimmed removes all non necessary files (i.e. openssl binary).
6261
FROM build-cross AS trimmed
6362

Dockerfile.test-libgit2-only

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
# This Dockerfile tests the hack/Makefile output against git2go.
2+
ARG BASE_VARIANT=alpine
3+
ARG GO_VERSION=1.17
4+
ARG XX_VERSION=1.1.0
5+
6+
FROM --platform=$BUILDPLATFORM tonistiigi/xx:${XX_VERSION} AS xx
7+
8+
FROM --platform=$BUILDPLATFORM ${BASE_VARIANT} AS build-base
9+
10+
RUN apk add --no-cache \
11+
bash \
12+
curl \
13+
build-base \
14+
linux-headers \
15+
perl \
16+
cmake \
17+
pkgconfig \
18+
gcc \
19+
musl-dev \
20+
clang \
21+
lld
22+
23+
COPY --from=xx / /
24+
25+
FROM build-base AS build-cross
26+
27+
ARG TARGETPLATFORM
28+
29+
RUN xx-apk add --no-cache \
30+
build-base \
31+
pkgconfig \
32+
gcc \
33+
musl-dev \
34+
clang \
35+
lld \
36+
llvm \
37+
linux-headers
38+
39+
WORKDIR /build
40+
COPY hack/static.sh .
41+
42+
ENV CC=xx-clang
43+
ENV CXX=xx-clang++
44+
45+
RUN ./static.sh build_libgit2_only
46+
47+
# trimmed removes all non necessary files (i.e. openssl binary).
48+
FROM build-cross AS trimmed
49+
50+
ARG TARGETPLATFORM
51+
RUN mkdir -p /trimmed/usr/local/$(xx-info triple)/ && \
52+
mkdir -p /trimmed/usr/local/$(xx-info triple)/share
53+
54+
RUN cp -r /usr/local/$(xx-info triple)/lib/ /trimmed/usr/local/$(xx-info triple)/ && \
55+
cp -r /usr/local/$(xx-info triple)/include/ /trimmed/usr/local/$(xx-info triple)/
56+
57+
FROM scratch as libs-arm64
58+
COPY --from=trimmed /trimmed/ /
59+
60+
FROM scratch as libs-amd64
61+
COPY --from=trimmed /trimmed/ /
62+
63+
FROM scratch as libs-armv7
64+
COPY --from=trimmed /trimmed/ /
65+
66+
FROM libs-$TARGETARCH$TARGETVARIANT as libs
67+
68+
# Everything above this line is a copy from Dockefile.
69+
70+
FROM --platform=$BUILDPLATFORM golang:${GO_VERSION}-${BASE_VARIANT} as gostable
71+
72+
FROM gostable AS go-linux
73+
74+
# Build-base consists of build platform dependencies and xx.
75+
# These will be used at current arch to yield execute the cross compilations.
76+
FROM go-${TARGETOS} AS go-base
77+
78+
RUN apk add clang lld pkgconfig
79+
80+
COPY --from=xx / /
81+
82+
# build-go-mod can still be cached at build platform architecture.
83+
FROM go-base as build-go-mod
84+
85+
WORKDIR /root/smoketest
86+
COPY tests/smoketest/go.mod .
87+
COPY tests/smoketest/go.sum .
88+
RUN go mod download
89+
90+
# Build stage install per target platform
91+
# dependency and effectively cross compile the application.
92+
FROM build-go-mod as build
93+
94+
ARG TARGETPLATFORM
95+
96+
# Some dependencies have to installed
97+
# for the target platform: https://github.com/tonistiigi/xx#go--cgo
98+
RUN xx-apk add musl-dev gcc clang lld
99+
100+
WORKDIR /root/smoketest
101+
102+
COPY tests/smoketest/main.go .
103+
COPY --from=libs /usr/local/ /usr/local/
104+
105+
ENV CGO_ENABLED=1
106+
RUN export LIBRARY_PATH="/usr/local/$(xx-info triple)" && \
107+
export PKG_CONFIG_PATH="/usr/local/$(xx-info triple)/lib/pkgconfig" && \
108+
export FLAGS="$(pkg-config --static --libs --cflags libgit2)" && \
109+
export CGO_LDFLAGS="${FLAGS} -static" && \
110+
xx-go build \
111+
-ldflags "-s -w" \
112+
-tags 'netgo,osusergo,static_build' \
113+
-o static-test-runner -trimpath main.go
114+
115+
116+
# Ensure that the generated binary is valid for the target platform
117+
RUN xx-verify --static static-test-runner
118+
119+
# This can be deployed into a gcr.io/distroless/static, however
120+
# the alpine has been chosen so it can run the static application
121+
# using the `RUN` statement.
122+
FROM ${BASE_VARIANT}
123+
124+
RUN apk add git
125+
126+
WORKDIR /root/smoketest
127+
COPY --from=build \
128+
/root/smoketest/static-test-runner .
129+
130+
ENV SSL_CERT_FILE=/etc/ssl/certs/ca-certificates.crt
131+
132+
RUN /root/smoketest/static-test-runner

Makefile

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
IMG ?= ghcr.io/fluxcd/golang-with-libgit2
21
TAG ?= latest
2+
ifeq($(LIBGIT2_ONLY),true)
3+
IMG ?= ghcr.io/fluxcd/golang-with-libgit2-only
4+
else
5+
IMG ?= ghcr.io/fluxcd/golang-with-libgit2-all
6+
endif
37

48
PLATFORMS ?= linux/amd64,linux/arm/v7,linux/arm64
59
BUILD_ARGS ?=
@@ -13,17 +17,17 @@ LIBGIT2_LIB_PATH := $(LIBGIT2_PATH)/lib
1317
LIBGIT2_LIB64_PATH := $(LIBGIT2_PATH)/lib64
1418
LIBGIT2 := $(LIBGIT2_LIB_PATH)/libgit2.a
1519
MUSL-CC =
20+
LIBGIT2_ONLY ?=
1621

1722
export CGO_ENABLED=1
1823
export LIBRARY_PATH=$(LIBGIT2_LIB_PATH):$(LIBGIT2_LIB64_PATH)
1924
export PKG_CONFIG_PATH=$(LIBGIT2_LIB_PATH)/pkgconfig:$(LIBGIT2_LIB64_PATH)/pkgconfig
2025
export CGO_CFLAGS=-I$(LIBGIT2_PATH)/include
2126

22-
2327
ifeq ($(shell uname -s),Linux)
2428
export CGO_LDFLAGS=$(shell PKG_CONFIG_PATH=$(PKG_CONFIG_PATH) pkg-config --libs --static --cflags libssh2 openssl libgit2) -static
2529
else
26-
export CGO_LDFLAGS=$(shell PKG_CONFIG_PATH=$(PKG_CONFIG_PATH) pkg-config --libs --static --cflags libssh2 openssl libgit2) -Wl,--unresolved-symbols=ignore-in-object-files -Wl,-allow-shlib-undefined -static
30+
export CGO_LDFLAGS=$(shell PKG_CONFIG_PATH=$(PKG_CONFIG_PATH) pkg-config --libs --static --cflags libgit2)
2731
endif
2832

2933
ifeq ($(shell uname -s),Linux)
@@ -36,24 +40,41 @@ endif
3640

3741
GO_STATIC_FLAGS=-tags 'netgo,osusergo,static_build'
3842

39-
4043
.PHONY: build
4144
build:
45+
ifeq ($(LIBGIT2_ONLY),true)
46+
docker buildx build \
47+
--platform=$(PLATFORMS) \
48+
--tag $(IMG):$(TAG) \
49+
--file Dockerfile.libgit2-only \
50+
$(BUILD_ARGS) .
51+
else
4252
docker buildx build \
4353
--platform=$(PLATFORMS) \
4454
--tag $(IMG):$(TAG) \
4555
--file Dockerfile \
4656
$(BUILD_ARGS) .
57+
endif
4758

4859
.PHONY: test
4960
test:
61+
ifeq ($(LIBGIT2_ONLY),true)
62+
docker buildx build \
63+
--platform=$(PLATFORMS) \
64+
--tag $(IMG):$(TAG)-test \
65+
--build-arg LIBGIT2_IMG=$(IMG) \
66+
--build-arg LIBGIT2_TAG=$(TAG) \
67+
--file Dockerfile.test-libgit2-only \
68+
$(BUILD_ARGS) .
69+
else
5070
docker buildx build \
5171
--platform=$(PLATFORMS) \
5272
--tag $(IMG):$(TAG)-test \
5373
--build-arg LIBGIT2_IMG=$(IMG) \
5474
--build-arg LIBGIT2_TAG=$(TAG) \
5575
--file Dockerfile.test \
5676
$(BUILD_ARGS) .
77+
endif
5778

5879
.PHONY: builder
5980
builder:
@@ -68,13 +89,21 @@ builder:
6889
# install qemu emulators
6990
docker run -it --rm --privileged tonistiigi/binfmt --install all
7091

71-
7292
$(LIBGIT2): $(MUSL-CC)
7393
ifeq ($(shell uname -s),Darwin)
94+
ifeq ($(LIBGIT2_ONLY),true)
95+
TARGET_DIR=$(TARGET_DIR) BUILD_ROOT_DIR=$(BUILD_ROOT_DIR) \
96+
./hack/static.sh build_libgit2_only
97+
else
7498
TARGET_DIR=$(TARGET_DIR) BUILD_ROOT_DIR=$(BUILD_ROOT_DIR) \
7599
./hack/static.sh all
100+
endif
76101
else
102+
ifeq ($(LIBGIT2_ONLY),true)
77103
IMG_TAG=$(IMG):$(TAG) ./hack/extract-libraries.sh
104+
else
105+
IMG_TAG=$(IMG):$(TAG) ./hack/extract-libraries.sh
106+
endif
78107
endif
79108

80109
$(MUSL-CC):

0 commit comments

Comments
 (0)