DONE: separate repo for be and fe, update cicd
Build Docker Image / build-backend (push) Successful in 1m18s

This commit is contained in:
2026-10-25 11:59:58 +08:00
parent b0475e3371
commit 337be0f332
6 changed files with 187 additions and 32 deletions
+42
View File
@@ -0,0 +1,42 @@
name: Build Docker Image
on:
push:
tags:
- "v*"
jobs:
build-backend:
runs-on: docker
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Login to Docker registry
run: |
echo "${{ secrets.REGISTRY_PASSWORD }}" | \
docker login git.koppkb.com \
-u "${{ secrets.REGISTRY_USERNAME }}" \
--password-stdin
- name: Build & push backend image
run: |
set -e
IMAGE="git.koppkb.com/kopkb/my-kopkb-be"
TAGS="-t ${IMAGE}:${{ gitea.sha }}"
if [ "${{ gitea.ref_type }}" = "tag" ] && echo "${{ gitea.ref_name }}" | grep -q '^v'; then
TAGS="${TAGS} -t ${IMAGE}:${{ gitea.ref_name }}"
fi
echo "Building and pushing backend: ${TAGS}"
docker buildx build \
--target production \
--platform linux/amd64 \
--cache-from type=registry,ref=${IMAGE}:buildcache,ignore-error=true \
--cache-to type=registry,ref=${IMAGE}:buildcache,mode=max \
-f docker/common/unified/Dockerfile \
${TAGS} \
--push .
+130
View File
@@ -0,0 +1,130 @@
name: Deploy to Production
# Manual production deploy — pulls pre-built images from the registry
on:
workflow_dispatch:
inputs:
image_tag:
description: "Docker image tag to deploy (commit SHA or version tag, e.g. v1.0.0)"
required: true
default: "latest"
type: string
env:
BACKEND_IMAGE: git.koppkb.com/kopkb/my-kopkb-be
DEPLOY_DIR: /home/arrahn/Project
jobs:
deploy:
runs-on: host
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
ref: ${{ inputs.image_tag }}
- name: Login to Docker registry
run: |
echo "${{ secrets.REGISTRY_PASSWORD }}" | \
docker login git.koppkb.com \
-u "${{ secrets.REGISTRY_USERNAME }}" \
--password-stdin
- name: Verify images exist in registry
run: |
set -e
IMAGE_TAG="${{ inputs.image_tag }}"
IMAGE="${BACKEND_IMAGE}:${IMAGE_TAG}"
echo "Checking if image exists: ${IMAGE}"
if ! docker manifest inspect "${IMAGE}" > /dev/null 2>&1; then
echo "ERROR: Image ${IMAGE} does not exist in registry!"
exit 1
fi
echo "✓ ${IMAGE} found"
- name: Pull Docker images
run: |
set -e
IMAGE_TAG="${{ inputs.image_tag }}"
docker pull "${BACKEND_IMAGE}:${IMAGE_TAG}"
echo "✓ Image pulled successfully"
- name: Sync compose file and deploy
run: |
set -e
IMAGE_TAG="${{ inputs.image_tag }}"
mkdir -p "${DEPLOY_DIR}"
cp docker/production/docker-compose.production.yml "${DEPLOY_DIR}/docker-compose.yml"
ENV_FILE=".env.production"
if [ ! -f "${ENV_FILE}" ]; then
echo "ERROR: ${ENV_FILE} is missing from the repository checkout"
exit 1
fi
cp "${ENV_FILE}" "${DEPLOY_DIR}/.env"
chmod 600 "${DEPLOY_DIR}/.env"
NGINX_CONF="$(pwd)/docker/production/nginx/nginx.conf"
if [ ! -f "${NGINX_CONF}" ]; then
echo "ERROR: nginx config not found at ${NGINX_CONF}"
exit 1
fi
cd "${DEPLOY_DIR}"
export IMAGE_TAG
export NGINX_CONF
docker rm -f mykopkb-be-production || true
docker compose -f docker-compose.yml pull backend
docker compose -f docker-compose.yml up -d backend
echo "✓ Deployed backend IMAGE_TAG=${IMAGE_TAG}"
- name: Verify deployment
run: |
set -e
cd "${DEPLOY_DIR}"
echo "Waiting for services to start..."
sleep 10
STATUS=$(docker compose -f docker-compose.yml ps --status running --format '{{.Name}}' backend 2>/dev/null || true)
if [ -z "$STATUS" ]; then
echo "ERROR: backend is not running"
docker compose -f docker-compose.yml ps
docker compose -f docker-compose.yml logs --tail=50 backend || true
exit 1
fi
echo "✓ backend is running (${STATUS})"
BE_CONTAINER=$(docker compose -f docker-compose.yml ps -q backend)
if docker exec "$BE_CONTAINER" test -f /var/www/.env; then
echo "✓ Backend .env file exists"
else
echo "WARNING: Backend .env file not found"
fi
API_PORT=$(grep -E '^API_PORT=' .env 2>/dev/null | cut -d= -f2- | tr -d '"' || true)
API_PORT=${API_PORT:-8080}
check_health() {
NAME=$1
URL=$2
for i in 1 2 3 4 5 6; do
if curl -sf "$URL" > /dev/null; then
echo "✓ ${NAME} health check passed (${URL})"
return 0
fi
echo "Waiting for ${NAME}... attempt ${i}/6"
sleep 5
done
echo "ERROR: ${NAME} health check failed (${URL})"
docker compose -f docker-compose.yml logs --tail=50 "${NAME}" || true
return 1
}
check_health "backend" "http://127.0.0.1:${API_PORT}/health"