Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save scottstanfield/9700098 to your computer and use it in GitHub Desktop.
Save scottstanfield/9700098 to your computer and use it in GitHub Desktop.

Revisions

  1. Thomas Fritz revised this gist Mar 21, 2014. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions Git push deployment in 7 easy steps.md
    Original 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 script from this gist
    # 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) 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.
    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
  2. Thomas Fritz revised this gist Mar 21, 2014. 1 changed file with 2 additions and 3 deletions.
    5 changes: 2 additions & 3 deletions Git push deployment in 7 easy steps.md
    Original 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`. (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)
    1. Create a user on `example.com`, as which we (the git client) connects (push) to `exmaple.com`.
    ```bash
    sudo groupadd -g 10001 git
    sudo useradd -u 10001 -g 10001 -d /home/git -m -s /usr/bin/git-shell git
    sudo useradd -m -s /usr/bin/git-shell git
    ```

    2. Add your ssh public key to the `authorized_keys` file of the created user:
  3. Thomas Fritz revised this gist Mar 21, 2014. 1 changed file with 6 additions and 4 deletions.
    10 changes: 6 additions & 4 deletions Git push deployment in 7 easy steps.md
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,8 @@
    On the server (example.com)
    =============
    # 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
    =============
    ## On the client

    7. Create a git repo and add our newly created `remote`:
    ```bash
  4. Thomas Fritz renamed this gist Mar 21, 2014. 1 changed file with 0 additions and 0 deletions.
  5. Thomas Fritz renamed this gist Mar 21, 2014. 1 changed file with 8 additions and 4 deletions.
    12 changes: 8 additions & 4 deletions 1_Preparations.md → GIT push deployment in 7 easy steps.md
    Original 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
    =============

    6. Create a git repo and add our newly created `remote`:
    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
    ```

    7. Commit and push to production:
    8. Commit and push to production:
    ```
    $ echo "foo" >> test
    $ 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
    $
    ```

    8. Repeat: Develop, commit and push :)
    9. Repeat: Develop, commit and push :)
    Congratulations, you just setup git push deployment with automated build and service restart


  6. Thomas Fritz renamed this gist Mar 21, 2014. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  7. Thomas Fritz revised this gist Mar 21, 2014. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions Preparations.md
    Original 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



  8. Thomas Fritz revised this gist Mar 21, 2014. 1 changed file with 23 additions and 0 deletions.
    23 changes: 23 additions & 0 deletions testapp.service
    Original 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
  9. Thomas Fritz revised this gist Mar 21, 2014. 2 changed files with 36 additions and 5 deletions.
    3 changes: 2 additions & 1 deletion Makefile
    Original 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
    @whoami
    @pwd
    38 changes: 34 additions & 4 deletions Preparations.md
    Original 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
    $ 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
    $
    ```


  10. Thomas Fritz revised this gist Mar 21, 2014. 1 changed file with 10 additions and 0 deletions.
    10 changes: 10 additions & 0 deletions Makefile
    Original 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
  11. Thomas Fritz revised this gist Mar 21, 2014. 1 changed file with 7 additions and 7 deletions.
    14 changes: 7 additions & 7 deletions Preparations.md
    Original 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`, 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)
    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 `git`s `authorized_keys` file:
    2. Add your ssh public key to the `authorized_keys` file of the created user:
    ```bash
    mkdir -p /home/git.ssh/
    vim /home/git/.ssh/authorized_keys
    # Paste your public key
    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
    sudo -u git bash
    cd ~
    mkdir testapp
    cd testapp
    git init --bare
  12. Thomas Fritz revised this gist Mar 21, 2014. 1 changed file with 14 additions and 16 deletions.
    30 changes: 14 additions & 16 deletions Preparations.md
    Original 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)
    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.
    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
    ```
    ```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
    ```
    ```
    echo "foo" >> test
    git add .
    git commit -am "test commit"
    git push production master
    ```



  13. Thomas Fritz revised this gist Mar 21, 2014. 1 changed file with 27 additions and 27 deletions.
    54 changes: 27 additions & 27 deletions Preparations.md
    Original 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
    ```
    ```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
    ```
    ```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
    ```
    ```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
    ```
    ```bash
    sudo chown root:git -R /var/www
    sudo chmod 775 /var/www
    ```


    On the client
  14. Thomas Fritz revised this gist Mar 21, 2014. 1 changed file with 8 additions and 12 deletions.
    20 changes: 8 additions & 12 deletions Preparations.md
    Original file line number Diff line number Diff line change
    @@ -1,24 +1,21 @@
    On the server
    On the server (example.com)
    =============

    * Create a user on the server `example.com`, which we (the git client) uses to connect (push).
    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
    ```

    * Add your ssh public key to `git`s `authorized_keys` file:

    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
    ```

    * Create a `git bare` repo for your project:

    3. Create a `git bare` repo for your project:
    ```bash
    sudo -u git bash
    cd ~
    @@ -27,15 +24,14 @@ cd testapp
    git init --bare
    ```

    * Copy the `post-receive` script from this gist to the `hooks` dir of the created bare repo.

    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
    ```

    * Set ownership and permissions of the `DEPLOY_ROOT` directory:
    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
    =============


    * Create a git repo and add our newly created `remote`:
    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
    ```

    * Commit and push to production:
    7. Commit and push to production:
    ```
    echo "foo" >> test
    git add .
  15. Thomas Fritz revised this gist Mar 21, 2014. 1 changed file with 17 additions and 4 deletions.
    21 changes: 17 additions & 4 deletions Preparations.md
    Original 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`

    * Create a git repo and add our newly created `remote`:
    ```bash
    mkdir testapp
    cd testapp
    git init
    git remote add production [email protected]:~/testapp
    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
    ```






  16. Thomas Fritz revised this gist Mar 21, 2014. 1 changed file with 14 additions and 5 deletions.
    19 changes: 14 additions & 5 deletions Preparations.md
    Original file line number Diff line number Diff line change
    @@ -1,15 +1,15 @@
    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
    * 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 the `git` users `authorized_keys` file:
    * 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
  17. Thomas Fritz revised this gist Mar 21, 2014. 1 changed file with 14 additions and 1 deletion.
    15 changes: 14 additions & 1 deletion Preparations.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,5 @@
    **On the server
    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
    _____________
  18. Thomas Fritz revised this gist Mar 21, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Preparations.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    == On the server ==
    **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
  19. Thomas Fritz revised this gist Mar 21, 2014. 1 changed file with 12 additions and 4 deletions.
    16 changes: 12 additions & 4 deletions Preparations.md
    Original 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
    * Create a `git bare` repo for your project:

    ```bash
    cd /home/git
    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 /home/git/testapp/hooks/post-receive
    vim testapp/hooks/post-receive
    # Paste the script from this gist
    chmod +x /home/git/testapp/hooks/post-receive
    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
    ```
  20. Thomas Fritz revised this gist Mar 21, 2014. 1 changed file with 8 additions and 0 deletions.
    8 changes: 8 additions & 0 deletions Preparations.md
    Original 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
    ```


  21. Thomas Fritz revised this gist Mar 21, 2014. 1 changed file with 13 additions and 2 deletions.
    15 changes: 13 additions & 2 deletions Preparations.md
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,18 @@
    * Create a user, we can then use to connect to this 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

    ```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
    ```


  22. Thomas Fritz revised this gist Mar 21, 2014. 1 changed file with 7 additions and 0 deletions.
    7 changes: 7 additions & 0 deletions Preparations.md
    Original 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
    ```
  23. Thomas Fritz revised this gist Mar 21, 2014. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions post-receive
    Original 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)
    #
  24. Thomas Fritz created this gist Mar 21, 2014.
    92 changes: 92 additions & 0 deletions post-receive
    Original 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