From bd26f2cd8f29e58bc64eb3ebea4889eeecdaad07 Mon Sep 17 00:00:00 2001 From: Claudemir Todo Bom Date: Sat, 3 Aug 2024 10:56:41 -0300 Subject: [PATCH 1/3] Remove gitlab-cy.yml from main --- .gitlab-ci.yml | 64 -------------------------------------------------- 1 file changed, 64 deletions(-) delete mode 100644 .gitlab-ci.yml diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml deleted file mode 100644 index 4cb66a9..0000000 --- a/.gitlab-ci.yml +++ /dev/null @@ -1,64 +0,0 @@ -stages: - - build - -variables: - REGISTRY: "ghcr.io" - IMAGE_BACKEND: "ticketz-oss/ticketz-backend" - IMAGE_FRONTEND: "ticketz-oss/ticketz-frontend" - DOCKER_CONFIG: /kaniko/.docker/ - -before_script: - - echo "{\"auths\":{\"$REGISTRY\":{\"username\":\"$CI_REGISTRY_USER\",\"password\":\"$CI_REGISTRY_PASSWORD\"}}}" > /kaniko/.docker/config.json - - export BUILD_TIMESTAMP=$(date -u +"%Y-%m-%dT%H:%M:%SZ") - -# Build and push backend Docker image -build-and-push-backend: - stage: build - image: - name: gcr.io/kaniko-project/executor:v1.14.0-debug - entrypoint: [""] - rules: - - if: '$CI_COMMIT_TAG =~ /-pro$/' - script: - - | - cat < backend/src/gitinfo.ts - export const GitInfo = { - commitHash: "${CI_COMMIT_SHORT_SHA}", - commitTimestamp: "N/A", - branchName: "${CI_COMMIT_REF_NAME", - tagName: "${CI_COMMIT_TAG}", - buildTimestamp: "${BUILD_TIMESTAMP}" - }; - EOT - - | - /kaniko/executor --context "${CI_PROJECT_DIR}/backend" \ - --dockerfile "${CI_PROJECT_DIR}/backend/Dockerfile" \ - --destination "${REGISTRY}/${IMAGE_BACKEND}:${CI_COMMIT_TAG}" \ - --destination "${REGISTRY}/${IMAGE_BACKEND}:latest" \ - --build-arg PLATFORM=linux/amd64,linux/arm64 - -# Build and push frontend Docker image -build-and-push-frontend: - stage: build - image: - name: gcr.io/kaniko-project/executor:v1.14.0-debug - entrypoint: [""] - rules: - - if: '$CI_COMMIT_TAG =~ /-pro$/' - script: - - | - cat < frontend/public/gitinfo.json - { - "commitHash": "${CI_COMMIT_SHORT_SHA}", - "commitTimestamp": "N/A", - "branchName": "${CI_COMMIT_REF_NAME", - "tagName": "${CI_COMMIT_TAG}", - "buildTimestamp": "${BUILD_TIMESTAMP}" - } - EOT - - | - /kaniko/executor --context "${CI_PROJECT_DIR}/frontend" \ - --dockerfile "${CI_PROJECT_DIR}/frontend/Dockerfile" \ - --destination "${REGISTRY}/${IMAGE_FRONTEND}:${CI_COMMIT_TAG}" \ - --destination "${REGISTRY}/${IMAGE_FRONTEND}:latest" \ - --build-arg PLATFORM=linux/amd64,linux/arm64 From 2f87628661232530e34383af6563d4d2001e28ac Mon Sep 17 00:00:00 2001 From: Claudemir Todo Bom Date: Mon, 5 Aug 2024 08:34:56 -0300 Subject: [PATCH 2/3] organize multi-arch build --- .gitlab-ci.yml | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index b60e661..4b8b25b 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,7 +1,8 @@ image: docker:20.10.16 stages: - - build + - build-amd64 + - build-arm64 - tag-images variables: @@ -13,6 +14,12 @@ variables: DOCKER_CLI_EXPERIMENTAL: enabled DOCKER_HOST: tcp://docker:2375 DOCKER_TLS_CERTDIR: "" + +cache: + key: npm-cache + paths: + - .buildx-cache + services: - docker:20.10.16-dind @@ -23,13 +30,13 @@ before_script: - export TAG_MINOR=$(echo $BASE_TAG | cut -d'.' -f1,2) - export TAG_PATCH=$(echo $BASE_TAG | cut -d'.' -f1,2,3) - export BUILD_TIMESTAMP=$(date -u +"%Y-%m-%dT%H:%M:%SZ") - - docker login $REGISTRY -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD + - echo $CI_REGISTRY_PASSWORD | docker login $REGISTRY -u $CI_REGISTRY_USER --password-stdin - docker run --privileged --rm tonistiigi/binfmt --install all - docker buildx create --name mybuilder --use - docker buildx inspect --bootstrap build-backend-amd64: - stage: build + stage: build-amd64 only: - /^.*-(pro|theme)+$/ script: @@ -45,10 +52,12 @@ build-backend-amd64: EOT - | docker buildx build --platform linux/amd64 --push \ - -t $REGISTRY/$IMAGE_FRONTEND:amd64-$CI_COMMIT_SHORT_SHA backend + --cache-from=type=local,src=.buildx-cache \ + --cache-to=type=local,dest=.buildx-cache \ + -t $REGISTRY/$IMAGE_BACKEND:amd64-$CI_COMMIT_SHORT_SHA backend build-backend-arm64: - stage: build + stage: build-arm64 only: - /^.*-(pro|theme)+$/ script: @@ -64,10 +73,12 @@ build-backend-arm64: EOT - | docker buildx build --platform linux/arm64 --push \ - -t $REGISTRY/$IMAGE_FRONTEND:arm64-$CI_COMMIT_SHORT_SHA backend + --cache-from=type=local,src=.buildx-cache \ + --cache-to=type=local,dest=.buildx-cache \ + -t $REGISTRY/$IMAGE_BACKEND:arm64-$CI_COMMIT_SHORT_SHA backend build-frontend-amd64: - stage: build + stage: build=-amd64 only: - /^.*-(pro|theme)+$/ script: @@ -83,12 +94,14 @@ build-frontend-amd64: EOT - | docker buildx build --platform linux/amd64 --push \ + --cache-from=type=local,src=.buildx-cache \ + --cache-to=type=local,dest=.buildx-cache \ -t $REGISTRY/$IMAGE_FRONTEND:amd64-$CI_COMMIT_SHORT_SHA frontend build-frontend-arm64: only: - /^.*-(pro|theme)+$/ - stage: build + stage: build-arm64 script: - | cat < frontend/public/gitinfo.json @@ -102,6 +115,8 @@ build-frontend-arm64: EOT - | docker buildx build --platform linux/arm64 --push \ + --cache-from=type=local,src=.buildx-cache \ + --cache-to=type=local,dest=.buildx-cache \ -t $REGISTRY/$IMAGE_FRONTEND:arm64-$CI_COMMIT_SHORT_SHA frontend From 493897759d95752a08de0bcc385bb49286bb1d67 Mon Sep 17 00:00:00 2001 From: Claudemir Todo Bom Date: Mon, 5 Aug 2024 08:38:07 -0300 Subject: [PATCH 3/3] Use buildkit cache --- backend/Dockerfile | 4 ++-- frontend/Dockerfile | 5 ++--- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/backend/Dockerfile b/backend/Dockerfile index 8199a95..c26381b 100644 --- a/backend/Dockerfile +++ b/backend/Dockerfile @@ -4,8 +4,8 @@ FROM node:20 AS build WORKDIR /usr/src/app COPY . . -RUN npm install \ - && npm run build +RUN --mount=type=cache,target=/root/.npm \ + npm install && npm run build # Stage 2: Create the final image without source files FROM ghcr.io/ticketz-oss/node diff --git a/frontend/Dockerfile b/frontend/Dockerfile index a11d5eb..5c74c02 100644 --- a/frontend/Dockerfile +++ b/frontend/Dockerfile @@ -5,9 +5,8 @@ COPY . . ENV NODE_OPTIONS --openssl-legacy-provider -RUN npm install \ - && echo -e "\n\nBulding...\n" \ - && npm run build +RUN --mount=type=cache,target=/root/.npm \ + npm install && npm run build FROM ghcr.io/ticketz-oss/nginx-alpine