From 87e2f2292c4c4aa0e4d516c0134e593dee399ab8 Mon Sep 17 00:00:00 2001 From: Nikolai R Kristiansen Date: Sat, 5 Feb 2022 21:10:14 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=91=B7=20Deploy=20from=20CI=20using=20SSH?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitlab-ci.yml | 4 +++- bin/deploy | 14 ++++++++++++++ fabfile.py | 49 ------------------------------------------------- 3 files changed, 17 insertions(+), 50 deletions(-) create mode 100755 bin/deploy delete mode 100644 fabfile.py diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 44f880a..f0ce71e 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -30,4 +30,6 @@ lint: deploy: stage: deploy script: - - echo "TODO run the fabric script" \ No newline at end of file + - ssh gitdeploy@dreamcast.neuf.no "$(< bin/deploy)" + only: + - main diff --git a/bin/deploy b/bin/deploy new file mode 100755 index 0000000..62954c8 --- /dev/null +++ b/bin/deploy @@ -0,0 +1,14 @@ +#!/bin/bash +set -eu + +PROJECT_PATH=/var/www/neuf.no/bbs +PROCESS_NAME="$PROJECT.neuf.no" + +cd "$PROJECT_PATH" || exit 1 +git pull +poetry install +umask 022; poetry run python manage.py collectstatic --noinput # Collect static +poetry run python manage.py migrate # Run DB migrations + +# Reload +sudo /usr/bin/supervisorctl pid "$PROCESS_NAME" | xargs kill -HUP diff --git a/fabfile.py b/fabfile.py deleted file mode 100644 index 441a4f0..0000000 --- a/fabfile.py +++ /dev/null @@ -1,49 +0,0 @@ -import os -from contextlib import contextmanager as _contextmanager -from fabric.api import run, sudo, env, cd, prefix - -env.use_ssh_config = True -env.hosts = ['dreamcast.neuf.no'] -env.user = 'gitdeploy' - - -BASE_PATH = '/var/www/neuf.no/' - -PROJECTS = { - 'bbs': { - 'path': os.path.join(BASE_PATH, 'bbs'), - 'process_name': 'bbs.neuf.no' - }, - 'fantastene': { - 'path': os.path.join(BASE_PATH, 'bbs_fantastene'), - 'process_name': 'fantastene-bbs.neuf.no' - }, - 'arrut': { - 'path': os.path.join(BASE_PATH, 'bbs_arrut'), - 'process_name': 'arrut-bbs.neuf.no' - } -} - - -@_contextmanager -def virtualenv(project): - activate = 'source {}/venv/bin/activate'.format(project['path']) - with cd(project['path']), prefix(activate): - yield - - -def deploy(project_name='bbs'): - if project_name not in PROJECTS: - print("'{}' not in valid projects({})".format(project_name, ', '.join(sorted(PROJECTS.keys())))) - exit(1) - - project = PROJECTS[project_name] - with virtualenv(project): - run('git pull') # Get source - run('pip install -r requirements.txt') # install deps in virtualenv - run('umask 022; python manage.py collectstatic --noinput') # Collect static - run('python manage.py migrate') # Run DB migrations - - # Reload - sudo('/usr/bin/supervisorctl pid {process_name} | xargs kill -HUP'.format(process_name=project['process_name']), - shell=False)