From 33318012fc26213ce37ea812fbae9ea38c494b2c Mon Sep 17 00:00:00 2001 From: sergeyyarkov Date: Tue, 23 May 2023 17:28:45 +0300 Subject: [PATCH] deploy script --- compose.dev.yml | 13 ++++++------ compose.yml | 46 +++++++++++++++--------------------------- scripts/deploy-web.sh | 38 ++++++++++++++++++++++++++++++++++ scripts/hello.sh | 6 +++++- scripts/rebuild-web.sh | 0 website/Dockerfile | 13 ++++++------ 6 files changed, 71 insertions(+), 45 deletions(-) create mode 100755 scripts/deploy-web.sh mode change 100644 => 100755 scripts/hello.sh delete mode 100644 scripts/rebuild-web.sh diff --git a/compose.dev.yml b/compose.dev.yml index 93da57f..540adc7 100644 --- a/compose.dev.yml +++ b/compose.dev.yml @@ -3,17 +3,16 @@ services: web: build: context: ./website - target: production network: host + target: production extra_hosts: - 'cms.localhost:127.0.0.1' - container_name: web - volumes: - - ./website:/app:rw - depends_on: - - directus + args: + - 'CACHE_DATE=$(date +%Y-%m-%d:%H:%M:%S)' networks: - directus + volumes: + - './website/nginx/conf.d:/etc/nginx/conf.d:rw' env_file: - ./website/.env @@ -22,7 +21,7 @@ services: container_name: nginx image: nginx:1.23.3-alpine volumes: - - './nginx/nginx.conf:/etc/nginx/nginx.conf' + - './nginx/nginx.conf:/etc/nginx/nginx.conf:rw' ports: - '80:80' - '433:433' diff --git a/compose.yml b/compose.yml index d88e269..4ede0a5 100644 --- a/compose.yml +++ b/compose.yml @@ -3,29 +3,27 @@ services: web: build: context: ./website + network: host target: production - container_name: web - volumes: - - './website/nginx/conf.d:/etc/nginx/conf.d:rw' + extra_hosts: + - 'cms.localhost:127.0.0.1' + args: + - 'CACHE_DATE=$(date +%Y-%m-%d:%H:%M:%S)' networks: - directus ports: - - '80' - depends_on: - - directus - - nginx - environment: - - DIRECTUS_URL=http://cms.localhost - - DIRECTUS_EMAIL=admin@example.com - - DIRECTUS_PASSWORD=d1r3ctu5 - - DIRECTUS_STATIC_TOKEN= + - '3000' + volumes: + - './website/nginx/conf.d:/etc/nginx/conf.d:rw' + env_file: + - ./website/.env nginx: - restart: 'always' + # restart: 'always' container_name: nginx image: nginx:1.23.3-alpine volumes: - - './nginx/nginx.conf:/etc/nginx/nginx.conf' + - './nginx/nginx.conf:/etc/nginx/nginx.conf:rw' ports: - '80:80' - '433:433' @@ -44,20 +42,14 @@ services: volumes: - db-data:/var/lib/postgresql/data ports: - - 5432 + - 5432:5432 networks: - directus - # cache: - # container_name: cache - # image: redis:6 - # networks: - # - directus - directus: - image: directus/directus:latest + image: directus/directus:10.0.0 ports: - - 8055 + - 8055:8055 volumes: - uploads:/directus/uploads depends_on: @@ -67,20 +59,13 @@ services: environment: KEY: '255d861b-5ea1-5996-9aa3-922530ec40b1' SECRET: '6116487b-cda1-52c2-b5b5-c8022c45e263' - MAX_PAYLOAD_SIZE: '100mb' - DB_CLIENT: 'pg' DB_HOST: 'database' DB_PORT: '5432' DB_DATABASE: 'directus' DB_USER: 'directus' DB_PASSWORD: 'directus' - - # CACHE_ENABLED: 'false' - # CACHE_STORE: 'redis' - # CACHE_REDIS: 'redis://cache:6379' - ADMIN_EMAIL: 'admin@example.com' ADMIN_PASSWORD: 'd1r3ctu5' volumes: @@ -88,3 +73,4 @@ volumes: uploads: networks: directus: + driver: bridge diff --git a/scripts/deploy-web.sh b/scripts/deploy-web.sh new file mode 100755 index 0000000..99d21ff --- /dev/null +++ b/scripts/deploy-web.sh @@ -0,0 +1,38 @@ +#!bin/bash + +reload_nginx() { + docker exec nginx nginx -s reload +} + +rebuild_web() { + docker compose build --build-arg CACHE_DATE=$(date +%Y-%m-%d:%H:%M:%S) --progress=plain web +} + +zero_downtime_deploy() { + service_name=web + old_container_id=$(docker ps -f name=$service_name -q | tail -n1) + + # bring a new container online, running new code + # (nginx continues routing to the old container only) + docker compose up -d --no-deps --scale $service_name=2 --no-recreate $service_name + + # wait for new container to be available + new_container_id=$(docker ps -f name=$service_name -q | head -n1) + new_container_ip=$(docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $new_container_id) + curl --silent -I --retry-connrefused --retry 30 --retry-delay 1 --fail http://$new_container_ip:80/ || exit 1 + + # start routing requests to the new container (as well as the old) + reload_nginx + + # take the old container offline + docker stop $old_container_id + docker rm $old_container_id + + docker compose up -d --no-deps --scale $service_name=1 --no-recreate $service_name + + # stop routing requests to the old container + reload_nginx +} + +rebuild_web +zero_downtime_deploy \ No newline at end of file diff --git a/scripts/hello.sh b/scripts/hello.sh old mode 100644 new mode 100755 index 6fbe8ef..72cfc9d --- a/scripts/hello.sh +++ b/scripts/hello.sh @@ -1,3 +1,7 @@ #!bin/bash -echo "Hello!" \ No newline at end of file +say_hello() { + echo "Hello!" +} + +say_hello \ No newline at end of file diff --git a/scripts/rebuild-web.sh b/scripts/rebuild-web.sh deleted file mode 100644 index e69de29..0000000 diff --git a/website/Dockerfile b/website/Dockerfile index d73612f..e61c814 100644 --- a/website/Dockerfile +++ b/website/Dockerfile @@ -3,31 +3,30 @@ ARG NODE_IMAGE=node:19-alpine3.16 ARG NGINX_IMAGE=nginx:1.23.3-alpine -# DEVELOPMENT STAGE FROM ${NODE_IMAGE} as development WORKDIR /app COPY ./package.json ./ COPY ./package-lock.json ./ -RUN npm ci +RUN npm ci && npm cache clean --force COPY . . EXPOSE 3000 CMD [ "npm", "start" ] -# BUILDER STAGE +ARG CACHE_DATE=not_a_date + FROM development as builder -ENV DIRECTUS_URL=http://cms.localhost WORKDIR /app +ENV DIRECTUS_URL=http://cms.localhost RUN npm run build -# PRODUCTION STAGE FROM ${NGINX_IMAGE} as production -COPY --from=builder /app/dist /usr/share/nginx/html COPY --from=builder /app/nginx/conf.d /etc/nginx/conf.d +COPY --from=builder /app/dist /usr/share/nginx/html EXPOSE 80 -CMD ["nginx", "-g", "daemon off;"] +CMD ["nginx", "-g", "daemon off;"] \ No newline at end of file