Skip to content

Commit 38a7b8a

Browse files
committed
dockerfile: licenses target to validate licenses
Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
1 parent d3b7a4f commit 38a7b8a

File tree

2 files changed

+37
-11
lines changed

2 files changed

+37
-11
lines changed

Dockerfile

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,34 @@ RUN --mount=type=cache,target=/go/pkg/mod \
1717
--mount=type=bind,source=go.sum,target=go.sum \
1818
go mod download
1919

20+
FROM builder-base AS go-licenses
21+
ARG TARGETPLATFORM
22+
RUN --mount=target=/root/.cache,type=cache \
23+
--mount=type=cache,target=/go/pkg/mod <<EOT
24+
set -ex
25+
xx-go install github.com/google/go-licenses@latest
26+
mkdir /out
27+
if ! xx-info is-cross; then
28+
mv /go/bin/go-licenses /out
29+
else
30+
mv /go/bin/*/go-licenses* /out
31+
fi
32+
EOT
33+
34+
FROM builder-base AS licenses-validate
35+
RUN --mount=type=bind,target=. \
36+
--mount=type=cache,target=/go/pkg/mod \
37+
--mount=from=go-licenses,source=/out/go-licenses,target=/usr/bin/go-licenses \
38+
go-licenses check . --allowed_licenses=Apache-2.0,MIT,BSD-3-Clause,BSD-2-Clause --ignore modernc.org/mathutil
39+
2040
FROM builder-base AS lint
2141
RUN apk add --no-cache gcc musl-dev
2242
WORKDIR /
2343
ARG GOLANGCI_LINT_VERSION
2444
RUN wget -O- -nv https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s ${GOLANGCI_LINT_VERSION}
2545
WORKDIR /src
2646
ARG TARGETPLATFORM
27-
RUN --mount=target=/src \
47+
RUN --mount=type=bind,target=. \
2848
--mount=type=cache,target=/go/pkg/mod \
2949
--mount=target=/root/.cache,type=cache,id=lint-$TARGETPLATFORM \
3050
xx-go --wrap && \
@@ -43,7 +63,7 @@ FROM scratch AS test-coverage
4363
COPY --from=test /tmp/coverage.txt /coverage.txt
4464

4565
FROM builder-base AS version
46-
RUN --mount=target=. <<'EOT'
66+
RUN --mount=type=bind,target=. <<'EOT'
4767
git rev-parse HEAD 2>/dev/null || {
4868
echo >&2 "Failed to get git revision, make sure --build-arg BUILDKIT_CONTEXT_KEEP_GIT_DIR=1 is set when building from Git directly"
4969
exit 1
@@ -55,18 +75,18 @@ RUN --mount=target=. <<'EOT'
5575
EOT
5676

5777
FROM builder-base AS builder
58-
COPY . ./
5978
ARG TARGETPLATFORM
6079
ARG TARGETOS
61-
RUN --mount=type=cache,target=/root/.cache,id=docker-ai-$TARGETPLATFORM \
80+
RUN --mount=type=bind,target=. \
81+
--mount=type=cache,target=/root/.cache,id=docker-ai-$TARGETPLATFORM \
6282
--mount=source=/tmp/.ldflags,target=/tmp/.ldflags,from=version \
6383
--mount=type=cache,target=/go/pkg/mod <<EOT
64-
set -ex
65-
xx-go build -trimpath -ldflags "-s -w $(cat /tmp/.ldflags)" -o /binaries/cagent .
66-
xx-verify --static /binaries/cagent
67-
if [ "$TARGETOS" = "windows" ]; then
68-
mv /binaries/cagent /binaries/cagent.exe
69-
fi
84+
set -ex
85+
xx-go build -trimpath -ldflags "-s -w $(cat /tmp/.ldflags)" -o /binaries/cagent .
86+
xx-verify --static /binaries/cagent
87+
if [ "$TARGETOS" = "windows" ]; then
88+
mv /binaries/cagent /binaries/cagent.exe
89+
fi
7090
EOT
7191

7292
FROM scratch AS binaries

docker-bake.hcl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ group "default" {
2828
}
2929

3030
group "validate" {
31-
targets = ["lint", "test"]
31+
targets = ["lint", "test", "licenses"]
3232
}
3333

3434
target "lint" {
@@ -43,6 +43,12 @@ target "test" {
4343
output = [bindir("coverage")]
4444
}
4545

46+
target "licenses" {
47+
inherits = ["_common"]
48+
target = "licenses-validate"
49+
output = ["type=cacheonly"]
50+
}
51+
4652
target "binaries" {
4753
inherits = ["_common"]
4854
target = "binaries"

0 commit comments

Comments
 (0)