Skip to content

Instantly share code, notes, and snippets.

@iambibhas
Last active December 5, 2018 18:04
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save iambibhas/5576601 to your computer and use it in GitHub Desktop.
Save iambibhas/5576601 to your computer and use it in GitHub Desktop.
Deploy only changed files from last deployed commit using Fabric
def upload_tar():
# First, get the last deployed commit's short hash. 1st time, just create the file manually.
last_deployed = local('cat last_deployed', capture=True)
# get the last commit's short hash
current_commit = local('git rev-parse --short HEAD', capture=True)
# get the list of changed files since last deployed commit, put them in a tgz file, named as the commit to be deployed.
local('git archive --format tar HEAD $(git diff --name-only HEAD %s) | gzip -9 > %s.tgz' % (last_deployed, current_commit))
# put the .tgz file to server, in project's archive/ directory
put('%s.tgz' % current_commit, '%s/archive/' % env.path)
# move the local file inside the archive/ directory, so the root is clean
local('mv %s.tgz archive/' % current_commit)
# go inside the project directory on server
with cd(env.path):
# extract the files, they'll be overwritten
run('tar -zxvf archive/%s.tgz' % current_commit)
# finally, save the deployed commit's short hash in last_deployed file
local('echo "%s" > last_deployed' % current_commit)
@SKalt
Copy link

SKalt commented Mar 7, 2018

at line 3, you might use a || to catch cat failing: cat last_deployed || echo HEAD^ > last_deployed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment