Skip to content

Commit f4b3163

Browse files
authored
Merge pull request #35 from austinvazquez/add-local-linting
Add linting to docker bake for local validation
2 parents 3a0d8bf + ca1cfe8 commit f4b3163

File tree

7 files changed

+165
-34
lines changed

7 files changed

+165
-34
lines changed

.github/actions/install-go/action.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ runs:
1313
uses: actions/setup-go@v5
1414
with:
1515
go-version: ${{ inputs.go-version }}
16-
cache: false # see actions/setup-go#368
16+
cache: false # see actions/setup-go#368

.github/workflows/ci.yml

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,36 +6,22 @@ on:
66
pull_request:
77
branches: ['main', 'release/**']
88

9-
permissions: # added using https://github.com/step-security/secure-workflows
9+
permissions: # added using https://github.com/step-security/secure-workflows
1010
contents: read
1111

1212
jobs:
13-
#
14-
# golangci-lint
15-
#
1613
linters:
1714
permissions:
18-
contents: read # for actions/checkout to fetch code
19-
pull-requests: read # for golangci/golangci-lint-action to fetch pull requests
15+
contents: read # for actions/checkout to fetch code
16+
pull-requests: read # for golangci/golangci-lint-action to fetch pull requests
2017
name: Linters
21-
runs-on: ${{ matrix.os }}
18+
runs-on: ubuntu-latest
2219
timeout-minutes: 10
2320

24-
strategy:
25-
matrix:
26-
os: [ubuntu-latest, ubuntu-24.04-arm, macos-latest] # skipping windows-latest for now
27-
exclude:
28-
- os: ${{ github.event.repository.private && 'ubuntu-24.04-arm' || '' }}
29-
30-
3121
steps:
32-
- uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
33-
- uses: ./.github/actions/install-go
34-
- uses: golangci/golangci-lint-action@4afd733a84b1f43292c63897423277bb7f4313a9 # v8.0.0
35-
with:
36-
version: v2.1.5
37-
skip-cache: true
38-
args: --timeout=8m
22+
- uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
23+
- uses: docker/setup-buildx-action@e468171a9de216ec08956ac3ada2f0791b6bd435 # v3.11.1
24+
- run: make validate
3925

4026
#
4127
# Project checks
@@ -46,14 +32,14 @@ jobs:
4632
timeout-minutes: 5
4733

4834
steps:
49-
- uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
35+
- uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
5036
with:
5137
path: src/github.com/containerd/nerdbox
5238
fetch-depth: 100
5339

5440
- uses: ./src/github.com/containerd/nerdbox/.github/actions/install-go
5541

56-
- uses: containerd/project-checks@d7751f3c375b8fe4a84c02a068184ee4c1f59bc4 # v1.2.2
42+
- uses: containerd/project-checks@d7751f3c375b8fe4a84c02a068184ee4c1f59bc4 # v1.2.2
5743
if: github.repository == 'containerd/nerdbox'
5844
with:
5945
working-directory: src/github.com/containerd/nerdbox
@@ -77,12 +63,12 @@ jobs:
7763
working-directory: src/github.com/containerd/nerdbox
7864

7965
steps:
80-
- uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
66+
- uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
8167
with:
8268
path: src/github.com/containerd/nerdbox
8369

8470
# Needed for proto lookup during generation
85-
- uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
71+
- uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
8672
with:
8773
repository: containerd/containerd
8874
path: src/github.com/containerd/containerd
@@ -106,4 +92,3 @@ jobs:
10692
- run: script/install-proto-tools
10793
- run: make proto-fmt
10894
- run: make check-protos check-api-descriptors
109-

.golangci.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
version: "2"
22
linters:
33
enable:
4-
- copyloopvar # Checks for loop variable copies in Go 1.22+
5-
- depguard # Checks for dependencies that should not be (re)introduced. See "settings" for further details.
6-
- dupword # Checks for duplicate words in the source code
4+
- copyloopvar # Checks for loop variable copies in Go 1.22+
5+
- depguard # Checks for dependencies that should not be (re)introduced. See "settings" for further details.
6+
- dupword # Checks for duplicate words in the source code
77
- gosec
88
- misspell
99
- nolintlint
@@ -39,7 +39,7 @@ linters:
3939
staticcheck:
4040
checks:
4141
- all
42-
- -QF1008 # Excludes QF1008 from staticcheck
42+
- -QF1008 # Excludes QF1008 from staticcheck
4343
- -ST1000
4444
- -ST1003
4545
- -ST1020
@@ -49,7 +49,7 @@ linters:
4949
- name: package-comments
5050
severity: warning
5151
disabled: true
52-
exclude: [ "" ]
52+
exclude: [""]
5353
nolintlint:
5454
allow-unused: true
5555
exclusions:

