-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #190 from tkkuehn/deploy
Set up non-heroku deploy workflow
- Loading branch information
Showing
9 changed files
with
1,229 additions
and
778 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -61,10 +61,3 @@ jobs: | |
publish: true | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
|
||
- name: Deploy heroku app | ||
uses: akhileshns/[email protected] | ||
with: | ||
heroku_api_key: ${{ secrets.HEROKU_API_KEY }} | ||
heroku_app_name: afids-validator | ||
heroku_email: ${{ secrets.HEROKU_EMAIL }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
name: AFIDs Validator Deploy | ||
|
||
on: | ||
workflow_dispatch: | ||
inputs: | ||
comments: | ||
description: "Comments" | ||
|
||
jobs: | ||
deploy: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Print author | ||
run: | | ||
echo "Author: ${{ github.event.inputs.author }}" | ||
echo "Date: ${{ github.event.inputs.date }}" | ||
echo "Comments: ${{ github.event.inputs.comments }}" | ||
- uses: actions/checkout@master | ||
with: | ||
ref: refs/heads/master | ||
|
||
- name: Install poetry | ||
uses: snok/install-poetry@v1 | ||
with: | ||
version: 1.3.2 | ||
virtualenvs-create: true | ||
virtualenvs-in-project: true | ||
|
||
- name: Install project | ||
run: | | ||
poetry install | ||
- name: Build package with poetry | ||
run: | | ||
poetry build | ||
- name: Write .env file | ||
env: | ||
DATABASE_URL: ${{ secrets.PRODUCTION_DATABASE_URL }} | ||
FLASK_ENV: ${{ secrets.PRODUCTION_FLASK_ENV }} | ||
ORCID_OAUTH_CLIENT_ID: ${{ secrets.PRODUCTION_ORCID_OAUTH_CLIENT_ID }} | ||
ORCID_OAUTH_CLIENT_SECRET: ${{ secrets.PRODUCTION_ORCID_OAUTH_CLIENT_SECRET }} | ||
SECRET_KEY: ${{ secrets.PRODUCTION_SECRET_KEY }} | ||
run: | | ||
echo DATABASE_URL="$DATABASE_URL" >> .env | ||
echo FLASK_ENV="$FLASK_ENV" >> .env | ||
echo ORCID_OAUTH_CLIENT_ID="$ORCID_OAUTH_CLIENT_ID" >> .env | ||
echo ORCID_OAUTH_CLIENT_SECRET="$ORCID_OAUTH_CLIENT_SECRET" >> .env | ||
echo SECRET_KEY="$SECRET_KEY" >> .env | ||
- name: Build release | ||
run: | | ||
DATE=`date +%s` | ||
mkdir -p releases/afidsvalidator-"$DATE" | ||
cp .env releases/afidsvalidator-"$DATE" | ||
WHEEL=`ls dist | grep whl` | ||
cp dist/"$WHEEL" releases/afidsvalidator-"$DATE" | ||
echo WHEEL="$WHEEL" >> $GITHUB_ENV | ||
echo DATE="$DATE" >> $GITHUB_ENV | ||
- name: Deploy release | ||
env: | ||
PRODUCTION_URL: ${{ secrets.PRODUCTION_URL }} | ||
PRIVATE_KEY: ${{ secrets.PRIVATE_KEY }} | ||
run: | | ||
echo "$PRIVATE_KEY" > .private_key | ||
poetry run python3 fabrictasks.py "$PRODUCTION_URL" releases/afidsvalidator-"$DATE" /opt/afidsvalidator/releases "$WHEEL" /opt/afidsvalidator/venv-afidsvalidator .private_key |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
[uwsgi] | ||
module = afidsvalidator.wsgi:app | ||
|
||
master = true | ||
processes = 5 | ||
|
||
socket = afidsvalidator.sock | ||
chmod-socket = 660 | ||
vacuum = true | ||
|
||
die-on-term = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
"""Entrypoint for flask""" | ||
|
||
from afidsvalidator import create_app | ||
|
||
app = create_app() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import argparse | ||
from pathlib import Path, PurePath | ||
|
||
from fabric import Connection, config | ||
|
||
|
||
def push_release( | ||
connection: Connection, | ||
new_release_path: Path, | ||
remote_releases_dir: PurePath, | ||
wheel_name: str, | ||
): | ||
remote_release_new = str(remote_releases_dir / new_release_path.name) | ||
connection.run(f"mkdir -p {remote_release_new}") | ||
wheel_path = str(new_release_path / wheel_name) | ||
connection.put(wheel_path, remote_release_new) | ||
connection.put(str(new_release_path / ".env"), remote_release_new) | ||
link_name = str(remote_releases_dir.parent / "current") | ||
connection.run(f"ln -s {remote_release_new} {link_name}") | ||
|
||
|
||
def install_wheel(connection: Connection, venv_path: str, wheel_path: str): | ||
connection.run( | ||
f"source {venv_path}/bin/activate && pip install {wheel_path}" | ||
) | ||
|
||
|
||
def upgrade_db( | ||
connection: Connection, new_release_remote: str, venv_path: str | ||
): | ||
connection.run( | ||
f"set -a && . {new_release_remote}/.env && set +a && " | ||
f"{venv_path}/bin/flask -A afidsvalidator.wsgi:app db upgrade " | ||
f"-d {venv_path}/lib/python3.8/site-packages/migrations" | ||
) | ||
|
||
|
||
def restart_validator(connection: Connection): | ||
connection.sudo("systemctl restart afidsvalidator.service") | ||
|
||
|
||
def deploy( | ||
connection: Connection, | ||
new_release_path: Path, | ||
remote_releases_dir: PurePath, | ||
wheel_name: str, | ||
venv_path: PurePath, | ||
): | ||
push_release(connection, new_release_path, remote_releases_dir, wheel_name) | ||
wheel_path = str(remote_releases_dir / new_release_path.name / wheel_name) | ||
install_wheel(connection, str(venv_path), wheel_path) | ||
upgrade_db( | ||
connection, | ||
str(remote_releases_dir / new_release_path.name), | ||
str(venv_path), | ||
) | ||
restart_validator(connection) | ||
|
||
|
||
def build_parser(): | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument("host") | ||
parser.add_argument("new_release_path", type=Path) | ||
parser.add_argument("remote_releases_dir", type=PurePath) | ||
parser.add_argument("wheel_name", type=str) | ||
parser.add_argument("venv_path", type=PurePath) | ||
parser.add_argument("identity_path", type=Path) | ||
return parser | ||
|
||
|
||
def main(): | ||
parser = build_parser() | ||
args = parser.parse_args() | ||
deploy( | ||
Connection( | ||
args.host, connect_kwargs={"key_filename": str(args.identity_path)} | ||
), | ||
args.new_release_path, | ||
args.remote_releases_dir, | ||
args.wheel_name, | ||
args.venv_path, | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
config.Config() | ||
main() |
Oops, something went wrong.