Files
rhein-sw-website/.gitlab-ci.yml

115 lines
3.1 KiB
YAML

image: node:20
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
build:
stage: build
script:
- npm install
- npx next build
- npm run lint
dockerize:
stage: docker
image: docker:latest
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:
stage: deploy
image: node:20
before_script: *install_deploy_key
script:
- |
if [[ "$CI_COMMIT_REF_NAME" == "production" ]]; then
TAG="latest"
PORT="4100"
elif [[ "$CI_COMMIT_REF_NAME" == "dev" ]]; then
TAG="dev"
PORT="5100"
else
echo "Skipping deployment for branch $CI_COMMIT_REF_NAME"
exit 0
fi
CONTAINER_NAME="$CI_PROJECT_NAME-$CI_COMMIT_REF_NAME"
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
"
only:
- dev
- production
# ------------------ New sync job added below ------------------
sync_branches:
stage: sync
image: node:20 # <-- reuse existing node image
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