Last active
June 7, 2019 17:14
-
-
Save brockthebear/cebde788a51a2ae6a372eb382fdde6d3 to your computer and use it in GitHub Desktop.
Example deployment of a Node app using Fabric
This file contains hidden or 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
#!/usr/local/bin/python3 | |
# List tasks with fab2 -l | |
import os | |
import os.path as path | |
from fabric2 import Config, Connection, task | |
import patchwork.transfers as transfer | |
# Define values in .env file. | |
HOST = os.environ['HOST'] # The server you want to deploy to. | |
USER = os.environ['USER'] # The server user (/home/{USER}/) | |
SOURCE_DIR = f"{path.abspath(path.dirname(__file__))}/" # Source folder. | |
TARGET_DIR = f"home/{USER}/{os.environ['TARGET_DIR']}/" # Destination folder. | |
APP_DIR = f"{os.environ['APP_DIR']}" # The directory of the Node app | |
# scp file onto remote server | |
# run with `fab transfer_files` | |
@task | |
def transfer_files(ctx): | |
c = Connection(host=HOST, user=USER) | |
transfer.rsync(c, SOURCE_DIR, TARGET_DIR) | |
c.close() | |
# install dependencies and run project. | |
# run with `fab deploy` | |
@task | |
def deploy(ctx): | |
conf = Config() | |
conf.load_ssh_config() | |
with Connection(config=conf, host=HOST, user=USER) as c: | |
c.run(f'cd {TARGET_DIR}/{APP_DIR}/ && npm install && npm run start') | |
c.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment