From 857e660e9f4611b2581adcde602f36a6735649e9 Mon Sep 17 00:00:00 2001 From: Tom Martensen Date: Mon, 11 Sep 2023 10:21:07 +0200 Subject: [PATCH 1/6] multi stage build --- .github/workflows/build-and-push.yaml | 2 +- Makefile | 4 ++++ image/Dockerfile.multi | 23 +++++++++++++++++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 image/Dockerfile.multi diff --git a/.github/workflows/build-and-push.yaml b/.github/workflows/build-and-push.yaml index 73bc9123e..ca526d03e 100644 --- a/.github/workflows/build-and-push.yaml +++ b/.github/workflows/build-and-push.yaml @@ -55,7 +55,7 @@ jobs: - name: Build run: | - make image + make multi-image - name: Login for image push env: diff --git a/Makefile b/Makefile index 62f4f6a1d..32c6d00be 100644 --- a/Makefile +++ b/Makefile @@ -79,6 +79,10 @@ image: server cli ui clean-image @cp bin/infractl-linux-amd64 image/static/downloads docker build -t $(IMAGE) image +.PHONY: multi-image +multi-image: + docker build . -t $(IMAGE) -f image/Dockerfile.multi --secret id=npmrc,src=${HOME}/.npmrc + .PHONY: push push: docker push $(IMAGE) | cat diff --git a/image/Dockerfile.multi b/image/Dockerfile.multi new file mode 100644 index 000000000..fb048698c --- /dev/null +++ b/image/Dockerfile.multi @@ -0,0 +1,23 @@ +FROM golang:1.20.7 as golang-builder + +WORKDIR /go/src/github.com/stackrox/infra + +COPY . . + +RUN make server cli + +FROM node:16.20.2 as ui-builder + +COPY ui ui + +RUN --mount=type=secret,id=npmrc,target=/root/.npmrc make -C ui all + +FROM alpine:3.14 as app + +COPY --from=golang-builder /go/src/github.com/stackrox/infra/bin/infra-server-linux-amd64 /infra-server + +COPY --from=ui-builder /ui/build /etc/infra/static + +COPY --from=golang-builder /go/src/github.com/stackrox/infra/bin/infractl-* /etc/infra/static/downloads + +ENTRYPOINT ["/infra-server"] From 6ba5b7224387bc0b3fbf1bcc77e5fd0a909ebbf2 Mon Sep 17 00:00:00 2001 From: Tom Martensen Date: Mon, 11 Sep 2023 10:28:56 +0200 Subject: [PATCH 2/6] clean ups --- .github/workflows/build-and-push.yaml | 21 +++++---------------- 1 file changed, 5 insertions(+), 16 deletions(-) diff --git a/.github/workflows/build-and-push.yaml b/.github/workflows/build-and-push.yaml index ca526d03e..b055e514c 100644 --- a/.github/workflows/build-and-push.yaml +++ b/.github/workflows/build-and-push.yaml @@ -33,15 +33,6 @@ jobs: PATH="${PATH}:${GOPATH}/bin" echo PATH="${PATH}" >> "$GITHUB_ENV" - - name: Cache Go dependencies - uses: ./go/src/github.com/stackrox/infra/.github/actions/cache-go-dependencies - - - name: Create UI cache lock - uses: ./go/src/github.com/stackrox/infra/.github/actions/create-concatenated-ui-monorepo-lock - - - name: Cache UI dependencies - uses: ./go/src/github.com/stackrox/infra/.github/actions/cache-ui-dependencies - - name: Generate src # This is committed at generated/ but building here ensure the make # targets do not go stale and that any updates are committed. @@ -58,14 +49,12 @@ jobs: make multi-image - name: Login for image push - env: - INFRA_IMAGE_PUSH: ${{ secrets.INFRA_IMAGE_PUSH }} - run: docker login -u _json_key --password-stdin <<<"$INFRA_IMAGE_PUSH" https://us.gcr.io + uses: docker/login-action@v2 + with: + registry: us.gcr.io + username: _json_key + password: ${{ secrets.INFRA_IMAGE_PUSH }} - name: Push run: | make push - - - name: Logout - run: | - docker logout https://us.gcr.io From a416420c678f3cdeca0eba7082f026f10816c2e6 Mon Sep 17 00:00:00 2001 From: Tom Martensen Date: Mon, 11 Sep 2023 10:54:51 +0200 Subject: [PATCH 3/6] hm --- image/Dockerfile.multi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/image/Dockerfile.multi b/image/Dockerfile.multi index fb048698c..20ac8f11f 100644 --- a/image/Dockerfile.multi +++ b/image/Dockerfile.multi @@ -18,6 +18,6 @@ COPY --from=golang-builder /go/src/github.com/stackrox/infra/bin/infra-server-li COPY --from=ui-builder /ui/build /etc/infra/static -COPY --from=golang-builder /go/src/github.com/stackrox/infra/bin/infractl-* /etc/infra/static/downloads +COPY --from=golang-builder /go/src/github.com/stackrox/infra/bin/infractl-* /etc/infra/static/downloads/ ENTRYPOINT ["/infra-server"] From 0295ff7e31389c12ba9fdb5a316c427521c5f939 Mon Sep 17 00:00:00 2001 From: Tom Martensen Date: Mon, 11 Sep 2023 11:21:20 +0200 Subject: [PATCH 4/6] remove cache --- .../actions/cache-go-dependencies/action.yaml | 24 ------------------- .../actions/cache-ui-dependencies/action.yaml | 20 ---------------- .../action.yaml | 17 ------------- 3 files changed, 61 deletions(-) delete mode 100644 .github/actions/cache-go-dependencies/action.yaml delete mode 100644 .github/actions/cache-ui-dependencies/action.yaml delete mode 100644 .github/actions/create-concatenated-ui-monorepo-lock/action.yaml diff --git a/.github/actions/cache-go-dependencies/action.yaml b/.github/actions/cache-go-dependencies/action.yaml deleted file mode 100644 index 444530db7..000000000 --- a/.github/actions/cache-go-dependencies/action.yaml +++ /dev/null @@ -1,24 +0,0 @@ -name: Cache Go Dependencies -description: Cache Go Dependencies -runs: - using: composite - steps: - - name: Determine Go cache paths - id: cache-paths - run: | - echo "GOCACHE=$(go env GOCACHE)" >> $GITHUB_OUTPUT - echo "GOMODCACHE=$(go env GOMODCACHE)" >> $GITHUB_OUTPUT - shell: bash - - - name: Cache Go Dependencies - uses: actions/cache@v3 - with: - path: | - ${{ steps.cache-paths.outputs.GOCACHE }} - ${{ steps.cache-paths.outputs.GOMODCACHE }} - /github/home/.cache/go-build - /github/home/.cache/golangci-lint - /github/home/.cache/staticcheck - key: go-v2-${{ hashFiles('**/go.sum') }} - restore-keys: | - go-v2- diff --git a/.github/actions/cache-ui-dependencies/action.yaml b/.github/actions/cache-ui-dependencies/action.yaml deleted file mode 100644 index 122a55826..000000000 --- a/.github/actions/cache-ui-dependencies/action.yaml +++ /dev/null @@ -1,20 +0,0 @@ -name: Cache UI Dependencies -description: Cache UI Dependencies -inputs: - lockFile: - description: Where the monorepo lock is written - required: true - default: "go/src/github.com/stackrox/infra/ui/monorepo.lock" -runs: - using: composite - steps: - - name: Cache UI Dependencies - uses: actions/cache@v3 - with: - path: | - /github/home/.cache/Cypress - /usr/local/share/.cache - go/src/github.com/stackrox/infra/ui/node_modules - key: npm-v2-${{ hashFiles(inputs.lockFile) }} - restore-keys: | - npm-v2- diff --git a/.github/actions/create-concatenated-ui-monorepo-lock/action.yaml b/.github/actions/create-concatenated-ui-monorepo-lock/action.yaml deleted file mode 100644 index dac862ce7..000000000 --- a/.github/actions/create-concatenated-ui-monorepo-lock/action.yaml +++ /dev/null @@ -1,17 +0,0 @@ -name: Create combined mono repo lock file -description: Create combined mono repo lock file -inputs: - lockFile: - description: Where the lockfile is written - required: true - default: "ui/monorepo.lock" -runs: - using: composite - steps: - - name: Create combined mono repo lock file - run: | - echo "Creating combined lock in: ${{ inputs.lockFile }}" - find ui/ -type d \( -name node_modules \) -prune -false -o -name package.json -print0 | sort -z | xargs -r0 cat > ${{ inputs.lockFile }} - cat ui/yarn.lock >> ${{ inputs.lockFile }} - shell: bash - working-directory: go/src/github.com/stackrox/infra \ No newline at end of file From 8853faeda69c8febc9455bb1ea0111a06cc53b58 Mon Sep 17 00:00:00 2001 From: Tom Martensen Date: Tue, 12 Sep 2023 13:25:13 +0200 Subject: [PATCH 5/6] cleanup --- .github/workflows/build-and-push.yaml | 2 +- Makefile | 20 ++------------------ image/Dockerfile | 22 +++++++++++++++++++--- image/Dockerfile.multi | 23 ----------------------- 4 files changed, 22 insertions(+), 45 deletions(-) delete mode 100644 image/Dockerfile.multi diff --git a/.github/workflows/build-and-push.yaml b/.github/workflows/build-and-push.yaml index b055e514c..250978e57 100644 --- a/.github/workflows/build-and-push.yaml +++ b/.github/workflows/build-and-push.yaml @@ -46,7 +46,7 @@ jobs: - name: Build run: | - make multi-image + make image - name: Login for image push uses: docker/login-action@v2 diff --git a/Makefile b/Makefile index 32c6d00be..3010b627a 100644 --- a/Makefile +++ b/Makefile @@ -69,29 +69,13 @@ ui: @make -C ui all .PHONY: image -image: server cli ui clean-image - @echo "+ $@" - @cp -f bin/infra-server-linux-amd64 image/infra-server - @mkdir -p image/static/downloads - @cp -R ui/build/* image/static/ - @cp bin/infractl-darwin-amd64 image/static/downloads - @cp bin/infractl-darwin-arm64 image/static/downloads - @cp bin/infractl-linux-amd64 image/static/downloads - docker build -t $(IMAGE) image - -.PHONY: multi-image -multi-image: - docker build . -t $(IMAGE) -f image/Dockerfile.multi --secret id=npmrc,src=${HOME}/.npmrc +image: + docker build . -t $(IMAGE) -f image/Dockerfile --secret id=npmrc,src=${HOME}/.npmrc .PHONY: push push: docker push $(IMAGE) | cat -.PHONY: clean-image -clean-image: - @echo "+ $@" - @rm -rf image/infra-server image/static - ############# ## Testing ## ############# diff --git a/image/Dockerfile b/image/Dockerfile index ba5d2d183..20ac8f11f 100644 --- a/image/Dockerfile +++ b/image/Dockerfile @@ -1,7 +1,23 @@ -FROM alpine:3.14 +FROM golang:1.20.7 as golang-builder -COPY infra-server /infra-server +WORKDIR /go/src/github.com/stackrox/infra -COPY static /etc/infra/static +COPY . . + +RUN make server cli + +FROM node:16.20.2 as ui-builder + +COPY ui ui + +RUN --mount=type=secret,id=npmrc,target=/root/.npmrc make -C ui all + +FROM alpine:3.14 as app + +COPY --from=golang-builder /go/src/github.com/stackrox/infra/bin/infra-server-linux-amd64 /infra-server + +COPY --from=ui-builder /ui/build /etc/infra/static + +COPY --from=golang-builder /go/src/github.com/stackrox/infra/bin/infractl-* /etc/infra/static/downloads/ ENTRYPOINT ["/infra-server"] diff --git a/image/Dockerfile.multi b/image/Dockerfile.multi deleted file mode 100644 index 20ac8f11f..000000000 --- a/image/Dockerfile.multi +++ /dev/null @@ -1,23 +0,0 @@ -FROM golang:1.20.7 as golang-builder - -WORKDIR /go/src/github.com/stackrox/infra - -COPY . . - -RUN make server cli - -FROM node:16.20.2 as ui-builder - -COPY ui ui - -RUN --mount=type=secret,id=npmrc,target=/root/.npmrc make -C ui all - -FROM alpine:3.14 as app - -COPY --from=golang-builder /go/src/github.com/stackrox/infra/bin/infra-server-linux-amd64 /infra-server - -COPY --from=ui-builder /ui/build /etc/infra/static - -COPY --from=golang-builder /go/src/github.com/stackrox/infra/bin/infractl-* /etc/infra/static/downloads/ - -ENTRYPOINT ["/infra-server"] From ca7882e581ecea6e72195770505d50ff3ff365b5 Mon Sep 17 00:00:00 2001 From: Tom Martensen Date: Tue, 12 Sep 2023 14:12:33 +0200 Subject: [PATCH 6/6] bump base images --- image/Dockerfile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/image/Dockerfile b/image/Dockerfile index 20ac8f11f..6f76b10b7 100644 --- a/image/Dockerfile +++ b/image/Dockerfile @@ -1,4 +1,4 @@ -FROM golang:1.20.7 as golang-builder +FROM golang:1.20.8 as golang-builder WORKDIR /go/src/github.com/stackrox/infra @@ -12,7 +12,7 @@ COPY ui ui RUN --mount=type=secret,id=npmrc,target=/root/.npmrc make -C ui all -FROM alpine:3.14 as app +FROM alpine:3.18.3 as app COPY --from=golang-builder /go/src/github.com/stackrox/infra/bin/infra-server-linux-amd64 /infra-server