.yamllint.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
ignore: |
2+
/vendor
3+
4+
extends: default
5+
6+
yaml-files:
7+
- '*.yaml'
8+
- '*.yml'
9+
10+
rules:
11+
truthy: disable
12+
line-length: disable
13+
document-start: disable

Dockerfile

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,24 @@
1313
# limitations under the License.
1414

1515
# -----------------------------------------------------------------------------
16+
# syntax=docker/dockerfile:1
1617

1718
# Build the Linux kernel, initrd ,and containerd shim for running nerbox
1819

20+
ARG XX_VERSION=1.6.1
1921
ARG GO_VERSION=1.25.1
2022
ARG BASE_DEBIAN_DISTRO="bookworm"
2123
ARG GOLANG_IMAGE="golang:${GO_VERSION}-${BASE_DEBIAN_DISTRO}"
24+
ARG GOLANGCI_LINT_VERSION=2.5.0
25+
ARG GOLANGCI_FROM_SOURCE=false
2226
ARG DOCKER_VERSION=28.4.0
2327
ARG DOCKER_IMAGE="docker:${DOCKER_VERSION}-cli"
2428
ARG RUST_IMAGE="rust:1.89.0-slim-${BASE_DEBIAN_DISTRO}"
2529

26-
FROM ${GOLANG_IMAGE} AS base
30+
# xx is a helper for cross-compilation
31+
FROM --platform=$BUILDPLATFORM tonistiigi/xx:${XX_VERSION} AS xx
32+
33+
FROM --platform=$BUILDPLATFORM ${GOLANG_IMAGE} AS base
2734

2835
RUN echo 'Binary::apt::APT::Keep-Downloaded-Packages "true";' > /etc/apt/apt.conf.d/keep-cache
2936
RUN apt-get update && apt-get install --no-install-recommends -y file
@@ -221,3 +228,68 @@ COPY --from=libkrun /libkrun.so /usr/local/lib64/libkrun.so
221228
ENV LIBKRUN_PATH=/go/src/github.com/containerd/nerdbox/_output
222229

