Skip to content

Instantly share code, notes, and snippets.

@cgarnier
Forked from edmondburnett/gist:40e7db34416fdc734846
Last active June 19, 2016 17:47
Show Gist options
  • Select an option

  • Save cgarnier/a97289d352a7c3c46fd8d03a8dfdf022 to your computer and use it in GitHub Desktop.

Select an option

Save cgarnier/a97289d352a7c3c46fd8d03a8dfdf022 to your computer and use it in GitHub Desktop.

Revisions

  1. cgarnier renamed this gist Jun 19, 2016. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. cgarnier revised this gist Jun 19, 2016. 1 changed file with 44 additions and 23 deletions.
    67 changes: 44 additions & 23 deletions gistfile1.py
    Original file line number Diff line number Diff line change
    @@ -1,30 +1,51 @@
    #!/usr/bin/env python
    # post-receive hook for git-based deployments
    # https://edmondburnett.com/post/python-push-to-deploy
    #!/usr/bin/env python
    # post-receive hook for git-based deployments

    import sys
    import os
    from subprocess import call
    import sys
    import os
    from subprocess import call

    # configuration
    deploy_to_path = '/path/to/deploy/directory/'
    deploy_branch = 'master'
    # configuration
    deploy_to_path = os.path.realpath('./deploy_dir')
    deploy_branch = 'master'

    def post_receive(from_commit, to_commit, branch_name):
    # Don't deploy if pushed branch != deploy_branch
    if not branch_name.endswith(deploy_branch):
    print('Received branch ' + branch_name + ', not deploying.')
    sys.exit()
    def init():
    if not os.path.exists(deploy_to_path):
    os.makedirs(deploy_to_path)

    # copy files to deploy directory
    call('GIT_WORK_TREE="' + deploy_to_path + '" git checkout -f ' + branch_name, shell=True)
    print('DEPLOY: ' + branch_name + '(' + to_commit + ') copied to ' + deploy_to_path)
    def post_receive(from_commit, to_commit, branch_name):
    # Don't deploy if pushed branch != deploy_branch
    if not branch_name.endswith(deploy_branch):
    print('Received branch ' + branch_name + ', not deploying.')
    sys.exit()

    # TODO: Deployment Tasks
    # i.e. Run a script, restart daemons, etc
    # copy files to deploy directory
    call('GIT_WORK_TREE="' + deploy_to_path + '" git checkout -f ' + branch_name, shell=True)
    print('DEPLOY: ' + branch_name + '(' + to_commit + ') copied to ' + deploy_to_path)

    # TODO: Deployment Tasks
    # i.e. Run a script, restart daemons, etc

    if __name__ == '__main__':
    # get values from STDIN
    fc,tc,bn = sys.stdin.read().split()
    post_receive(fc, tc, bn)
    # npm run stop
    print('running: npm run stop')
    call('/bin/bash -i -c "npm run stop"', shell=True, cwd=deploy_to_path, executable='/bin/bash')

    # Npm install
    print('running: npm install')
    call('/bin/bash -i -c "npm install"', shell=True, cwd=deploy_to_path, executable='/bin/bash')

    # Npm run build
    print('running: npm run build')
    call('/bin/bash -i -c "npm run build"', shell=True, cwd=deploy_to_path, executable='/bin/bash')


    # Npm start
    print('running: npm run start')
    call('/bin/bash -i -c "npm run start"', shell=True, cwd=deploy_to_path, executable='/bin/bash')


    if __name__ == '__main__':
    # get values from STDIN
    fc,tc,bn = sys.stdin.read().split()
    init()
    post_receive(fc, tc, bn)
  3. @edmondburnett edmondburnett revised this gist Jul 28, 2014. No changes.
  4. @edmondburnett edmondburnett created this gist Jul 28, 2014.
    30 changes: 30 additions & 0 deletions gistfile1.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    #!/usr/bin/env python
    # post-receive hook for git-based deployments
    # https://edmondburnett.com/post/python-push-to-deploy

    import sys
    import os
    from subprocess import call

    # configuration
    deploy_to_path = '/path/to/deploy/directory/'
    deploy_branch = 'master'

    def post_receive(from_commit, to_commit, branch_name):
    # Don't deploy if pushed branch != deploy_branch
    if not branch_name.endswith(deploy_branch):
    print('Received branch ' + branch_name + ', not deploying.')
    sys.exit()

    # copy files to deploy directory
    call('GIT_WORK_TREE="' + deploy_to_path + '" git checkout -f ' + branch_name, shell=True)
    print('DEPLOY: ' + branch_name + '(' + to_commit + ') copied to ' + deploy_to_path)

    # TODO: Deployment Tasks
    # i.e. Run a script, restart daemons, etc


    if __name__ == '__main__':
    # get values from STDIN
    fc,tc,bn = sys.stdin.read().split()
    post_receive(fc, tc, bn)