Skip to content

Instantly share code, notes, and snippets.

@brockthebear
Last active June 7, 2019 17:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brockthebear/cebde788a51a2ae6a372eb382fdde6d3 to your computer and use it in GitHub Desktop.
Save brockthebear/cebde788a51a2ae6a372eb382fdde6d3 to your computer and use it in GitHub Desktop.
Example deployment of a Node app using Fabric
#!/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