Skip to content

Instantly share code, notes, and snippets.

@rubenmromero
Last active May 5, 2019 23:52
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 rubenmromero/2caf7534af080ffc8e95fd15d5c93b65 to your computer and use it in GitHub Desktop.
Save rubenmromero/2caf7534af080ffc8e95fd15d5c93b65 to your computer and use it in GitHub Desktop.
Python :: Web service to deploy a repository on a server through a webhook
#!/usr/bin/env python
#
# Modules Import
#
import os, sys, shlex, subprocess
#
# Variables Definition
#
workspace = '<repository_root_folder>'
#
# Main
#
# For execution through HTTP request
print "Content-Type: text/html\n"
# Change position to the repository root folder
os.chdir(workspace)
print "Verify that there are not local changes in '" + workspace + "' path of the target server:"
command = shlex.split('git diff --exit-code')
command_exec = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output = command_exec.communicate()
if command_exec.returncode != 0:
print "It is not possible to deploy in '" + workspace + "' path due to there are local changes on the target server"
exit(1)
print "OK"
print "\nPull the existent new commits from origin on the target server:"
command = shlex.split('git pull')
output, error = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
if output != '':
print output
if error != '':
print error
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment