Git push deployment
#
Find similar titles
- (rev. 8)
- Sardor
Structured data
- Category
- Programming
Recommended to use this for projects as a starting point.
On the server (example.com) #
-
Create user on
example.com
, as which we (the git client) connects (push) toexmaple.com
.sudo useradd -m -s /usr/bin/git-shell git
-
Add your ssh public key to the
authorized_keys
file of the created user:sudo -u git bash cd ~ mkdir -p .ssh vim .ssh/authorized_keys # Paste your public key and save
-
Create a
git bare
repo for your project:mkdir testapp cd testapp git init --bare
-
Copy the
post-receive
script from this gist to thehooks
dir of the created bare repo.vim testapp/hooks/post-receive #!/bin/bash DEPLOY_BRANCH="master" DEPLOY_ROOT="/var/www" UPDATE_CMD='cd "${DEPLOY_TO}" && git pull origin master RESTART_CMD='sudo systemctl restart apache chmod +x testapp/hooks/post-receive
-
Set ownership and permissions of the
DEPLOY_ROOT
directory:sudo chown root:git -R /var/www sudo chmod 775 /var/www
On the client #
-
Create a git repo and add our newly created
remote
:mkdir testapp cd testapp git init git remote add production git@example.com:~/testapp
-
Commit and push to production:
$ vim Makefile $ # Paste contents of Makefile from this gist (as an example) $ git add . $ git commit -am "test commit" $ git push production master Counting objects: 12, done. Delta compression using up to 4 threads. Compressing objects: 100% (4/4), done. Writing objects: 100% (4/4), 432 bytes | 0 bytes/s, done. Total 4 (delta 2), reused 0 (delta 0) remote: +++++++++++++++++++++++ Welcome to 'example.com' (1.2.3.4) ++++++++++++++++++++++++ remote: remote: githook: I will deploy 'master' branch of the 'testapp' project to '/var/www/testapp' remote: remote: githook: UPDATE (CMD: 'cd "${DEPLOY_TO}" && make "update"'): remote: Makefile: Doing UPDATE stuff like grunt, gulp, rake,... remote: git remote: /var/www/testapp remote: remote: githook: RESTART (CMD: 'sudo systemctl restart "${PROJECT_NAME}.service" && sudo systemctl status "${PROJECT_NAME}.service"'): remote: testapp.service - node.js testapp remote: Loaded: loaded (/etc/systemd/system/testapp.service; disabled) remote: Active: inactive (dead) since Fri 2014-03-21 22:10:23 UTC; 10ms ago remote: Process: 9265 ExecStart=/bin/bash -c sleep 3;echo "I am starting";echo "$(whoami)"; (code=exited, status=0/SUCCESS) remote: remote: Mar 21 22:10:20 image systemd[1]: Starting nodejs testapp... remote: Mar 21 22:10:23 image testapp[9265]: I am starting remote: Mar 21 22:10:23 image testapp[9265]: www-data remote: Mar 21 22:10:23 image systemd[1]: Started node.js testapp. remote: remote: ++++++++++++++++++++ See you soon at 'example.com' (1.2.3.4) ++++++++++++++++++++++ To git@example.com:~/testapp 08babc4..95cabcc master -> master $
-
Repeat: Develop, commit and push. This is how we did setup git push deployment with automated build and service restart.