From 46676c1e4a8bf67b99ef55f0fb9c2105719c816b Mon Sep 17 00:00:00 2001 From: Thatsaphorn Atchariyaphap Date: Sun, 27 Apr 2025 20:17:08 +0200 Subject: [PATCH 01/10] Add GitLab CI/CD pipeline configuration (dummy) --- .gitlab-ci.yml | 39 ++++++++++++++++++++++++++++++++ backend/.gitlab-ci-backend.yml | 7 ++++++ frontend/.gitlab-ci-frontend.yml | 7 ++++++ 3 files changed, 53 insertions(+) create mode 100644 .gitlab-ci.yml create mode 100644 backend/.gitlab-ci-backend.yml create mode 100644 frontend/.gitlab-ci-frontend.yml diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml new file mode 100644 index 0000000..a642a2c --- /dev/null +++ b/.gitlab-ci.yml @@ -0,0 +1,39 @@ +image: docker:20.10@sha256:2967f0819c84dd589ed0a023b9d25dcfe7a3c123d5bf784ffbb77edf55335f0c + +include: + - local: 'backend/.gitlab-ci-backend.yml' + - local: 'frontend/.gitlab-ci-frontend.yml' + +stages: + - sync + - deploy + - backend_build + - frontend_build + +sync_branches: + stage: sync + rules: + - if: '$CI_COMMIT_BRANCH == "production"' + before_script: + - git config --global user.email "gitlab-ci@rhein-software.dev" + - git config --global user.name "GitLab CI" + script: + - git remote set-url origin "https://oauth2:${CI_JOB_TOKEN}@${CI_SERVER_HOST}/${CI_PROJECT_PATH}.git" + - git checkout dev + - git pull origin dev + - git merge --no-ff origin/production + - git push origin dev + +deploy_dev: + stage: deploy + rules: + - if: '$CI_COMMIT_BRANCH == "dev"' + script: + - echo "Deploying DEV environment" + +deploy_production: + stage: deploy + rules: + - if: '$CI_COMMIT_BRANCH == "production"' + script: + - echo "Deploying PROD environment" diff --git a/backend/.gitlab-ci-backend.yml b/backend/.gitlab-ci-backend.yml new file mode 100644 index 0000000..36bcc18 --- /dev/null +++ b/backend/.gitlab-ci-backend.yml @@ -0,0 +1,7 @@ +stages: + - backend_build + +backend_build_job: + stage: backend_build + script: + - echo "Building backend project on branch: $CI_COMMIT_BRANCH" diff --git a/frontend/.gitlab-ci-frontend.yml b/frontend/.gitlab-ci-frontend.yml new file mode 100644 index 0000000..1cc5590 --- /dev/null +++ b/frontend/.gitlab-ci-frontend.yml @@ -0,0 +1,7 @@ +stages: + - frontend_build + +frontend_build_job: + stage: frontend_build + script: + - echo "Building frontend project on branch: $CI_COMMIT_BRANCH" -- 2.39.5 From 5e659d0175131653239fd97c9f0b8391d4f55493 Mon Sep 17 00:00:00 2001 From: Thatsaphorn Atchariyaphap Date: Sun, 27 Apr 2025 21:05:41 +0200 Subject: [PATCH 02/10] Simplify CI configurations for frontend and backend projects. --- ...{.gitlab-ci-backend.yml => .gitlab-ci.yml} | 2 +- frontend/.gitlab-ci-frontend.yml | 7 -- frontend/.gitlab-ci.yml | 116 +----------------- frontend/.gitlab-ci_old.yml | 115 +++++++++++++++++ 4 files changed, 120 insertions(+), 120 deletions(-) rename backend/{.gitlab-ci-backend.yml => .gitlab-ci.yml} (54%) delete mode 100644 frontend/.gitlab-ci-frontend.yml create mode 100644 frontend/.gitlab-ci_old.yml diff --git a/backend/.gitlab-ci-backend.yml b/backend/.gitlab-ci.yml similarity index 54% rename from backend/.gitlab-ci-backend.yml rename to backend/.gitlab-ci.yml index 36bcc18..aa774f8 100644 --- a/backend/.gitlab-ci-backend.yml +++ b/backend/.gitlab-ci.yml @@ -4,4 +4,4 @@ stages: backend_build_job: stage: backend_build script: - - echo "Building backend project on branch: $CI_COMMIT_BRANCH" + - echo "Building backend project" diff --git a/frontend/.gitlab-ci-frontend.yml b/frontend/.gitlab-ci-frontend.yml deleted file mode 100644 index 1cc5590..0000000 --- a/frontend/.gitlab-ci-frontend.yml +++ /dev/null @@ -1,7 +0,0 @@ -stages: - - frontend_build - -frontend_build_job: - stage: frontend_build - script: - - echo "Building frontend project on branch: $CI_COMMIT_BRANCH" diff --git a/frontend/.gitlab-ci.yml b/frontend/.gitlab-ci.yml index 589e155..e9b0d33 100644 --- a/frontend/.gitlab-ci.yml +++ b/frontend/.gitlab-ci.yml @@ -1,115 +1,7 @@ -image: node:22@sha256:f6b9c31ace05502dd98ef777aaa20464362435dcc5e312b0e213121dcf7d8b95 - -workflow: - rules: - - if: $CI_PIPELINE_SOURCE == "merge_request_event" - - if: $CI_COMMIT_BRANCH == "production" - - if: $CI_COMMIT_BRANCH == "dev" - - if: $CI_COMMIT_TAG =~ /^v[\d]{1,4}\.[\d]{1,2}\.[\d]{1,2}$/ - stages: - - build - - docker - - deploy - - sync + - frontend_build -cache: - key: ${CI_COMMIT_REF_SLUG} - paths: - - node_modules/ - - .next/cache/ - -variables: - NEXT_PUBLIC_ENV: "production" - OUTPUT_DIR: ".next" - PROJECT_NAME: $CI_PROJECT_NAME - DOCKER_IMAGE: "registry.boomlab.party/rheinsw/$CI_PROJECT_NAME" - -.deploy_production_rule: &deploy_production_rule - - if: $CI_COMMIT_BRANCH == "production" - when: manual - allow_failure: true - -# Reusable SSH key setup block -.install_deploy_key: &install_deploy_key - - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )' - - mkdir -p ~/.ssh - - echo "$DEPLOY_KEY_BASE64" | base64 -d > ~/.ssh/deploy_key - - eval "$(ssh-agent -s)" - - chmod 600 ~/.ssh/deploy_key - - ssh-add ~/.ssh/deploy_key - - ssh-keyscan -p 22 -H '192.168.41.101' >> ~/.ssh/known_hosts || true - -.deploy_script: &deploy_script - - | - echo "Deploying $DOCKER_IMAGE:$TAG to $CONTAINER_NAME on port $PORT..." - - ssh gitlab@192.168.41.101 -p 22 " - echo \"$CI_REGISTRY_PASSWORD\" | docker login $CI_REGISTRY -u \"$CI_REGISTRY_USER\" --password-stdin && - docker pull $DOCKER_IMAGE:$TAG && - docker stop $CONTAINER_NAME || true && - docker rm $CONTAINER_NAME || true && - docker run -d --name $CONTAINER_NAME -p $PORT:3000 $DOCKER_IMAGE:$TAG - " - -build: - stage: build +frontend_build_job: + stage: frontend_build script: - - npm install - - npx next build - - npm run lint - -dockerize: - stage: docker - image: docker:20.10@sha256:2967f0819c84dd589ed0a023b9d25dcfe7a3c123d5bf784ffbb77edf55335f0c - before_script: - - echo "$CI_REGISTRY_PASSWORD" | docker login "$CI_REGISTRY" -u "$CI_REGISTRY_USER" --password-stdin - script: - - | - RAW_TAG="$CI_COMMIT_REF_NAME" - TAG="${RAW_TAG//\//_}" # replaces "/" with "_" - echo "Sanitized tag: $TAG" - docker build -t $DOCKER_IMAGE:$TAG -f Dockerfile . - - if [[ "$RAW_TAG" == "dev" || "$RAW_TAG" == "production" ]]; then - echo "Pushing Docker image $DOCKER_IMAGE:$TAG" - docker push $DOCKER_IMAGE:$TAG - else - echo "Skipping Docker push for non-dev or production branch: $RAW_TAG" - fi - - -deploy_production: - stage: deploy - rules: *deploy_production_rule - before_script: *install_deploy_key - script: - - TAG="production" - - PORT="4100" - - CONTAINER_NAME="$CI_PROJECT_NAME-production" - - *deploy_script - -deploy_dev: - stage: deploy - before_script: *install_deploy_key - script: - - TAG="dev" - - PORT="5100" - - CONTAINER_NAME="$CI_PROJECT_NAME-dev" - - *deploy_script - only: - - dev - -sync_branches: - stage: sync - rules: - - if: '$CI_COMMIT_BRANCH == "production"' - before_script: - - git config --global user.email "gitlab-ci@rheinsw.com" - - git config --global user.name "GitLab CI" - script: - - git remote set-url origin "https://oauth2:${CI_JOB_TOKEN}@${CI_SERVER_HOST}/${CI_PROJECT_PATH}.git" - - git checkout dev - - git pull origin dev - - git merge --no-ff origin/production - - git push origin dev + - echo "Building frontend project" diff --git a/frontend/.gitlab-ci_old.yml b/frontend/.gitlab-ci_old.yml new file mode 100644 index 0000000..589e155 --- /dev/null +++ b/frontend/.gitlab-ci_old.yml @@ -0,0 +1,115 @@ +image: node:22@sha256:f6b9c31ace05502dd98ef777aaa20464362435dcc5e312b0e213121dcf7d8b95 + +workflow: + rules: + - if: $CI_PIPELINE_SOURCE == "merge_request_event" + - if: $CI_COMMIT_BRANCH == "production" + - if: $CI_COMMIT_BRANCH == "dev" + - if: $CI_COMMIT_TAG =~ /^v[\d]{1,4}\.[\d]{1,2}\.[\d]{1,2}$/ + +stages: + - build + - docker + - deploy + - sync + +cache: + key: ${CI_COMMIT_REF_SLUG} + paths: + - node_modules/ + - .next/cache/ + +variables: + NEXT_PUBLIC_ENV: "production" + OUTPUT_DIR: ".next" + PROJECT_NAME: $CI_PROJECT_NAME + DOCKER_IMAGE: "registry.boomlab.party/rheinsw/$CI_PROJECT_NAME" + +.deploy_production_rule: &deploy_production_rule + - if: $CI_COMMIT_BRANCH == "production" + when: manual + allow_failure: true + +# Reusable SSH key setup block +.install_deploy_key: &install_deploy_key + - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )' + - mkdir -p ~/.ssh + - echo "$DEPLOY_KEY_BASE64" | base64 -d > ~/.ssh/deploy_key + - eval "$(ssh-agent -s)" + - chmod 600 ~/.ssh/deploy_key + - ssh-add ~/.ssh/deploy_key + - ssh-keyscan -p 22 -H '192.168.41.101' >> ~/.ssh/known_hosts || true + +.deploy_script: &deploy_script + - | + echo "Deploying $DOCKER_IMAGE:$TAG to $CONTAINER_NAME on port $PORT..." + + ssh gitlab@192.168.41.101 -p 22 " + echo \"$CI_REGISTRY_PASSWORD\" | docker login $CI_REGISTRY -u \"$CI_REGISTRY_USER\" --password-stdin && + docker pull $DOCKER_IMAGE:$TAG && + docker stop $CONTAINER_NAME || true && + docker rm $CONTAINER_NAME || true && + docker run -d --name $CONTAINER_NAME -p $PORT:3000 $DOCKER_IMAGE:$TAG + " + +build: + stage: build + script: + - npm install + - npx next build + - npm run lint + +dockerize: + stage: docker + image: docker:20.10@sha256:2967f0819c84dd589ed0a023b9d25dcfe7a3c123d5bf784ffbb77edf55335f0c + before_script: + - echo "$CI_REGISTRY_PASSWORD" | docker login "$CI_REGISTRY" -u "$CI_REGISTRY_USER" --password-stdin + script: + - | + RAW_TAG="$CI_COMMIT_REF_NAME" + TAG="${RAW_TAG//\//_}" # replaces "/" with "_" + echo "Sanitized tag: $TAG" + docker build -t $DOCKER_IMAGE:$TAG -f Dockerfile . + + if [[ "$RAW_TAG" == "dev" || "$RAW_TAG" == "production" ]]; then + echo "Pushing Docker image $DOCKER_IMAGE:$TAG" + docker push $DOCKER_IMAGE:$TAG + else + echo "Skipping Docker push for non-dev or production branch: $RAW_TAG" + fi + + +deploy_production: + stage: deploy + rules: *deploy_production_rule + before_script: *install_deploy_key + script: + - TAG="production" + - PORT="4100" + - CONTAINER_NAME="$CI_PROJECT_NAME-production" + - *deploy_script + +deploy_dev: + stage: deploy + before_script: *install_deploy_key + script: + - TAG="dev" + - PORT="5100" + - CONTAINER_NAME="$CI_PROJECT_NAME-dev" + - *deploy_script + only: + - dev + +sync_branches: + stage: sync + rules: + - if: '$CI_COMMIT_BRANCH == "production"' + before_script: + - git config --global user.email "gitlab-ci@rheinsw.com" + - git config --global user.name "GitLab CI" + script: + - git remote set-url origin "https://oauth2:${CI_JOB_TOKEN}@${CI_SERVER_HOST}/${CI_PROJECT_PATH}.git" + - git checkout dev + - git pull origin dev + - git merge --no-ff origin/production + - git push origin dev -- 2.39.5 From ac30a7be719a69e20fcefd5950ace3664d98b424 Mon Sep 17 00:00:00 2001 From: Thatsaphorn Atchariyaphap Date: Sun, 27 Apr 2025 21:06:35 +0200 Subject: [PATCH 03/10] Update GitLab CI includes --- .gitlab-ci.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index a642a2c..0250d88 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,8 +1,8 @@ image: docker:20.10@sha256:2967f0819c84dd589ed0a023b9d25dcfe7a3c123d5bf784ffbb77edf55335f0c include: - - local: 'backend/.gitlab-ci-backend.yml' - - local: 'frontend/.gitlab-ci-frontend.yml' + - local: 'backend/.gitlab-ci.yml' + - local: 'frontend/.gitlab-ci.yml' stages: - sync -- 2.39.5 From 4de500608c3526beb9a5dd596b0e885d0dec8507 Mon Sep 17 00:00:00 2001 From: Thatsaphorn Atchariyaphap Date: Sun, 27 Apr 2025 21:08:38 +0200 Subject: [PATCH 04/10] Reorder stages in .gitlab-ci.yml for correct execution order. - Moved the 'deploy' stage to the end of the stages list. --- .gitlab-ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 0250d88..42ce980 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -6,9 +6,9 @@ include: stages: - sync - - deploy - backend_build - frontend_build + - deploy sync_branches: stage: sync -- 2.39.5 From 838bd2744dcbec3477a07cbcd6e15819d39803a0 Mon Sep 17 00:00:00 2001 From: Thatsaphorn Atchariyaphap Date: Sun, 27 Apr 2025 22:00:33 +0200 Subject: [PATCH 06/10] Moved sync stage to the end of the pipeline and adapt the stage to use an Alpine Git image --- .gitlab-ci.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 42ce980..73eee31 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -5,13 +5,14 @@ include: - local: 'frontend/.gitlab-ci.yml' stages: - - sync - backend_build - frontend_build - deploy + - sync sync_branches: stage: sync + image: alpine:git@sha256:47a8a580ba3cbc85737c5dccc50078cce5662b84a6c5eaaf5800a841ff5a19c8 rules: - if: '$CI_COMMIT_BRANCH == "production"' before_script: -- 2.39.5 From 338eb8e92daf5f52046119c5a62b5b93237f2b67 Mon Sep 17 00:00:00 2001 From: Thatsaphorn Atchariyaphap Date: Sun, 27 Apr 2025 20:17:08 +0200 Subject: [PATCH 07/10] Add GitLab CI/CD pipeline configuration (dummy) --- backend/.gitlab-ci-backend.yml | 7 +++++++ frontend/.gitlab-ci-frontend.yml | 7 +++++++ 2 files changed, 14 insertions(+) create mode 100644 backend/.gitlab-ci-backend.yml create mode 100644 frontend/.gitlab-ci-frontend.yml diff --git a/backend/.gitlab-ci-backend.yml b/backend/.gitlab-ci-backend.yml new file mode 100644 index 0000000..36bcc18 --- /dev/null +++ b/backend/.gitlab-ci-backend.yml @@ -0,0 +1,7 @@ +stages: + - backend_build + +backend_build_job: + stage: backend_build + script: + - echo "Building backend project on branch: $CI_COMMIT_BRANCH" diff --git a/frontend/.gitlab-ci-frontend.yml b/frontend/.gitlab-ci-frontend.yml new file mode 100644 index 0000000..1cc5590 --- /dev/null +++ b/frontend/.gitlab-ci-frontend.yml @@ -0,0 +1,7 @@ +stages: + - frontend_build + +frontend_build_job: + stage: frontend_build + script: + - echo "Building frontend project on branch: $CI_COMMIT_BRANCH" -- 2.39.5 From efc906297ed34632121ab06089f87d930a30bdc5 Mon Sep 17 00:00:00 2001 From: Thatsaphorn Atchariyaphap Date: Sun, 27 Apr 2025 21:05:41 +0200 Subject: [PATCH 08/10] Simplify CI configurations for frontend and backend projects. --- backend/.gitlab-ci-backend.yml | 7 ------- frontend/.gitlab-ci-frontend.yml | 7 ------- 2 files changed, 14 deletions(-) delete mode 100644 backend/.gitlab-ci-backend.yml delete mode 100644 frontend/.gitlab-ci-frontend.yml diff --git a/backend/.gitlab-ci-backend.yml b/backend/.gitlab-ci-backend.yml deleted file mode 100644 index 36bcc18..0000000 --- a/backend/.gitlab-ci-backend.yml +++ /dev/null @@ -1,7 +0,0 @@ -stages: - - backend_build - -backend_build_job: - stage: backend_build - script: - - echo "Building backend project on branch: $CI_COMMIT_BRANCH" diff --git a/frontend/.gitlab-ci-frontend.yml b/frontend/.gitlab-ci-frontend.yml deleted file mode 100644 index 1cc5590..0000000 --- a/frontend/.gitlab-ci-frontend.yml +++ /dev/null @@ -1,7 +0,0 @@ -stages: - - frontend_build - -frontend_build_job: - stage: frontend_build - script: - - echo "Building frontend project on branch: $CI_COMMIT_BRANCH" -- 2.39.5 From 68f63ea9e7b7b0e4a1b512aa5a7d2dacb761874a Mon Sep 17 00:00:00 2001 From: Thatsaphorn Atchariyaphap Date: Sun, 27 Apr 2025 22:15:52 +0200 Subject: [PATCH 10/10] Fix wrong image definition. --- .gitlab-ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 73eee31..0c59767 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -12,7 +12,7 @@ stages: sync_branches: stage: sync - image: alpine:git@sha256:47a8a580ba3cbc85737c5dccc50078cce5662b84a6c5eaaf5800a841ff5a19c8 + image: alpine/git:v2.47.2@sha256:47a8a580ba3cbc85737c5dccc50078cce5662b84a6c5eaaf5800a841ff5a19c8 rules: - if: '$CI_COMMIT_BRANCH == "production"' before_script: -- 2.39.5