From 6351769c39333402cfce53e324ae6479532c8c61 Mon Sep 17 00:00:00 2001 From: Alex Gluck Date: Sun, 10 Mar 2024 01:32:35 +0300 Subject: [PATCH 1/3] Improve Dockerfile 1. Union argument layers and copy source code. 2. Changed layers order final image for grow cache hit --- Dockerfile | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/Dockerfile b/Dockerfile index aca26f92..39760c25 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,20 +1,17 @@ # Build the manager binary FROM golang:1.21 AS builder -ARG TARGETOS -ARG TARGETARCH +ARG TARGETOS TARGETARCH WORKDIR /workspace # Copy the Go Modules manifests -COPY go.mod go.mod -COPY go.sum go.sum +COPY go.mod go.sum ./ + # cache deps before building and copying source so that we don't need to re-download as much # and so that source changes don't invalidate our downloaded layer RUN go mod download # Copy the go source -COPY cmd/main.go cmd/main.go -COPY api/ api/ -COPY internal/controller/ internal/controller/ +COPY cmd/main.go api/ internal/controller/ ./ # Build # the GOARCH has not a default value to allow the binary be built according to the host where the command @@ -26,8 +23,7 @@ RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build -a -o ma # Use distroless as minimal base image to package the manager binary # Refer to https://github.com/GoogleContainerTools/distroless for more details FROM gcr.io/distroless/static:nonroot -WORKDIR / -COPY --from=builder /workspace/manager . -USER 65532:65532 - ENTRYPOINT ["/manager"] +USER 65532:65532 +WORKDIR / +COPY --chown=root:root --from=builder /workspace/manager . From ed538df331fb166067b076cf3f7f724e40eb6fbb Mon Sep 17 00:00:00 2001 From: Oleg Kunitsyn <114359669+hiddenmarten@users.noreply.github.com> Date: Sun, 10 Mar 2024 14:23:26 +0100 Subject: [PATCH 2/3] GHA docker publish pipeline (#4) --- .github/workflows/docker-publish.yaml | 92 +++++++++++++++++++++++++++ api/.gitkeep | 0 internal/controller/.gitkeep | 0 3 files changed, 92 insertions(+) create mode 100644 .github/workflows/docker-publish.yaml create mode 100644 api/.gitkeep create mode 100644 internal/controller/.gitkeep diff --git a/.github/workflows/docker-publish.yaml b/.github/workflows/docker-publish.yaml new file mode 100644 index 00000000..0769667a --- /dev/null +++ b/.github/workflows/docker-publish.yaml @@ -0,0 +1,92 @@ +name: Docker publish + +# This workflow uses actions that are not certified by GitHub. +# They are provided by a third-party and are governed by +# separate terms of service, privacy policy, and support +# documentation. + +on: + push: + # Publish semver tags as releases. + tags: [ 'v*.*.*' ] + +env: + # Use docker.io for Docker Hub if empty + REGISTRY: ghcr.io + # github.repository as / + IMAGE_NAME: ${{ github.repository }} + + +jobs: + build: + runs-on: ubuntu-22.04 + permissions: + contents: read + packages: write + # This is used to complete the identity challenge + # with sigstore/fulcio when running outside of PRs. + id-token: write + + steps: + - name: Checkout repository + uses: actions/checkout@v3 + + # Install the cosign tool except on PR + # https://github.com/sigstore/cosign-installer + - name: Install cosign + if: github.event_name != 'pull_request' + uses: sigstore/cosign-installer@v3.1.1 + with: + cosign-release: 'v2.1.1' + + # Set up BuildKit Docker container builder to be able to build + # multi-platform images and export cache + # https://github.com/docker/setup-buildx-action + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3.0.0 + + # Login against a Docker registry except on PR + # https://github.com/docker/login-action + - name: Log into registry ${{ env.REGISTRY }} + if: github.event_name != 'pull_request' + uses: docker/login-action@v3.0.0 + with: + registry: ${{ env.REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + # Extract metadata (tags, labels) for Docker + # https://github.com/docker/metadata-action + - name: Extract Docker metadata + id: meta + uses: docker/metadata-action@v5.0.0 + with: + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} + + # Build and push Docker image with Buildx (don't push on PR) + # https://github.com/docker/build-push-action + - name: Build and push Docker image + id: build-and-push + uses: docker/build-push-action@v5.0.0 + with: + context: . + push: ${{ github.event_name != 'pull_request' }} + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} + cache-from: type=gha + cache-to: type=gha,mode=max + + # Sign the resulting Docker image digest except on PRs. + # This will only write to the public Rekor transparency log when the Docker + # repository is public to avoid leaking data. If you would like to publish + # transparency data even for private images, pass --force to cosign below. + # https://github.com/sigstore/cosign + - name: Sign the published Docker image + if: ${{ github.event_name != 'pull_request' }} + env: + # https://docs.github.com/en/actions/security-guides/security-hardening-for-github-actions#using-an-intermediate-environment-variable + TAGS: ${{ steps.meta.outputs.tags }} + DIGEST: ${{ steps.build-and-push.outputs.digest }} + # This step uses the identity token to provision an ephemeral certificate + # against the sigstore community Fulcio instance. + run: echo "${TAGS}" | xargs -I {} cosign sign --yes {}@${DIGEST} diff --git a/api/.gitkeep b/api/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/internal/controller/.gitkeep b/internal/controller/.gitkeep new file mode 100644 index 00000000..e69de29b From 858d4dc7a6dbdfa0a60b138277ee613fb3664524 Mon Sep 17 00:00:00 2001 From: Alex Gluck Date: Mon, 11 Mar 2024 22:04:11 +0300 Subject: [PATCH 3/3] Fixed folder structure copying --- Dockerfile | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index 39760c25..52bb1b9a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -11,14 +11,16 @@ COPY go.mod go.sum ./ RUN go mod download # Copy the go source -COPY cmd/main.go api/ internal/controller/ ./ +COPY cmd/main.go ./cmd/ +COPY api/ ./api/ +COPY internal/controller/ ./internal/controller/ # Build # the GOARCH has not a default value to allow the binary be built according to the host where the command # was called. For example, if we call make docker-build in a local env which has the Apple Silicon M1 SO # the docker BUILDPLATFORM arg will be linux/arm64 when for Apple x86 it will be linux/amd64. Therefore, # by leaving it empty we can ensure that the container and binary shipped on it will have the same platform. -RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build -a -o manager cmd/main.go +RUN CGO_ENABLED=0 GOOS="${TARGETOS:-linux}" GOARCH="${TARGETARCH}" go build -a -o manager cmd/main.go # Use distroless as minimal base image to package the manager binary # Refer to https://github.com/GoogleContainerTools/distroless for more details