From 91fae7e5698c2033dbf28c12b7d3e3a34d9b697e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iv=C3=A1n=20Ovejero?= Date: Sun, 24 Nov 2024 17:38:14 +0100 Subject: [PATCH] Set up `03-deploy` --- deploy/scripts/start-services.sh | 14 ----- docs/03-deploy.md | 62 +++++++------------ .../02-configure/04-app-dir-setup.yml | 20 ++++-- .../02-configure/05-backup-setup.yml | 15 ----- .../03-deploy}/config-loki.yml | 0 .../03-deploy}/config-prometheus.yml | 0 .../03-deploy}/config-promtail.yml | 0 .../03-deploy}/docker-compose.monitoring.yml | 1 + .../03-deploy}/docker-compose.yml | 2 +- 9 files changed, 39 insertions(+), 75 deletions(-) delete mode 100644 deploy/scripts/start-services.sh rename {deploy => infrastructure/03-deploy}/config-loki.yml (100%) rename {deploy => infrastructure/03-deploy}/config-prometheus.yml (100%) rename {deploy => infrastructure/03-deploy}/config-promtail.yml (100%) rename {deploy => infrastructure/03-deploy}/docker-compose.monitoring.yml (98%) rename {deploy => infrastructure/03-deploy}/docker-compose.yml (97%) diff --git a/deploy/scripts/start-services.sh b/deploy/scripts/start-services.sh deleted file mode 100644 index 814c3f5..0000000 --- a/deploy/scripts/start-services.sh +++ /dev/null @@ -1,14 +0,0 @@ -#!/bin/bash - -# Script to start server and monitoring stack: deploy/scripts/start-server.sh - -GITHUB_USER=$(grep GITHUB_USER $HOME/deploy/.config | cut -d'=' -f2 | tr -d '"*') -GITHUB_REPO=$(grep GITHUB_REPO $HOME/deploy/.config | cut -d'=' -f2 | tr -d '"*') - -RAW_REPO_ROOT="https://raw.githubusercontent.com/$GITHUB_USER/$GITHUB_REPO" - -curl -o deploy/docker-compose.monitoring.yml $RAW_REPO_ROOT/master/deploy/docker-compose.monitoring.yml -curl -o deploy/docker-compose.yml $RAW_REPO_ROOT/master/deploy/docker-compose.yml - -COMPOSE_PROJECT_NAME=n8n_shortlink docker-compose --file deploy/docker-compose.monitoring.yml up -d -COMPOSE_PROJECT_NAME=n8n_shortlink docker-compose --file deploy/docker-compose.yml --profile production up -d \ No newline at end of file diff --git a/docs/03-deploy.md b/docs/03-deploy.md index 2fd7a82..0206a6a 100644 --- a/docs/03-deploy.md +++ b/docs/03-deploy.md @@ -1,39 +1,25 @@ # Deploy -## Old instructions +## Initial deploy -- **Sentry**: Create a project and note down the **DSN**. - -Run server start script: +> When the docker containers are not running yet, copy the compose files and start the containers. ```sh -export GITHUB_USER= -export GITHUB_REPO= -export GITHUB_TOKEN= -export SENTRY_DSN= - -echo "GITHUB_USER=$GITHUB_USER" >> deploy/.config -echo "GITHUB_REPO=$GITHUB_REPO" >> deploy/.config -mkdir -p ~/.docker -echo '{ - "auths": { - "ghcr.io": { - "auth": "'$(echo -n "$GITHUB_USER:$GITHUB_TOKEN" | base64)'" - } - } -}' > ~/.docker/config.json -echo "N8N_SHORTLINK_SENTRY_DSN=$SENTRY_DSN" >> deploy/.env.production - -docker network create n8n-shortlink-network -deploy/scripts/start-services.sh +cd infrastructure/03-deploy +scp -r * n8n-shortlink-infra:~/.n8n-shortlink/deploy/ + +ssh n8n-shortlink-infra +docker-compose --file ~/.n8n-shortlink/deploy/docker-compose.monitoring.yml up --detach +docker-compose --file ~/.n8n-shortlink/deploy/docker-compose.yml --profile production up --detach ``` -Log in to `https://grafana.domain.com` and set a password for the Grafana admin user. +Log in to `https://grafana.domain.com` with `admin/admin credentials` and set a new secure password for the Grafana admin user. +## Deploy on release -## Old: Release +> When the docker containers are already running, release a new version of the image. -Ensure local and remote `master` branches are in sync: +1. Ensure local and remote `master` branches are in sync: ```sh git fetch origin @@ -41,27 +27,23 @@ git status # -> Your branch is up to date with 'origin/master'. ``` -Create tag (following semver) and push it: +2. Create a tag (following semver) and push it to remote: ```sh -git tag v1.0.0 -git push origin v1.0.0 +git tag v1.2.3 +git push origin v1.2.3 ``` -Monitor the release on GitHub: - -- https://github.com/ivov/n8n-shortlink/actions/workflows/release-on-tag-push.yml +3. This triggers the [`release` workflow](https://github.com/ivov/n8n-shortlink/actions/workflows/release.yml). On completion, the new image will be [listed](https://github.com/ivov/n8n-shortlink/pkgs/container/n8n-shortlink) on GHCR. Watchtower will discover for the image and deploy it to production. (Watchtower polls every six hours.) -On completion, this release is listed as `latest` on GHCR: +To prompt Watchtower to check immediately: -- https://github.com/ivov/n8n-shortlink/pkgs/container/n8n-shortlink +```sh +ssh n8n-shortlink-infra "docker kill --signal=SIGHUP watchtower" +``` -Deploy the release to production: +Then wait for Watchtower to pull the new image and start the container: ```sh -ssh shortlink_vps - -COMPOSE_PROJECT_NAME=n8n_shortlink docker-compose --file deploy/docker-compose.monitoring.yml down -COMPOSE_PROJECT_NAME=n8n_shortlink docker-compose --file deploy/docker-compose.yml down -deploy/scripts/start-services.sh +ssh n8n-shortlink-infra "docker ps | grep n8n-shortlink" ``` diff --git a/infrastructure/02-configure/04-app-dir-setup.yml b/infrastructure/02-configure/04-app-dir-setup.yml index 5362c2b..0382650 100644 --- a/infrastructure/02-configure/04-app-dir-setup.yml +++ b/infrastructure/02-configure/04-app-dir-setup.yml @@ -4,25 +4,35 @@ vars: app_dir: "{{ ansible_user_dir }}/.n8n-shortlink" app_db: "{{ app_dir }}/n8n-shortlink.sqlite" + backup_dir: "{{ app_dir }}/backup" + deploy_dir: "{{ app_dir }}/deploy" + vars_prompt: - name: sentry_dsn prompt: Enter Sentry DSN private: no tasks: - - name: Create app dir + - name: Create required directories file: - path: "{{ app_dir }}" + path: "{{ item }}" state: directory mode: "0700" + owner: "{{ ansible_user }}" + group: "{{ ansible_user }}" + loop: + - "{{ app_dir }}" + - "{{ deploy_dir }}" + - "{{ backup_dir }}" + - "{{ ansible_user_dir }}/.aws" - name: Create empty SQLite database shell: "sqlite3 {{ app_db }} ''" args: creates: "{{ app_db }}" # run `shell` _if and only if_ this file does not yet exist - - name: Create .env.production file with Sentry DSN + - name: Create `.env.production` file copy: - dest: "{{ app_dir }}/.env.production" + dest: "{{ deploy_dir }}/.env.production" content: "N8N_SHORTLINK_SENTRY_DSN={{ sentry_dsn }}" - mode: "0600" \ No newline at end of file + mode: "0600" diff --git a/infrastructure/02-configure/05-backup-setup.yml b/infrastructure/02-configure/05-backup-setup.yml index 7d53668..6be0281 100644 --- a/infrastructure/02-configure/05-backup-setup.yml +++ b/infrastructure/02-configure/05-backup-setup.yml @@ -26,21 +26,6 @@ state: present update_cache: yes - # ================================== - # backup and aws dirs - # ================================== - - - name: Create dirs - file: - path: "{{ item }}" - state: directory - mode: "0700" - owner: "{{ ansible_user }}" - group: "{{ ansible_user }}" - loop: - - "{{ backup_dir }}" - - "{{ ansible_user_dir }}/.aws" - # ================================== # encryption key # ================================== diff --git a/deploy/config-loki.yml b/infrastructure/03-deploy/config-loki.yml similarity index 100% rename from deploy/config-loki.yml rename to infrastructure/03-deploy/config-loki.yml diff --git a/deploy/config-prometheus.yml b/infrastructure/03-deploy/config-prometheus.yml similarity index 100% rename from deploy/config-prometheus.yml rename to infrastructure/03-deploy/config-prometheus.yml diff --git a/deploy/config-promtail.yml b/infrastructure/03-deploy/config-promtail.yml similarity index 100% rename from deploy/config-promtail.yml rename to infrastructure/03-deploy/config-promtail.yml diff --git a/deploy/docker-compose.monitoring.yml b/infrastructure/03-deploy/docker-compose.monitoring.yml similarity index 98% rename from deploy/docker-compose.monitoring.yml rename to infrastructure/03-deploy/docker-compose.monitoring.yml index 1764635..ccc0508 100644 --- a/deploy/docker-compose.monitoring.yml +++ b/infrastructure/03-deploy/docker-compose.monitoring.yml @@ -1,3 +1,4 @@ +name: n8n_shortlink_monitoring_compose services: grafana: image: grafana/grafana-oss:latest diff --git a/deploy/docker-compose.yml b/infrastructure/03-deploy/docker-compose.yml similarity index 97% rename from deploy/docker-compose.yml rename to infrastructure/03-deploy/docker-compose.yml index 88f854c..fced754 100644 --- a/deploy/docker-compose.yml +++ b/infrastructure/03-deploy/docker-compose.yml @@ -1,4 +1,4 @@ -name: n8n-shortlink +name: n8n_shortlink_compose services: n8n-shortlink: image: ghcr.io/ivov/n8n-shortlink:latest