From d11eaeaa4afe871dcc552b8391d375db215baf1e Mon Sep 17 00:00:00 2001 From: Carl Montanari <8515611+carlmontanari@users.noreply.github.com> Date: Thu, 21 Sep 2023 11:20:00 -0700 Subject: [PATCH] chore: add build arg for version/hash to devspace and dockerfiles, bump go/debian versions in dockerfiles, dockerfile consistency stuff (#26) * chore: add build arg for version/hash to devspace and dockerfiles, bump go/debian versions in dockerfiles, dockerfile consistency stuff * chore: rename dockerfiles so github is smart enough to highlight things (boo github) * chore: bump go version in actions, only push images/chart if on main --- .develop/{Dockerfile.dev => dev.Dockerfile} | 2 +- .develop/devspace.yaml | 55 ++++++++++++++----- .github/dependabot.yml | 1 + .github/workflows/build.yaml | 12 +++- .github/workflows/lint.yaml | 2 +- .github/workflows/release.yaml | 2 +- .github/workflows/test.yaml | 2 +- build/clabverter.Dockerfile | 33 +++++++++++ .../launcher.Dockerfile | 12 +++- .../manager.Dockerfile | 16 ++++-- clabverter/clabverter.dockerfile | 15 ----- 11 files changed, 108 insertions(+), 44 deletions(-) rename .develop/{Dockerfile.dev => dev.Dockerfile} (92%) create mode 100644 build/clabverter.Dockerfile rename Dockerfile.launcher => build/launcher.Dockerfile (89%) rename Dockerfile.manager => build/manager.Dockerfile (62%) delete mode 100644 clabverter/clabverter.dockerfile diff --git a/.develop/Dockerfile.dev b/.develop/dev.Dockerfile similarity index 92% rename from .develop/Dockerfile.dev rename to .develop/dev.Dockerfile index c1eb67cf..6551436c 100644 --- a/.develop/Dockerfile.dev +++ b/.develop/dev.Dockerfile @@ -1,4 +1,4 @@ -FROM golang:1.20 +FROM golang:1.21-bookworm RUN set -x && apt-get update -y && DEBIAN_FRONTEND=noninteractive apt-get install -y \ ca-certificates wget jq procps curl vim inetutils-ping binutils && \ diff --git a/.develop/devspace.yaml b/.develop/devspace.yaml index 67b3206f..c8996946 100644 --- a/.develop/devspace.yaml +++ b/.develop/devspace.yaml @@ -1,3 +1,4 @@ +--- version: v2beta1 name: clabernetes @@ -17,6 +18,11 @@ vars: LAUNCHER_IMAGE: ${REGISTRY}/${CLABERNETES}-launcher CLABVERTER_IMAGE: ${REGISTRY}/clabverter + # always build w/ 0.0.0 -- overriden for actual releases by release profile. for "normal" builds + # (not releases) we'll do VERSION-HASH, for releases we'll just override to have version tag. + VERSION: 0.0.0 + COMMIT_HASH: $(git describe --always --abbrev=8) + # to be set in ci RELEASE_VERSION: "" @@ -24,42 +30,46 @@ localRegistry: enabled: false images: - clabernetes: - image: ${MANAGER_IMAGE} + clabernetes-dev: + image: ${MANAGER_DEV_IMAGE} context: ../ - dockerfile: ../Dockerfile.manager + dockerfile: dev.Dockerfile rebuildStrategy: ignoreContextChanges tags: - - dev-latest - $(git describe --always --abbrev=8) - clabernetes-dev: - image: ${MANAGER_DEV_IMAGE} + clabernetes: + image: ${MANAGER_IMAGE} context: ../ - dockerfile: Dockerfile.dev + dockerfile: ../build/manager.Dockerfile rebuildStrategy: ignoreContextChanges + buildArgs: + VERSION: ${VERSION}-${COMMIT_HASH} tags: + - dev-latest - $(git describe --always --abbrev=8) clabernetes-launcher: image: ${LAUNCHER_IMAGE} context: ../ - dockerfile: ../Dockerfile.launcher + dockerfile: ../build/launcher.Dockerfile rebuildStrategy: ignoreContextChanges + buildArgs: + VERSION: ${VERSION}-${COMMIT_HASH} tags: - dev-latest - - $(git describe --always --abbrev=8) + - ${COMMIT_HASH} clabverter: image: ${CLABVERTER_IMAGE} context: ../ - dockerfile: ../clabverter/clabverter.dockerfile + dockerfile: ../build/clabverter.Dockerfile rebuildStrategy: ignoreContextChanges buildArgs: - VERSION: "0.0.0-$(git rev-parse --short HEAD)" + VERSION: ${VERSION}-${COMMIT_HASH} tags: - dev-latest - - $(git rev-parse --short HEAD) + - ${COMMIT_HASH} deployments: clabernetes: @@ -137,13 +147,30 @@ profiles: value: - latest - ${RELEASE_VERSION} - - $(git describe --always --abbrev=8) + - ${COMMIT_HASH} + - op: replace + path: images.clabernetes.buildArgs.VERSION + value: ${RELEASE_VERSION} + - op: replace path: images.clabernetes-launcher.tags value: - latest - ${RELEASE_VERSION} - - $(git describe --always --abbrev=8) + - ${COMMIT_HASH} + - op: replace + path: images.clabernetes-launcher.buildArgs.VERSION + value: ${RELEASE_VERSION} + + - op: replace + path: images.clabernetes-clabverter.tags + value: + - latest + - ${RELEASE_VERSION} + - ${COMMIT_HASH} + - op: replace + path: images.clabernetes-clabverter.buildArgs.VERSION + value: ${RELEASE_VERSION} pipelines: build: diff --git a/.github/dependabot.yml b/.github/dependabot.yml index b2f37335..c2042de4 100644 --- a/.github/dependabot.yml +++ b/.github/dependabot.yml @@ -1,3 +1,4 @@ +--- version: 2 updates: - package-ecosystem: "github-actions" diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index 5110608d..b6fba028 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -6,7 +6,7 @@ on: workflow_dispatch: {} env: - GO_VERSION: "1.20" + GO_VERSION: "1.21" jobs: build: @@ -44,9 +44,15 @@ jobs: username: ${{ github.actor }} password: ${{ secrets.GITHUB_TOKEN }} - - name: build images + - name: build images, don't push + if: github.head_ref != 'main' + run: | + devspace run build --skip-push + + - name: build and push images + if: github.head_ref == 'main' run: | devspace run build helm registry login -u ${{ github.actor }} -p ${{ secrets.GITHUB_TOKEN }} ghcr.io helm package chart --version 0.0.0 - helm push clabernetes-0.0.0.tgz oci://ghcr.io/srl-labs/clabernetes + helm push clabernetes-0.0.0.tgz oci://ghcr.io/srl-labs/clabernetes \ No newline at end of file diff --git a/.github/workflows/lint.yaml b/.github/workflows/lint.yaml index 5da2c766..89ac9c3e 100644 --- a/.github/workflows/lint.yaml +++ b/.github/workflows/lint.yaml @@ -6,7 +6,7 @@ on: workflow_dispatch: {} env: - GO_VERSION: "1.20" + GO_VERSION: "1.21" jobs: lint: diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index 95ab1a8d..952c9599 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -7,7 +7,7 @@ on: - created env: - GO_VERSION: "1.20" + GO_VERSION: "1.21" jobs: release: diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index e3d46ab3..47e5a24a 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -6,7 +6,7 @@ on: workflow_dispatch: {} env: - GO_VERSION: "1.20" + GO_VERSION: "1.21" jobs: test: diff --git a/build/clabverter.Dockerfile b/build/clabverter.Dockerfile new file mode 100644 index 00000000..d020c6e9 --- /dev/null +++ b/build/clabverter.Dockerfile @@ -0,0 +1,33 @@ +FROM golang:1.21-bookworm as builder + +ARG VERSION + +WORKDIR /clabernetes + +RUN mkdir build + +COPY cmd/clabverter/main.go main.go + +COPY . . + +COPY go.mod go.mod +COPY go.sum go.sum + +RUN go mod download + +RUN CGO_ENABLED=0 \ + go build \ + -ldflags "-s -w -X github.com/srl-labs/clabernetes/constants.Version=${VERSION}" \ + -trimpath \ + -a \ + -o \ + build/clabverter \ + main.go + +FROM gcr.io/distroless/static-debian12:nonroot + +WORKDIR /clabernetes +COPY --from=builder /clabernetes/build/clabverter . +USER nonroot:nonroot + +ENTRYPOINT ["/clabernetes/clabverter", "run"] \ No newline at end of file diff --git a/Dockerfile.launcher b/build/launcher.Dockerfile similarity index 89% rename from Dockerfile.launcher rename to build/launcher.Dockerfile index 49d0876a..3ec19816 100644 --- a/Dockerfile.launcher +++ b/build/launcher.Dockerfile @@ -1,4 +1,6 @@ -FROM golang:1.20 as builder +FROM golang:1.21-bookworm as builder + +ARG VERSION WORKDIR /clabernetes @@ -17,8 +19,12 @@ RUN CGO_ENABLED=0 \ GOOS=linux \ GOARCH=amd64 \ go build \ - -ldflags="-s -w" \ - -a -o build/manager main.go + -ldflags "-s -w -X github.com/srl-labs/clabernetes/constants.Version=${VERSION}" \ + -trimpath \ + -a \ + -o \ + build/manager \ + main.go FROM debian:bookworm-slim diff --git a/Dockerfile.manager b/build/manager.Dockerfile similarity index 62% rename from Dockerfile.manager rename to build/manager.Dockerfile index f8069e7d..0977f20d 100644 --- a/Dockerfile.manager +++ b/build/manager.Dockerfile @@ -1,4 +1,6 @@ -FROM golang:1.20 as builder +FROM golang:1.21-bookworm as builder + +ARG VERSION WORKDIR /clabernetes @@ -17,14 +19,18 @@ RUN CGO_ENABLED=0 \ GOOS=linux \ GOARCH=amd64 \ go build \ - -ldflags="-s -w" \ - -a -o build/manager main.go + -ldflags "-s -w -X github.com/srl-labs/clabernetes/constants.Version=${VERSION}" \ + -trimpath \ + -a \ + -o \ + build/manager \ + main.go -FROM gcr.io/distroless/static-debian11:nonroot +FROM gcr.io/distroless/static-debian12:nonroot WORKDIR /clabernetes COPY --from=builder --chown=nonroot:nonroot /clabernetes/certificates /clabernetes/certificates COPY --from=builder /clabernetes/build/manager . -USER nonroot +USER nonroot:nonroot ENTRYPOINT ["/clabernetes/manager", "run"] \ No newline at end of file diff --git a/clabverter/clabverter.dockerfile b/clabverter/clabverter.dockerfile deleted file mode 100644 index 25b8a2e7..00000000 --- a/clabverter/clabverter.dockerfile +++ /dev/null @@ -1,15 +0,0 @@ -FROM golang:1.20-bookworm as builder - -COPY . /workdir -WORKDIR /workdir - -ARG VERSION - -RUN CGO_ENABLED=0 go build -ldflags "-s -w -X github.com/srl-labs/clabernetes/constants.Version=${VERSION}" -trimpath -o clabverter-bin ./cmd/clabverter/main.go - -FROM gcr.io/distroless/static-debian12:nonroot -COPY --from=builder /workdir/clabverter-bin /bin/clabverter - -USER nonroot:nonroot - -ENTRYPOINT ["/bin/clabverter"]