Forked from thomasfr/Git push deployment in 7 easy steps.md
Created
March 22, 2014 02:12
-
-
Save scottstanfield/9700098 to your computer and use it in GitHub Desktop.
Revisions
-
Thomas Fritz revised this gist
Mar 21, 2014 . 1 changed file with 3 additions and 3 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -28,7 +28,7 @@ These are my notes basically. I first created this just as a reminder for myself 4. Copy the `post-receive` script from this gist to the `hooks` dir of the created bare repo. ```bash vim testapp/hooks/post-receive # Paste the contents of post-receive from this gist chmod +x testapp/hooks/post-receive ``` @@ -38,8 +38,8 @@ These are my notes basically. I first created this just as a reminder for myself sudo chmod 775 /var/www ``` 6. (Optional) Add a systemd service file for your app. If you are using `systemd`, you can use the `testapp.service` file from this gist. Make sure you name it like your repository, so the post-receive hook can automatically find and restart your app. ## On the client -
Thomas Fritz revised this gist
Mar 21, 2014 . 1 changed file with 2 additions and 3 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -4,10 +4,9 @@ These are my notes basically. I first created this just as a reminder for myself ## On the server (example.com) 1. Create a user on `example.com`, as which we (the git client) connects (push) to `exmaple.com`. ```bash sudo useradd -m -s /usr/bin/git-shell git ``` 2. Add your ssh public key to the `authorized_keys` file of the created user: -
Thomas Fritz revised this gist
Mar 21, 2014 . 1 changed file with 6 additions and 4 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,8 @@ # How-to setup a simple git push deployment These are my notes basically. I first created this just as a reminder for myself. Feel free to use this for your project as a starting point. ## On the server (example.com) 1. Create a user on `example.com`, as which we (the git client) connects (push) to `exmaple.com`. (I am using gid und uid higher than normally because, in case i want to restrict the user later based on the uid, i could then check for uid>10000 or gid>10000 - but you can safely ommit the `-u` and `-g` flags) ```bash @@ -40,8 +43,7 @@ On the server (example.com) If you are using `systemd`, you can use `testapp.service` from this gist. Make sure you name it like your repository, so the post-receive hook can automatically find and restart your app. ## On the client 7. Create a git repo and add our newly created `remote`: ```bash -
Thomas Fritz renamed this gist
Mar 21, 2014 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
Thomas Fritz renamed this gist
Mar 21, 2014 . 1 changed file with 8 additions and 4 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -36,21 +36,25 @@ On the server (example.com) sudo chmod 775 /var/www ``` 6. (Optional) Systemd service file for your app If you are using `systemd`, you can use `testapp.service` from this gist. Make sure you name it like your repository, so the post-receive hook can automatically find and restart your app. On the client ============= 7. Create a git repo and add our newly created `remote`: ```bash mkdir testapp cd testapp git init git remote add production [email protected]:~/testapp ``` 8. 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 @@ -86,7 +90,7 @@ On the client $ ``` 9. Repeat: Develop, commit and push :) Congratulations, you just setup git push deployment with automated build and service restart -
Thomas Fritz renamed this gist
Mar 21, 2014 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
Thomas Fritz revised this gist
Mar 21, 2014 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -86,6 +86,8 @@ On the client $ ``` 8. Repeat: Develop, commit and push :) Congratulations, you just setup git push deployment with automated build and service restart -
Thomas Fritz revised this gist
Mar 21, 2014 . 1 changed file with 23 additions and 0 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,23 @@ [Unit] Description=node.js testapp Requires=network.target After=network.target [Service] WorkingDirectory=/var/www/testapp Type=forking ExecStart=/bin/bash -c 'sleep 3;echo "I am starting";echo "$(whoami)";' # For a node.js app this could be something like: #ExecStart=/bin/bash -c 'npm start' StandardOutput=syslog StandardError=syslog SyslogIdentifier=testapp User=www-data Group=www-data Environment="NODE_ENV=production" "DEBUG=testapp:*" [Install] WantedBy=multi-user.target -
Thomas Fritz revised this gist
Mar 21, 2014 . 2 changed files with 36 additions and 5 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -7,4 +7,5 @@ deploy: update: @echo "Makefile: Doing UPDATE stuff like grunt, gulp, rake,..." @whoami @pwd 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 charactersOriginal file line number Diff line number Diff line change @@ -50,10 +50,40 @@ On the client 7. Commit and push to production: ``` $ echo "foo" >> test $ 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 [email protected]:~/testapp 08babc4..95cabcc master -> master $ ``` -
Thomas Fritz revised this gist
Mar 21, 2014 . 1 changed file with 10 additions and 0 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,10 @@ all: @echo "Doing all" deploy: @echo "Pushing to production" @git push [email protected]:~/testapp master update: @echo "Makefile: Doing UPDATE stuff like grunt, gulp, rake,..." @whoami -
Thomas Fritz revised this gist
Mar 21, 2014 . 1 changed file with 7 additions and 7 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,23 +1,23 @@ On the server (example.com) ============= 1. Create a user on `example.com`, as which we (the git client) connects (push) to `exmaple.com`. (I am using gid und uid higher than normally because, in case i want to restrict the user later based on the uid, i could then check for uid>10000 or gid>10000 - but you can safely ommit the `-u` and `-g` flags) ```bash sudo groupadd -g 10001 git sudo useradd -u 10001 -g 10001 -d /home/git -m -s /usr/bin/git-shell git ``` 2. Add your ssh public key to the `authorized_keys` file of the created user: ```bash sudo -u git bash cd ~ mkdir -p .ssh vim .ssh/authorized_keys # Paste your public key and save ``` 3. Create a `git bare` repo for your project: ```bash mkdir testapp cd testapp git init --bare -
Thomas Fritz revised this gist
Mar 21, 2014 . 1 changed file with 14 additions and 16 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,8 +1,7 @@ On the server (example.com) ============= 1. Create a user on `example.com`, which we (the git client) use(s) to connect (push). (I am using gid und uid higher than normally because, in case i want to restrict the user later based on the uid, i could then check for uid>10000 or gid>10000 - but you can safely ommit the `-u` and `-g` flags) ```bash sudo groupadd -g 10001 git sudo useradd -u 10001 -g 10001 -d /home/git -m -s /usr/bin/git-shell git @@ -24,7 +23,7 @@ On the server (example.com) git init --bare ``` 4. Copy the `post-receive` script from this gist to the `hooks` dir of the created bare repo. ```bash vim testapp/hooks/post-receive # Paste the script from this gist @@ -41,22 +40,21 @@ On the server (example.com) On the client ============= 6. Create a git repo and add our newly created `remote`: ```bash mkdir testapp cd testapp git init git remote add production [email protected]:~/testapp ``` 7. Commit and push to production: ``` echo "foo" >> test git add . git commit -am "test commit" git push production master ``` -
Thomas Fritz revised this gist
Mar 21, 2014 . 1 changed file with 27 additions and 27 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -3,39 +3,39 @@ On the server (example.com) 1. Create a user on `example.com`, which we (the git client) use(s) to connect (push). (I am using gid und uid higher than normally because, in case i want to restrict the user later based on the uid, i could then check for uid>10000 or gid>10000 - but you can safely ommit the `-u` and `-g` flags) ```bash sudo groupadd -g 10001 git sudo useradd -u 10001 -g 10001 -d /home/git -m -s /usr/bin/git-shell git ``` 2. Add your ssh public key to `git`s `authorized_keys` file: ```bash mkdir -p /home/git.ssh/ vim /home/git/.ssh/authorized_keys # Paste your public key ``` 3. Create a `git bare` repo for your project: ```bash sudo -u git bash cd ~ mkdir testapp cd testapp git init --bare ``` 4. Copy the `post-receive` script from this gist to the `hooks` dir of the created bare repo. ```bash vim testapp/hooks/post-receive # Paste the script from this gist chmod +x testapp/hooks/post-receive ``` 5. Set ownership and permissions of the `DEPLOY_ROOT` directory: ```bash sudo chown root:git -R /var/www sudo chmod 775 /var/www ``` On the client -
Thomas Fritz revised this gist
Mar 21, 2014 . 1 changed file with 8 additions and 12 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,24 +1,21 @@ On the server (example.com) ============= 1. Create a user on `example.com`, which we (the git client) use(s) to connect (push). (I am using gid und uid higher than normally because, in case i want to restrict the user later based on the uid, i could then check for uid>10000 or gid>10000 - but you can safely ommit the `-u` and `-g` flags) ```bash sudo groupadd -g 10001 git sudo useradd -u 10001 -g 10001 -d /home/git -m -s /usr/bin/git-shell git ``` 2. Add your ssh public key to `git`s `authorized_keys` file: ```bash mkdir -p /home/git.ssh/ vim /home/git/.ssh/authorized_keys # Paste your public key ``` 3. Create a `git bare` repo for your project: ```bash sudo -u git bash cd ~ @@ -27,15 +24,14 @@ cd testapp git init --bare ``` 4. Copy the `post-receive` script from this gist to the `hooks` dir of the created bare repo. ```bash vim testapp/hooks/post-receive # Paste the script from this gist chmod +x testapp/hooks/post-receive ``` 5. Set ownership and permissions of the `DEPLOY_ROOT` directory: ```bash sudo chown root:git -R /var/www sudo chmod 775 /var/www @@ -46,15 +42,15 @@ On the client ============= 6. Create a git repo and add our newly created `remote`: ```bash mkdir testapp cd testapp git init git remote add production [email protected]:~/testapp ``` 7. Commit and push to production: ``` echo "foo" >> test git add . -
Thomas Fritz revised this gist
Mar 21, 2014 . 1 changed file with 17 additions and 4 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -36,7 +36,6 @@ chmod +x testapp/hooks/post-receive ``` * Set ownership and permissions of the `DEPLOY_ROOT` directory: ```bash sudo chown root:git -R /var/www sudo chmod 775 /var/www @@ -47,10 +46,24 @@ On the client ============= * Create a git repo and add our newly created `remote`: ```bash mkdir testapp cd testapp git init git remote add production [email protected]:~/testapp ``` * Commit and push to production: ``` echo "foo" >> test git add . git commit -am "test commit" git push production master ``` -
Thomas Fritz revised this gist
Mar 21, 2014 . 1 changed file with 14 additions and 5 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,15 +1,15 @@ On the server ============= * Create a user on the server `example.com`, which we (the git client) uses to connect (push). (I am using gid und uid higher than normally because, in case i want to restrict the user later based on the uid, i could then check for uid>10000 or gid>10000 - but you can safely ommit the `-u` and `-g` flags) ```bash sudo groupadd -g 10001 git sudo useradd -u 10001 -g 10001 -d /home/git -m -s /usr/bin/git-shell git ``` * Add your ssh public key to `git`s `authorized_keys` file: ```bash mkdir -p /home/git.ssh/ @@ -44,4 +44,13 @@ sudo chmod 775 /var/www On the client ============= * Create a git repo and add our newly created `remote` ```bash mkdir testapp cd testapp git init git remote add production [email protected]:~/testapp -
Thomas Fritz revised this gist
Mar 21, 2014 . 1 changed file with 14 additions and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,5 @@ On the server _____________ * Create a user on the server, which we (the git client) uses to connect (push) to this server. I am using gid und uid higher than normally, if i want to restrict the user more than other user accounts, i could check for uid>10000 or gid>10000 @@ -8,6 +9,14 @@ sudo groupadd -g 10001 git sudo useradd -u 10001 -g 10001 -d /home/git -m -s /usr/bin/git-shell git ``` * Add your ssh public key to the `git` users `authorized_keys` file: ```bash mkdir -p /home/git.ssh/ vim /home/git/.ssh/authorized_keys # Paste your public key ``` * Create a `git bare` repo for your project: ```bash @@ -32,3 +41,7 @@ chmod +x testapp/hooks/post-receive sudo chown root:git -R /var/www sudo chmod 775 /var/www ``` On the client _____________ -
Thomas Fritz revised this gist
Mar 21, 2014 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,4 @@ **On the server * Create a user on the server, which we (the git client) uses to connect (push) to this server. I am using gid und uid higher than normally, if i want to restrict the user more than other user accounts, i could check for uid>10000 or gid>10000 -
Thomas Fritz revised this gist
Mar 21, 2014 . 1 changed file with 12 additions and 4 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,5 @@ == On the server == * Create a user on the server, which we (the git client) uses to connect (push) to this server. I am using gid und uid higher than normally, if i want to restrict the user more than other user accounts, i could check for uid>10000 or gid>10000 @@ -6,10 +8,11 @@ sudo groupadd -g 10001 git sudo useradd -u 10001 -g 10001 -d /home/git -m -s /usr/bin/git-shell git ``` * Create a `git bare` repo for your project: ```bash sudo -u git bash cd ~ mkdir testapp cd testapp git init --bare @@ -18,9 +21,14 @@ git init --bare * Copy the `post-receive` script from this gist to the `hooks` dir of the created bare repo. ```bash vim testapp/hooks/post-receive # Paste the script from this gist chmod +x testapp/hooks/post-receive ``` * Set ownership and permissions of the `DEPLOY_ROOT` directory: ```bash sudo chown root:git -R /var/www sudo chmod 775 /var/www ``` -
Thomas Fritz revised this gist
Mar 21, 2014 . 1 changed file with 8 additions and 0 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -15,4 +15,12 @@ cd testapp git init --bare ``` * Copy the `post-receive` script from this gist to the `hooks` dir of the created bare repo. ```bash vim /home/git/testapp/hooks/post-receive # Paste the script from this gist chmod +x /home/git/testapp/hooks/post-receive ``` -
Thomas Fritz revised this gist
Mar 21, 2014 . 1 changed file with 13 additions and 2 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,7 +1,18 @@ * Create a user on the server, which we (the git client) uses to connect (push) to this server. I am using gid und uid higher than normally, if i want to restrict the user more than other user accounts, i could check for uid>10000 or gid>10000 ```bash sudo groupadd -g 10001 git sudo useradd -u 10001 -g 10001 -d /home/git -m -s /usr/bin/git-shell git ``` * Create a `git bare` repo for your project ```bash cd /home/git mkdir testapp cd testapp git init --bare ``` -
Thomas Fritz revised this gist
Mar 21, 2014 . 1 changed file with 7 additions and 0 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,7 @@ * Create a user, we can then use to connect to this server. I am using gid und uid higher than normally, if i want to restrict the user more than other user accounts, i could check for uid>10000 or gid>10000 ```bash sudo groupadd -g 10001 git sudo useradd -u 10001 -g 10001 -d /home/git -m -s /usr/bin/git-shell git ``` -
Thomas Fritz revised this gist
Mar 21, 2014 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,7 @@ #!/bin/bash # # Author: "FRITZ Thomas" <[email protected]> (http://www.fritzthomas.com) # GitHub: https://gist.github.com/thomasfr/9691385 # # The MIT License (MIT) # -
Thomas Fritz created this gist
Mar 21, 2014 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,92 @@ #!/bin/bash # # Author: "FRITZ Thomas" <[email protected]> (http://www.fritzthomas.com) # # The MIT License (MIT) # # Copyright (c) 2014 FRITZ Thomas # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in all # copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. # # Received branch will be checked against this. Only deploy when receiving this branch. DEPLOY_BRANCH="master" # This is the root deploy dir. DEPLOY_ROOT="/var/www" # This will be 'eval'ed after git checkout -f step. # Use this to call your build/update script or run make, grunt, gulp, etc. # If empty or not set this step gets ignored. UPDATE_CMD='cd "${DEPLOY_TO}" && make "update"' # Command gets executed after 'UPDATE_CMD' has finished. Use this to restart running services # or daemons. If empty or unset this step gets ignored. RESTART_CMD='sudo systemctl restart "${PROJECT_NAME}.service" && sudo systemctl status "${PROJECT_NAME}.service"' ########################################################################################### export GIT_DIR="$(cd $(dirname $(dirname $0));pwd)" export PROJECT_NAME="${GIT_DIR##*/}" export DEPLOY_TO="${DEPLOY_ROOT}/${PROJECT_NAME}" export GIT_WORK_TREE="${DEPLOY_TO}" IP="$(ip addr show eth0 | grep 'inet ' | cut -f2 | awk '{ print $2}')" echo "+++++++++++++++++++++++ Welcome to '${HOSTNAME}' (${IP}) ++++++++++++++++++++++++" echo # Make sure directory exists. Maybe its deployed for the first time. mkdir -p "${DEPLOY_TO}" # Loop, because it is possible to push more than one branch at a time. (git push --all) while read oldrev newrev refname do branch=$(git rev-parse --symbolic --abbrev-ref $refname) if [ "${DEPLOY_BRANCH}" == "$branch" ]; then # Make sure GIT_DIR and GIT_WORK_TREE is correctly set and 'export'ed. Otherwhise # these two environment variables could also be passed as parameters to the git cli echo "githook: I will deploy '${DEPLOY_BRANCH}' branch of the '${PROJECT_NAME}' project to '${DEPLOY_TO}'" git checkout -f if [ ! -z "${UPDATE_CMD}" ]; then echo echo "githook: UPDATE (CMD: '${UPDATE_CMD}'):" eval $UPDATE_CMD fi if [ ! -z "${RESTART_CMD}" ]; then echo echo "githook: RESTART (CMD: '${RESTART_CMD}'):" eval $RESTART_CMD fi else echo "githook: I am NOT going to deploy the '${branch}' branch of the '${PROJECT_NAME}' project. Expected branch to deploy is '${DEPLOY_BRANCH}'." fi done echo echo "++++++++++++++++++++ See you soon at '${HOSTNAME}' (${IP}) ++++++++++++++++++++++" exit 0