223230
VOLUME /var/lib/containerd
231+
232+
233+
FROM base AS golangci-build
234+
WORKDIR /src
235+
ARG GOLANGCI_LINT_VERSION
236+
ADD https://github.com/golangci/golangci-lint.git#v${GOLANGCI_LINT_VERSION} .
237+
COPY --link --from=xx / /
238+
RUN --mount=type=cache,target=/go/pkg/mod \
239+
--mount=type=cache,target=/root/.cache/ \
240+
xx-go --wrap && \
241+
go mod download
242+
RUN --mount=type=cache,target=/go/pkg/mod \
243+
--mount=type=cache,target=/root/.cache/ \
244+
xx-go --wrap && \
245+
mkdir -p out && \
246+
go build -o /out/golangci-lint ./cmd/golangci-lint
247+
248+
FROM scratch AS golangci-binary-false
249+
FROM scratch AS golangci-binary-true
250+
COPY --from=golangci-build /out/golangci-lint golangci-lint
251+
FROM golangci-binary-${GOLANGCI_FROM_SOURCE} AS golangci-binary
252+
253+
FROM base AS lint-base
254+
ENV GOFLAGS="-buildvcs=false"
255+
RUN <<EOT
256+
apt-get update
257+
apt-get install -y --no-install-recommends gcc libc6-dev yamllint
258+
rm -rf /var/lib/apt/lists/*
259+
EOT
260+
ARG GOLANGCI_LINT_VERSION
261+
ARG GOLANGCI_FROM_SOURCE
262+
COPY --link --from=golangci-binary / /usr/bin/
263+
RUN [ "${GOLANGCI_FROM_SOURCE}" = "true" ] && exit 0; wget -O- -nv https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s v${GOLANGCI_LINT_VERSION}
264+
COPY --link --from=xx / /
265+
WORKDIR /go/src/github.com/containerd/nerdbox
266+
267+
FROM lint-base AS golangci-lint
268+
ARG TARGETNAME
269+
ARG TARGETPLATFORM
270+
RUN --mount=target=/go/src/github.com/containerd/nerdbox \
271+
--mount=target=/root/.cache,type=cache,id=lint-cache-${TARGETNAME}-${TARGETPLATFORM} \
272+
xx-go --wrap && \
273+
golangci-lint run -c .golangci.yml && \
274+
touch /golangci-lint.done
275+
276+
FROM lint-base AS golangci-verify-false
277+
RUN --mount=target=/go/src/github.com/containerd/nerdbox \
278+
golangci-lint config verify && \
279+
touch /golangci-verify.done
280+
281+
FROM scratch AS golangci-verify-true
282+
COPY <<EOF /golangci-verify.done
283+
EOF
284+
285+
FROM golangci-verify-${GOLANGCI_FROM_SOURCE} AS golangci-verify
286+
287+
FROM lint-base AS yamllint
288+
RUN --mount=target=/go/src/github.com/containerd/nerdbox \
289+
yamllint -c .yamllint.yml --strict . && \
290+
touch /yamllint.done
291+
292+
FROM scratch AS lint
293+
COPY --link --from=golangci-lint /golangci-lint.done /
294+
COPY --link --from=golangci-verify /golangci-verify.done /
295+
COPY --link --from=yamllint /yamllint.done /

Makefile

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ GO_STATIC_LDFLAGS := -ldflags '-extldflags "-static" $(LDFLAGS) $(EXTRA_LDFLAGS)
5252
MODULE_NAME=$(shell go list -m)
5353
API_PACKAGES=$(shell ($(GO) list ${GO_TAGS} ./... | grep /api/ ))
5454

55-
.PHONY: clean all generate protos check-protos check-api-descriptors proto-fmt shell
55+
.PHONY: clean all validate lint generate protos check-protos check-api-descriptors proto-fmt shell
5656

5757
all:
5858
$(BUILDX) bake
@@ -135,6 +135,12 @@ endif
135135

136136
FORCE:
137137

138+
validate:
139+
@$(BUILDX) bake validate
140+
141+
lint:
142+
@$(BUILDX) bake lint
143+
138144
clean:
139145
rm -rf _output
140146

docker-bake.hcl

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ variable "GO_LDFLAGS" {
2626
default = ""
2727
}
2828

29+
variable "GOLANGCI_LINT_MULTIPLATFORM" {
30+
default = ""
31+
}
32+
2933
target "_common" {
3034
args = {
3135
KERNEL_VERSION = KERNEL_VERSION
@@ -81,3 +85,54 @@ target "dev" {
8185
target = "dev"
8286
output = ["type=image,name=nerdbox-dev"]
8387
}
88+
89+
group "validate" {
90+
targets = ["lint", "validate-dockerfile"]
91+
}
92+
93+
target "lint" {
94+
name = "lint-${build.name}"
95+
inherits = ["_common"]
96+
output = ["type=cacheonly"]
97+
target = build.target
98+
args = {
99+
TARGETNAME = build.name
100+
GOLANGCI_FROM_SOURCE = "true"
101+
}
102+
platforms = (build.target == "golangci-lint") && (GOLANGCI_LINT_MULTIPLATFORM != null) ? [
103+
"linux/amd64",
104+
"linux/arm64",
105+
"darwin/amd64",
106+
"darwin/arm64",
107+
// "windows/amd64",
108+
// "windows/arm64",
109+
] : []
110+
matrix = {
111+
build = [
112+
{
113+
name = "default",
114+
target = "golangci-lint",
115+
},
116+
{
117+
name = "golangci-verify",
118+
target = "golangci-verify",
119+
},
120+
{
121+
name = "yaml",
122+
target = "yamllint",
123+
},
124+
]
125+
}
126+
}
127+
128+
target "validate-dockerfile" {
129+
matrix = {
130+
dockerfile = [
131+
"Dockerfile",
132+
]
133+
}
134+
name = "validate-dockerfile-${md5(dockerfile)}"
135+
inherits = ["_common"]
136+
dockerfile = dockerfile
137+
call = "check"
138+
}

0 commit comments

Comments
 (0)