Files
rhein-sw-website/.gitlab-ci.yml
Thatsaphorn Atchariyaphap d9ea610423 Merge branch 'dev' into 'production'
Merge branch 'pipeline-refactoring' from 'dev' into 'production'

See merge request rheinsw/website!12
2025-03-29 21:34:55 +00:00

119 lines
3.2 KiB
YAML

image: node:20@sha256:735b1ba7e4550c415f98568efbf527e3f75828ac4f10692e490ca78e11d89f6e
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/
variables:
NEXT_PUBLIC_ENV: "production"
OUTPUT_DIR: ".next"
PROJECT_NAME: $CI_PROJECT_NAME
DOCKER_IMAGE: "registry.boomlab.party/rheinsw/$CI_PROJECT_NAME"
# 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_production_rule: &deploy_production_rule
- if: $CI_COMMIT_BRANCH == "production"
when: manual
allow_failure: 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
services:
- docker:dind
before_script:
- echo "$CI_REGISTRY_PASSWORD" | docker login "$CI_REGISTRY" -u "$CI_REGISTRY_USER" --password-stdin
script:
- |
if [[ "$CI_COMMIT_REF_NAME" == "production" ]]; then
TAG="latest"
elif [[ "$CI_COMMIT_REF_NAME" == "dev" ]]; then
TAG="dev"
else
echo "Skipping Docker build for branch $CI_COMMIT_REF_NAME"
exit 0
fi
echo "Using tag: $TAG"
docker build -t $DOCKER_IMAGE:$TAG -f Dockerfile .
docker push $DOCKER_IMAGE:$TAG
only:
- dev
- production
deploy_dev:
stage: deploy
before_script: *install_deploy_key
script:
- TAG="dev"
- PORT="5100"
- CONTAINER_NAME="$CI_PROJECT_NAME-dev"
- *deploy_script
only:
- dev
deploy_production:
stage: deploy
rules: *deploy_production_rule
before_script: *install_deploy_key
script:
- TAG="latest"
- PORT="4100"
- CONTAINER_NAME="$CI_PROJECT_NAME-production"
- *deploy_script
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