From c2295ec513ede3671a66f0035e917eb76d2edd85 Mon Sep 17 00:00:00 2001 From: wangshouren <1115808717@qq.com> Date: Thu, 1 Aug 2024 11:06:06 +0800 Subject: [PATCH] ci(npcs): update --- .github/workflows/docker-deploy-staging.yml | 4 +- .github/workflows/docker-deploy.yml | 4 +- deploy/{ => production}/.env.local.example | 0 deploy/production/README.md | 22 +++++ deploy/{ => production}/docker-compose.yml | 22 ++++- deploy/staging/.env.local.example | 6 ++ deploy/{ => staging}/README.md | 2 +- deploy/staging/docker-compose.yml | 94 +++++++++++++++++++++ 8 files changed, 147 insertions(+), 7 deletions(-) rename deploy/{ => production}/.env.local.example (100%) create mode 100644 deploy/production/README.md rename deploy/{ => production}/docker-compose.yml (85%) create mode 100644 deploy/staging/.env.local.example rename deploy/{ => staging}/README.md (89%) create mode 100644 deploy/staging/docker-compose.yml diff --git a/.github/workflows/docker-deploy-staging.yml b/.github/workflows/docker-deploy-staging.yml index cc710f8..047ede6 100644 --- a/.github/workflows/docker-deploy-staging.yml +++ b/.github/workflows/docker-deploy-staging.yml @@ -19,7 +19,7 @@ jobs: username: ${{ secrets.VPS_USER_STAGING }} key: ${{ secrets.VPS_SSH_KEY_STAGING }} port: ${{ secrets.VPS_PORT_STAGING }} - source: ./deploy + source: ./deploy/staging target: ${{ secrets.VPS_WORK_DIR_STAGING }} - name: Execute deployment via SSH @@ -30,7 +30,7 @@ jobs: key: ${{ secrets.VPS_SSH_KEY_STAGING }} port: ${{ secrets.VPS_PORT_STAGING }} script: | - cd ${{ secrets.VPS_WORK_DIR_STAGING }}/deploy + cd ${{ secrets.VPS_WORK_DIR_STAGING }}/deploy/staging export $(grep -v '^#' ~/.env.local | xargs) docker compose pull ${{ github.event.client_payload.app }} docker compose up -d ${{ github.event.client_payload.app }} diff --git a/.github/workflows/docker-deploy.yml b/.github/workflows/docker-deploy.yml index 070bf85..15caed8 100644 --- a/.github/workflows/docker-deploy.yml +++ b/.github/workflows/docker-deploy.yml @@ -19,7 +19,7 @@ jobs: username: ${{ secrets.VPS_USER }} key: ${{ secrets.VPS_SSH_KEY }} port: ${{ secrets.VPS_PORT }} - source: ./deploy + source: ./deploy/production target: ${{ secrets.VPS_WORK_DIR }} - name: Execute deployment via SSH @@ -30,7 +30,7 @@ jobs: key: ${{ secrets.VPS_SSH_KEY }} port: ${{ secrets.VPS_PORT }} script: | - cd ${{ secrets.VPS_WORK_DIR }}/deploy + cd ${{ secrets.VPS_WORK_DIR }}/deploy/production export $(grep -v '^#' ~/.env.local | xargs) docker compose pull ${{ github.event.client_payload.app }} docker compose up -d ${{ github.event.client_payload.app }} diff --git a/deploy/.env.local.example b/deploy/production/.env.local.example similarity index 100% rename from deploy/.env.local.example rename to deploy/production/.env.local.example diff --git a/deploy/production/README.md b/deploy/production/README.md new file mode 100644 index 0000000..c93358f --- /dev/null +++ b/deploy/production/README.md @@ -0,0 +1,22 @@ +# next.js-practical-cases - deploy production + +## Deploy by hand + +### Cheat sheet + +```bash +# Load env vars +export $(grep -v '^#' ~/.env.local | xargs) +# Pull the latest image +docker compose pull +# Restart container +docker compose up -d +# Clear unused images +docker image prune -f +``` + +To kill all containers in current machine. + +```bash +docker kill $(docker ps -q) && docker rm $(docker ps -a -q) +``` diff --git a/deploy/docker-compose.yml b/deploy/production/docker-compose.yml similarity index 85% rename from deploy/docker-compose.yml rename to deploy/production/docker-compose.yml index f37d550..5423225 100644 --- a/deploy/docker-compose.yml +++ b/deploy/production/docker-compose.yml @@ -1,4 +1,4 @@ -name: nextjs-prictical-cases +version: "3.8" services: template: @@ -10,7 +10,7 @@ services: - CLERK_SECRET_KEY - NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY ports: - - 80:3000 + - 3000:3000 depends_on: postgres: condition: service_healthy @@ -74,3 +74,21 @@ services: retries: 5 volumes: - ./volumes/pgdata:/var/lib/postgresql/data + + nginx: + container_name: nginx + image: nginx:latest + ports: + - "80:80" + - "443:443" + volumes: + - ./volumes/nginx.conf:/etc/nginx/nginx.conf:ro + depends_on: + - template + - notes + - stackoverflow + - large-file-upload + +networks: + default: + driver: bridge diff --git a/deploy/staging/.env.local.example b/deploy/staging/.env.local.example new file mode 100644 index 0000000..ae2c6bf --- /dev/null +++ b/deploy/staging/.env.local.example @@ -0,0 +1,6 @@ +DOCKER_REGISTRY= +DOCKER_USER= +DOCKER_PASSWORD= +POSTGRES_PASSWORD= +NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY= +CLERK_SECRET_KEY= diff --git a/deploy/README.md b/deploy/staging/README.md similarity index 89% rename from deploy/README.md rename to deploy/staging/README.md index baff725..99a85ac 100644 --- a/deploy/README.md +++ b/deploy/staging/README.md @@ -1,4 +1,4 @@ -# next.js-practical-cases - deploy +# next.js-practical-cases - deploy staging ## Deploy by hand diff --git a/deploy/staging/docker-compose.yml b/deploy/staging/docker-compose.yml new file mode 100644 index 0000000..5423225 --- /dev/null +++ b/deploy/staging/docker-compose.yml @@ -0,0 +1,94 @@ +version: "3.8" + +services: + template: + container_name: template + image: $DOCKER_REGISTRY/$DOCKER_USER/npcs-template:$DOCKER_TAG + restart: always + environment: + - DATABASE_URL=postgresql://postgres:${POSTGRES_PASSWORD}@postgres:5432/template?pool_timeout=6000 + - CLERK_SECRET_KEY + - NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY + ports: + - 3000:3000 + depends_on: + postgres: + condition: service_healthy + + notes: + container_name: notes + image: $DOCKER_REGISTRY/$DOCKER_USER/npcs-notes:$DOCKER_TAG + restart: always + environment: + - DATABASE_URL=postgresql://postgres:${POSTGRES_PASSWORD}@postgres:5432/notes?pool_timeout=6000 + - CLERK_SECRET_KEY + - NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY + ports: + - 3001:3000 + depends_on: + postgres: + condition: service_healthy + + stackoverflow: + container_name: stackoverflow + image: $DOCKER_REGISTRY/$DOCKER_USER/npcs-stackoverflow:$DOCKER_TAG + restart: always + environment: + - DATABASE_URL=postgresql://postgres:${POSTGRES_PASSWORD}@postgres:5432/stackoverflow?pool_timeout=6000 + - CLERK_SECRET_KEY + - NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY + ports: + - 3002:3000 + depends_on: + postgres: + condition: service_healthy + + large-file-upload: + container_name: large-file-upload + image: $DOCKER_REGISTRY/$DOCKER_USER/npcs-large-file-upload:$DOCKER_TAG + restart: always + environment: + - DATABASE_URL=postgresql://postgres:${POSTGRES_PASSWORD}@postgres:5432/large-file-upload?pool_timeout=6000 + - CLERK_SECRET_KEY + - NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY + ports: + - 3003:3000 + - 9999:9999 + depends_on: + postgres: + condition: service_healthy + + postgres: + container_name: postgres + image: postgres:16 + restart: always + shm_size: 128mb + environment: + - POSTGRES_PASSWORD + ports: + - 5432:5432 + healthcheck: + test: [ "CMD-SHELL", "pg_isready -U postgres" ] + interval: 5s + timeout: 5s + retries: 5 + volumes: + - ./volumes/pgdata:/var/lib/postgresql/data + + nginx: + container_name: nginx + image: nginx:latest + ports: + - "80:80" + - "443:443" + volumes: + - ./volumes/nginx.conf:/etc/nginx/nginx.conf:ro + depends_on: + - template + - notes + - stackoverflow + - large-file-upload + +networks: + default: + driver: bridge