Last active
August 29, 2015 14:10
-
-
Save kvendrik/5b1044e12f69ad3a9ae2 to your computer and use it in GitHub Desktop.
Simple continuous deployment script
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
| ''' | |
| Name: git-to-public | |
| Description: Simple continuous deployment script. Listens for pushes to | |
| Gitlab repositories. When there is a push to the master | |
| branch it clones the repo to the same folder as the script | |
| is in. | |
| Author: Koen Vendrik <[email protected]> | |
| Date: 22/11/2014 | |
| License: MIT | |
| Usage: 1. Generate a SSH key on your server and add it to your Gitlab. | |
| 2. Setup a hook for localhost:9000 on the repos you want to | |
| copy to the script directory when there is a push to | |
| the master branch. | |
| 3. Start listening by starting the script: | |
| python _git-to-public.py | |
| 4. Done! Now when you push to the master branch in one of the | |
| repos the script will clone it to the same folder as the | |
| script is in. | |
| Note: If you want to use a copy of Gitlab that is on a different | |
| server (e.g. gitlab.com) then you should open up port <PORT> | |
| on your server so Gitlab can reach it. | |
| ''' | |
| from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer | |
| from os import path | |
| from shutil import rmtree | |
| from json import loads | |
| from subprocess import call | |
| HOST = '127.0.0.1' | |
| PORT = 9000 | |
| # branch to listen on | |
| # script clones if there is a push to this branch | |
| BRANCH = 'master' | |
| def rm_dir_if_exists(dir_name): | |
| if path.exists(dir_name) and path.isdir(dir_name): | |
| rmtree(dir_name) | |
| print '>> Removed ./'+dir_name+'/' | |
| def process_hook_data(data): | |
| # only act if push was to the <BRANCH> branch | |
| if data['ref'] == 'refs/heads/'+BRANCH: | |
| repo_details = data['repository'] | |
| # remove repo folder if it exists | |
| print '>> Checking if '+repo_details['name']+' already exists...' | |
| rm_dir_if_exists(repo_details['name']) | |
| # clone repo | |
| print '>> Cloning repo...' | |
| call('git clone -b '+BRANCH+' '+repo_details['url'], shell=True) | |
| print '>> Done.\n' | |
| class VerbHandler(BaseHTTPRequestHandler): | |
| def do_POST(self): | |
| self.send_response(200) | |
| length = int(self.headers['Content-Length']) | |
| # get post data, parse as json | |
| data = loads(self.rfile.read(length).decode('utf-8')) | |
| process_hook_data(data) | |
| if __name__ == '__main__': | |
| try: | |
| # start server and keep listening | |
| httpd = HTTPServer((HOST, PORT), VerbHandler) | |
| print '>> Listening on '+HOST+':'+str(PORT)+'\n' | |
| httpd.serve_forever() | |
| except KeyboardInterrupt: | |
| # stop server when ^C is pressed | |
| print '>> Stopping server...' | |
| httpd.server_close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment