Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simple automated GIT Deployment using Hooks

Simple automated GIT Deployment using GIT Hooks

Here are the simple steps needed to create a deployment from your lokal GIT repository to a server based on this in-depth tutorial.

How it works

You are developing in a working-copy on your local machine, lets say on the master branch. Most of the time, people would push code to a remote server like github.com or gitlab.com and pull or export it to a production server. Or you use a service like my Deepl.io to act upon a Web-Hook that's triggered that service.

But here, we add a "bare" git repository that we create on the production server and pusblish our branch (f.e. master) directly to that server. This repository acts upon the push event using a 'git-hook' to move the files into a deployment directory on your server. No need for a midle man.

This creates a scenario where there is no middle man, high security with encrypted communication (using ssh keys, only authorized people get access to the server) and high flexibility tue to the use of .sh scripts for the deployment.

Prerequisit

  1. Know how to use GIT, Terminal etc.
  2. Have a local working-working copy ready
  3. Have SSH access to your server using private/public key

Todos

  1. Create a folder to deploy to on production server (i.e. your httpds folder)
  2. Add a bare repository on the productions server
  3. Add the post-receive hook script to the bare repository (and make it executable)
  4. Add the remote-repository resided on the production server to your local repository
  5. Push to the production server, relax.

1. Have a local working-working copy ready

Nuf said. I asume we are working on master – but you could work on any branch.

2. Create a folder to deploy to

ssh into your prodctionserver:

$ ssh user@server.com
$ mkdir ~/deploy-folder

3. Add a bare repository on the productions server

Now we create a "bare" repository – one that does not contain the working copy files. It basicaly is the content of the .git repository folder in a normal working copy. Name it whatever you like, you can also ommit the .git part from project.git or leave it to create the repository in an exisiting empty folder:

$ git init --bare ~/project.git

4. Add the post-receive hook

This scrtipt is executed when the push from the local machine has been completed and moves the files into place. It recides in project.git/hooks/ and is named 'post-receive'. You can use vim to edit and create it. The script does check if the correct branch is pushed (not deploying a develop branch for example) and

#!/bin/bash
while read oldrev newrev ref
do
    # only checking out the master (or whatever branch you would like to deploy)
    if [[ $ref =~ .*/master$ ]];
    then
        echo "Master ref received.  Deploying master branch to production..."
        git --work-tree=/home/webuser/deploy-folder/ --git-dir=/home/webuser/project.git/ checkout -f
    else
        echo "Ref $ref successfully received.  Doing nothing: only the master branch may be deployed on this server."
    fi
done

Download post-receive for an improved version

5. Add remote-repository localy

Now we add the this bare repository to your local system as a remote. Where "production" is the name you want to give the remote. This also be called "staging" or "live" or "test" etc if you want to deploy to a different system or multiple systems.

$ cd ~/path/to/working-copy/
$ git remote add production demo@yourserver.com:project.git

Make sure "project.git" coresponds to the name you gave in step 3. If you are using Tower or a similar App, you will see the newly added remote in your sidebar under "Remotes" (make sure it's not collapsed).

6. Push to the production server

Now you can push the master branch to the production server:

$ git push production master

If you are using tower, you can drag&drop the master branch onto the new production remote. That's it. Have questions, improvements?

#!/bin/bash
TRAGET="/home/webuser/deploy-folder"
GIT_DIR="/home/webuser/www.git"
BRANCH="master"
while read oldrev newrev ref
do
# only checking out the master (or whatever branch you would like to deploy)
if [[ $ref = refs/heads/$BRANCH ]];
then
echo "Ref $ref received. Deploying ${BRANCH} branch to production..."
git --work-tree=$TRAGET --git-dir=$GIT_DIR checkout -f
else
echo "Ref $ref received. Doing nothing: only the ${BRANCH} branch may be deployed on this server."
fi
done
@life1347

This comment has been minimized.

Show comment
Hide comment
@life1347

life1347 Oct 24, 2016

this really help me a lot! thanks

life1347 commented Oct 24, 2016

this really help me a lot! thanks

@Yamilquery

This comment has been minimized.

Show comment
Hide comment
@Yamilquery

Yamilquery Feb 10, 2017

Arigato gozaimasu

Yamilquery commented Feb 10, 2017

Arigato gozaimasu

@Jossnaz

This comment has been minimized.

Show comment
Hide comment
@Jossnaz

Jossnaz Feb 19, 2017

so if the server disappears or you delete it, you lose your git repository...

what about staging or test environment? different git repository? this makes no sense

Jossnaz commented Feb 19, 2017

so if the server disappears or you delete it, you lose your git repository...

what about staging or test environment? different git repository? this makes no sense

@yarkovaleksei

This comment has been minimized.

Show comment
Hide comment
@yarkovaleksei

yarkovaleksei Feb 22, 2017

Something tells me that TRAGET It must be replaced by TARGET

yarkovaleksei commented Feb 22, 2017

Something tells me that TRAGET It must be replaced by TARGET

@chadmiller

This comment has been minimized.

Show comment
Hide comment
@chadmiller

chadmiller Feb 27, 2017

#!/bin/bash 
set -eu

TARGET="/deployment-location-here"
GIT_DIR="/trigger-location-here"
BRANCH="master"

while read oldrev newrev ref
do
        # only checking out the master (or whatever branch you would like to deploy)
        if [[ $ref = refs/heads/"$BRANCH" ]];
        then
                echo "Ref $ref received. Deploying ${BRANCH} branch to production..."
                git --work-tree="$TARGET" --git-dir="$GIT_DIR" checkout -f
        else
                echo "Ref $ref received. Doing nothing: only the ${BRANCH} branch may be deployed on this server."
        fi
done

chadmiller commented Feb 27, 2017

#!/bin/bash 
set -eu

TARGET="/deployment-location-here"
GIT_DIR="/trigger-location-here"
BRANCH="master"

while read oldrev newrev ref
do
        # only checking out the master (or whatever branch you would like to deploy)
        if [[ $ref = refs/heads/"$BRANCH" ]];
        then
                echo "Ref $ref received. Deploying ${BRANCH} branch to production..."
                git --work-tree="$TARGET" --git-dir="$GIT_DIR" checkout -f
        else
                echo "Ref $ref received. Doing nothing: only the ${BRANCH} branch may be deployed on this server."
        fi
done
@vqiu

This comment has been minimized.

Show comment
Hide comment
@vqiu

vqiu Apr 15, 2017

It's very helpful to me. Thanks.

vqiu commented Apr 15, 2017

It's very helpful to me. Thanks.

@ramseylove

This comment has been minimized.

Show comment
Hide comment
@ramseylove

ramseylove May 3, 2017

I did all this including making the post-receive script executable(chmod +x post-receive), but after I do a git push, no files show up in the target location. The git push looks successful. I am using command line so i didn't see the script responses but when I do a git remote status, or try to push again, git says that everything is up to date. What could be happening here? I tried this twice on two different hosting services.

ramseylove commented May 3, 2017

I did all this including making the post-receive script executable(chmod +x post-receive), but after I do a git push, no files show up in the target location. The git push looks successful. I am using command line so i didn't see the script responses but when I do a git remote status, or try to push again, git says that everything is up to date. What could be happening here? I tried this twice on two different hosting services.

@rajibdebslg

This comment has been minimized.

Show comment
Hide comment
@rajibdebslg

rajibdebslg May 5, 2017

Hi, It's very helpful, but I am facing an issue. I am using CWP web panel in my VPS. I am using git under my company domain. All things are working except, when I do a push, the files goes under development directory. But it showing "Internal Server Error", if I fix that using "Fix permission" tools in CWP admin, I am able to browse the URLs. But, post-receive hook permissions also changed and it's not working. Again if I add executable permission in post-receive files and push new update, the "Internal server error" starts showing. Please help

rajibdebslg commented May 5, 2017

Hi, It's very helpful, but I am facing an issue. I am using CWP web panel in my VPS. I am using git under my company domain. All things are working except, when I do a push, the files goes under development directory. But it showing "Internal Server Error", if I fix that using "Fix permission" tools in CWP admin, I am able to browse the URLs. But, post-receive hook permissions also changed and it's not working. Again if I add executable permission in post-receive files and push new update, the "Internal server error" starts showing. Please help

@orome

This comment has been minimized.

Show comment
Hide comment
@orome

orome May 26, 2017

@ramseylove: This doesn't nothing for me either. Shouldn't it be checkout -f ${BRANCH}?

orome commented May 26, 2017

@ramseylove: This doesn't nothing for me either. Shouldn't it be checkout -f ${BRANCH}?

@scriptburn

This comment has been minimized.

Show comment
Hide comment
@scriptburn

scriptburn Jun 29, 2017

Hello guys i made this auto deployment application which you can use
https://github.com/scriptburn/git-auto-deploy

scriptburn commented Jun 29, 2017

Hello guys i made this auto deployment application which you can use
https://github.com/scriptburn/git-auto-deploy

@frensuren

This comment has been minimized.

Show comment
Hide comment
@frensuren

frensuren Aug 4, 2017

Thanks for this great gist. I have though but one question. How can we setup this auto-deploy and checkout all the files as other user. I want to be able to checkout all the files and have the owner of the files to be www-data:www-data right now doing this way sets the user and group to whichever user create the post-receive hook.

frensuren commented Aug 4, 2017

Thanks for this great gist. I have though but one question. How can we setup this auto-deploy and checkout all the files as other user. I want to be able to checkout all the files and have the owner of the files to be www-data:www-data right now doing this way sets the user and group to whichever user create the post-receive hook.

@adnanh

This comment has been minimized.

Show comment
Hide comment
@zhengfish

This comment has been minimized.

Show comment
Hide comment
@zhengfish

zhengfish Sep 27, 2017

Hello, Guru,
I tried one of your hooks -- post-receive.
I found that it works only I use git over SSH, however it will not work if I use git over HTTP.
Do you know this? or Could you please give me any advice?

Thanks in advance.

zhengfish commented Sep 27, 2017

Hello, Guru,
I tried one of your hooks -- post-receive.
I found that it works only I use git over SSH, however it will not work if I use git over HTTP.
Do you know this? or Could you please give me any advice?

Thanks in advance.

@jopark94

This comment has been minimized.

Show comment
Hide comment
@jopark94

jopark94 Oct 2, 2017

I have similar problem with @zhengfish .
I'm trying to use it over HTTP, but it doesn't seem to work.
Are there any solutions to this?

jopark94 commented Oct 2, 2017

I have similar problem with @zhengfish .
I'm trying to use it over HTTP, but it doesn't seem to work.
Are there any solutions to this?

@ianmin2

This comment has been minimized.

Show comment
Hide comment
@ianmin2

ianmin2 Oct 6, 2017

This is the most to the point guide on this subject that I have come across.

This is about the tenth time i'm using it and it always gets the job done fast!

I am grateful for your sharing

ianmin2 commented Oct 6, 2017

This is the most to the point guide on this subject that I have come across.

This is about the tenth time i'm using it and it always gets the job done fast!

I am grateful for your sharing

@Nanod10

This comment has been minimized.

Show comment
Hide comment
@Nanod10

Nanod10 Oct 17, 2017

I have a question, what happens if I replace a directory structure of this style

/ home / username /
     ./public_html/
     [public files not included in repo]
         ./app_folder/
            [app files]
     ./app_core/
         [core files]

That's my directory structure ...
In my repository I do not have public_html files ...
I only have the directory structures (without all the other public_html files) and the app_folder and app_core files ...

I did not quite understand if using the Hook ...
1- What would happen in this case?
2- would lose the files of the public folder?
3 - would the files be combined?
4- What happens if I delete files from the repository? these will be eliminated?

I await your answers, tomorrow I will make some tests about it.

Thank you very much!!

--------------------- EDIT ---------------------

Well ... according to my tests:

  • files that match the output of the repository will be silently replaced.
  • Deleted files from the repository will be deleted from the output directory.
  • all other existing files not included will not be modified.
  • Folders are combined, if added files are added.
  • Folders removed from the repository, which exist on the server, are not deleted if they include files that are not included in the repository.
  • If the folder exists (as it is in the repository and the server) and is deleted, when they perform the push it will be completely eliminated.

There were some doubts that I had before using this directly in production since it was not clarified that is what happened explicitly.

Thank you very much for the post! The truth ... this is great! =)

Nanod10 commented Oct 17, 2017

I have a question, what happens if I replace a directory structure of this style

/ home / username /
     ./public_html/
     [public files not included in repo]
         ./app_folder/
            [app files]
     ./app_core/
         [core files]

That's my directory structure ...
In my repository I do not have public_html files ...
I only have the directory structures (without all the other public_html files) and the app_folder and app_core files ...

I did not quite understand if using the Hook ...
1- What would happen in this case?
2- would lose the files of the public folder?
3 - would the files be combined?
4- What happens if I delete files from the repository? these will be eliminated?

I await your answers, tomorrow I will make some tests about it.

Thank you very much!!

--------------------- EDIT ---------------------

Well ... according to my tests:

  • files that match the output of the repository will be silently replaced.
  • Deleted files from the repository will be deleted from the output directory.
  • all other existing files not included will not be modified.
  • Folders are combined, if added files are added.
  • Folders removed from the repository, which exist on the server, are not deleted if they include files that are not included in the repository.
  • If the folder exists (as it is in the repository and the server) and is deleted, when they perform the push it will be completely eliminated.

There were some doubts that I had before using this directly in production since it was not clarified that is what happened explicitly.

Thank you very much for the post! The truth ... this is great! =)

@cookra

This comment has been minimized.

Show comment
Hide comment
@cookra

cookra Oct 22, 2017

@Jossnaz

"so if the server disappears or you delete it, you lose your git repository...what about staging or test environment? different git repository? this makes no sense"

This is for a push to prod or any env you like..? Staging, testing a little old skool ;)

cookra commented Oct 22, 2017

@Jossnaz

"so if the server disappears or you delete it, you lose your git repository...what about staging or test environment? different git repository? this makes no sense"

This is for a push to prod or any env you like..? Staging, testing a little old skool ;)

@menriquez

This comment has been minimized.

Show comment
Hide comment
@menriquez

menriquez Nov 16, 2017

step 5....what is demo@yourserver.com supposed to reference?

menriquez commented Nov 16, 2017

step 5....what is demo@yourserver.com supposed to reference?

@mydudechris

This comment has been minimized.

Show comment
Hide comment
@mydudechris

mydudechris Nov 22, 2017

Yeah, I don't understand this line either:

git --work-tree=/home/webuser/deploy-folder/ --git-dir=/home/webuser/project.git/ checkout -f

Should it be something like git --work-tree=/var/www/html/ --git-dir=/https://github.com/username/projectname.git

What if you have configured your DO to use ssh keys with no password?

Also, you mention running this as an executable but you don't say anything about making it one. Does it run as a service, how does that work?

mydudechris commented Nov 22, 2017

Yeah, I don't understand this line either:

git --work-tree=/home/webuser/deploy-folder/ --git-dir=/home/webuser/project.git/ checkout -f

Should it be something like git --work-tree=/var/www/html/ --git-dir=/https://github.com/username/projectname.git

What if you have configured your DO to use ssh keys with no password?

Also, you mention running this as an executable but you don't say anything about making it one. Does it run as a service, how does that work?

@saurabh-sikchi

This comment has been minimized.

Show comment
Hide comment
@saurabh-sikchi

saurabh-sikchi Nov 29, 2017

To get this work in git bash for windows I had to run this command:

eval $(ssh-agent -s) && ssh-add "C:\Users\YOURNAMEHERE\.ssh\key_name"

where key_name is the OpenSSH key (not the ppk key that putty exports by default)

Hope this helps some poor soul stuck with windows.

saurabh-sikchi commented Nov 29, 2017

To get this work in git bash for windows I had to run this command:

eval $(ssh-agent -s) && ssh-add "C:\Users\YOURNAMEHERE\.ssh\key_name"

where key_name is the OpenSSH key (not the ppk key that putty exports by default)

Hope this helps some poor soul stuck with windows.

@adnanh

This comment has been minimized.

Show comment
Hide comment
@adnanh

adnanh Nov 30, 2017

One could also use webhook server and GitHub's outgoing webhooks to automate this.

adnanh commented Nov 30, 2017

One could also use webhook server and GitHub's outgoing webhooks to automate this.

@isaacalves

This comment has been minimized.

Show comment
Hide comment
@isaacalves

isaacalves Dec 18, 2017

I want to deploy my app's build folder contents to a folder on a Digital Ocean droplet.
I set up the post_receive hook and it works, but the files are never up-to-date.

For instance, if I do this:

  1. On the remote server, go to target folder (on which I want to checkout my build folder) and delete everything. List folder to make sure there's nothing there.
  2. Locally commit and push to production (which will trigger the post-receive hook on the remote)
  3. On the remote server, go to target folder to check the contents. The files are there, which means the post-receive hook worked.

But the files will be still outdated! They are an older version, I believe. Is this a caching issue? I was suppose to see this file main.9533e87b.css in a /static/css folder but I see this one: main.164fa07d.css .

If I run git log on my bare repo I can see the latest commits there. The build folder is in the repo. I'm pushing to origin (bitbucket) and I can see the correct file there. So how come the files that get checked out in the target folder and are out to date?

How can this possibly be?

isaacalves commented Dec 18, 2017

I want to deploy my app's build folder contents to a folder on a Digital Ocean droplet.
I set up the post_receive hook and it works, but the files are never up-to-date.

For instance, if I do this:

  1. On the remote server, go to target folder (on which I want to checkout my build folder) and delete everything. List folder to make sure there's nothing there.
  2. Locally commit and push to production (which will trigger the post-receive hook on the remote)
  3. On the remote server, go to target folder to check the contents. The files are there, which means the post-receive hook worked.

But the files will be still outdated! They are an older version, I believe. Is this a caching issue? I was suppose to see this file main.9533e87b.css in a /static/css folder but I see this one: main.164fa07d.css .

If I run git log on my bare repo I can see the latest commits there. The build folder is in the repo. I'm pushing to origin (bitbucket) and I can see the correct file there. So how come the files that get checked out in the target folder and are out to date?

How can this possibly be?

@pparthenhs

This comment has been minimized.

Show comment
Hide comment
@pparthenhs

pparthenhs Jan 24, 2018

A possible error while following the above process is the permission for file post-receive. The default creation of file has the permission read-write, but to work properly need to have also execute permion to do that execute the following command.
chom 777 $home/project.git/hooks/post-receive

pparthenhs commented Jan 24, 2018

A possible error while following the above process is the permission for file post-receive. The default creation of file has the permission read-write, but to work properly need to have also execute permion to do that execute the following command.
chom 777 $home/project.git/hooks/post-receive

@dantuck

This comment has been minimized.

Show comment
Hide comment
@dantuck

dantuck Jan 31, 2018

I would love to use this gist but I am having trouble setting something up on my productions windows webservers that allows me to push my code too. I am bound to windows unfortunately but was looking for a lightweight option with low impact to a production server that would set up a remote for the bare git repos. Any options?

dantuck commented Jan 31, 2018

I would love to use this gist but I am having trouble setting something up on my productions windows webservers that allows me to push my code too. I am bound to windows unfortunately but was looking for a lightweight option with low impact to a production server that would set up a remote for the bare git repos. Any options?

@TylerGubala

This comment has been minimized.

Show comment
Hide comment
@TylerGubala

TylerGubala Feb 26, 2018

I had a dumb issue I feel I need to share with everyone... feel free to point and laugh...

So I was using this tutorial, and I'm like "OMG! this is exactly what I want for my Angular 5 app!"

I have a Raspberry Pi 3 as my outward facing server. I have Samba configured on it so that I can easily edit the file on my Windows machine (make note of that as it is important).

So I create a new directory on my pi that I can read write and execute from and have my bare, and another directory, same permissions for my deployment.

I create a new file on Windows through Samba for the post-receive... and open it up and paste your post-receive hook code on my Windows machine's installation of Notepad++.

This is where it all went wrong... that littered the ENTIRE FILE with (CR)(LF) as the line ending, instead of the (LF) that bash is expecting.

To me, this came as a big surprise (novice at bash here, prefer Python), as for the last two days I have been debugging the post-receive hook.

I could not for the life of me figure out why I kept getting

remote: : No such file or directory.

Went back to Notepad++, clicked

Edit -> EOL Conversion -> Unix (LF)

Instantly worked!

Such a relief, hope this helps someone else!

TylerGubala commented Feb 26, 2018

I had a dumb issue I feel I need to share with everyone... feel free to point and laugh...

So I was using this tutorial, and I'm like "OMG! this is exactly what I want for my Angular 5 app!"

I have a Raspberry Pi 3 as my outward facing server. I have Samba configured on it so that I can easily edit the file on my Windows machine (make note of that as it is important).

So I create a new directory on my pi that I can read write and execute from and have my bare, and another directory, same permissions for my deployment.

I create a new file on Windows through Samba for the post-receive... and open it up and paste your post-receive hook code on my Windows machine's installation of Notepad++.

This is where it all went wrong... that littered the ENTIRE FILE with (CR)(LF) as the line ending, instead of the (LF) that bash is expecting.

To me, this came as a big surprise (novice at bash here, prefer Python), as for the last two days I have been debugging the post-receive hook.

I could not for the life of me figure out why I kept getting

remote: : No such file or directory.

Went back to Notepad++, clicked

Edit -> EOL Conversion -> Unix (LF)

Instantly worked!

Such a relief, hope this helps someone else!

@udezekene

This comment has been minimized.

Show comment
Hide comment
@udezekene

udezekene Mar 4, 2018

This works great for one repo to one server. But how do we approach this, if we have multiple repositories, we want to [automatically] deploy to the same folder on the server?

Does anyone have any solution to this?

udezekene commented Mar 4, 2018

This works great for one repo to one server. But how do we approach this, if we have multiple repositories, we want to [automatically] deploy to the same folder on the server?

Does anyone have any solution to this?

@Hailong

This comment has been minimized.

Show comment
Hide comment
@Hailong

Hailong Mar 9, 2018

You might also want to append the $BRANCH name to the end of git command, so that to make sure it checks out the expected branch. I had waste some time on this, because I had set my default branch to "master" while I'd like to deploy with another branch called "deploy".

#!/bin/bash
TRAGET="/home/webuser/deploy-folder"
GIT_DIR="/home/webuser/www.git"
BRANCH="master"

while read oldrev newrev ref
do
	# only checking out the master (or whatever branch you would like to deploy)
	if [[ $ref = refs/heads/$BRANCH ]];
	then
		echo "Ref $ref received. Deploying ${BRANCH} branch to production..."
		git --work-tree=$TRAGET --git-dir=$GIT_DIR checkout -f $BRANCH
	else
		echo "Ref $ref received. Doing nothing: only the ${BRANCH} branch may be deployed on this server."
	fi
done

Hailong commented Mar 9, 2018

You might also want to append the $BRANCH name to the end of git command, so that to make sure it checks out the expected branch. I had waste some time on this, because I had set my default branch to "master" while I'd like to deploy with another branch called "deploy".

#!/bin/bash
TRAGET="/home/webuser/deploy-folder"
GIT_DIR="/home/webuser/www.git"
BRANCH="master"

while read oldrev newrev ref
do
	# only checking out the master (or whatever branch you would like to deploy)
	if [[ $ref = refs/heads/$BRANCH ]];
	then
		echo "Ref $ref received. Deploying ${BRANCH} branch to production..."
		git --work-tree=$TRAGET --git-dir=$GIT_DIR checkout -f $BRANCH
	else
		echo "Ref $ref received. Doing nothing: only the ${BRANCH} branch may be deployed on this server."
	fi
done
@nesslopz

This comment has been minimized.

Show comment
Hide comment
@nesslopz

nesslopz Mar 14, 2018

Hi, If you're here looking for make deployment to same server but different branch, this worked for me

#!/bin/bash
while read oldrev newrev ref
do
    # only checking out the master (or whatever branch you would like to deploy)
    if [[ $ref = refs/heads/master ]];
    then
        echo "Master ref received.  Deploying master branch to production..."
        git --work-tree=/home/webuser/public --git-dir=/home/webuser/www.git checkout -f master
    else
        if [[ $ref = refs/heads/sandbox ]];
        then
                echo "Sandbox ref received. Deploying sandbox branch to beta..."
                git --work-tree=/home/webuser/beta --git-dir=/home/webuser/www.git checkout -f sandbox
        else
                echo "Ref $ref successfully received.  Doing nothing: only the master branch may be deployed on this server."
        fi
    fi
done

nesslopz commented Mar 14, 2018

Hi, If you're here looking for make deployment to same server but different branch, this worked for me

#!/bin/bash
while read oldrev newrev ref
do
    # only checking out the master (or whatever branch you would like to deploy)
    if [[ $ref = refs/heads/master ]];
    then
        echo "Master ref received.  Deploying master branch to production..."
        git --work-tree=/home/webuser/public --git-dir=/home/webuser/www.git checkout -f master
    else
        if [[ $ref = refs/heads/sandbox ]];
        then
                echo "Sandbox ref received. Deploying sandbox branch to beta..."
                git --work-tree=/home/webuser/beta --git-dir=/home/webuser/www.git checkout -f sandbox
        else
                echo "Ref $ref successfully received.  Doing nothing: only the master branch may be deployed on this server."
        fi
    fi
done
@aadii104

This comment has been minimized.

Show comment
Hide comment
@aadii104

aadii104 Apr 9, 2018

Hello all,
In my case, I am not using a local to do so.
I m having a team and I cannot give ssh access to everyone, also what If I am not pushing to Master branch. I mean merging branches should trigger the deployment, in that case what are the other changes?

Thanks. :)

aadii104 commented Apr 9, 2018

Hello all,
In my case, I am not using a local to do so.
I m having a team and I cannot give ssh access to everyone, also what If I am not pushing to Master branch. I mean merging branches should trigger the deployment, in that case what are the other changes?

Thanks. :)

@Onitz

This comment has been minimized.

Show comment
Hide comment
@Onitz

Onitz Jul 29, 2018

Mine worked after removing the first line #!/bin/bash :) :) :)
(also added execute permission chmod +x post-receive if it helps anyone)

Onitz commented Jul 29, 2018

Mine worked after removing the first line #!/bin/bash :) :) :)
(also added execute permission chmod +x post-receive if it helps anyone)

@thisisjamessmith

This comment has been minimized.

Show comment
Hide comment
@thisisjamessmith

thisisjamessmith Sep 10, 2018

In reference to @nesslopz's example above, for most use cases, there's probably no need to loop through the refs and take different actions depending on which branch has been pushed - you can simply trigger pulls from all the relevant environments, and as long as they are pulling different branches it'll all work fine. For example, this is what I use for deploying master to production and dev to staging:

cd /webserver/domains/example.com/dev/
sudo -u www-data git pull /gitserver/example.git dev

echo "Dev branch pulled in to staging.example.com"

cd /webserver/domains/example.com/prod/
sudo -u www-data git pull /gitserver/example.git master

echo "Master branch pulled in to example.com"

It simply means that when pushing the master branch, it'll also attempt to pull from the dev branch, but that pull will never actually do anything unless the dev branch has been pushed - there's no need to use conditionals to test for it. (Also, I'd say cding into the right directory is more readable and instantly understandable than using --work-tree, and the remote should already be set in the working copies, so no need for specifying the git-dir. I'm sandboxing this behaviour to a git user on the machine with restricted permissions, so I'm also needing to sudo as the www-data user so the pulled files maintain the correct permissions.

thisisjamessmith commented Sep 10, 2018

In reference to @nesslopz's example above, for most use cases, there's probably no need to loop through the refs and take different actions depending on which branch has been pushed - you can simply trigger pulls from all the relevant environments, and as long as they are pulling different branches it'll all work fine. For example, this is what I use for deploying master to production and dev to staging:

cd /webserver/domains/example.com/dev/
sudo -u www-data git pull /gitserver/example.git dev

echo "Dev branch pulled in to staging.example.com"

cd /webserver/domains/example.com/prod/
sudo -u www-data git pull /gitserver/example.git master

echo "Master branch pulled in to example.com"

It simply means that when pushing the master branch, it'll also attempt to pull from the dev branch, but that pull will never actually do anything unless the dev branch has been pushed - there's no need to use conditionals to test for it. (Also, I'd say cding into the right directory is more readable and instantly understandable than using --work-tree, and the remote should already be set in the working copies, so no need for specifying the git-dir. I'm sandboxing this behaviour to a git user on the machine with restricted permissions, so I'm also needing to sudo as the www-data user so the pulled files maintain the correct permissions.

@kazi-shahin

This comment has been minimized.

Show comment
Hide comment
@kazi-shahin

kazi-shahin Sep 17, 2018

How much time it will take to auto deploy? I mean when I be able o see the changes n web?
Is there any way to check whether I have configure it correctly or not ?

kazi-shahin commented Sep 17, 2018

How much time it will take to auto deploy? I mean when I be able o see the changes n web?
Is there any way to check whether I have configure it correctly or not ?

@ido2267

This comment has been minimized.

Show comment
Hide comment
@ido2267

ido2267 Sep 18, 2018

I am from Israel and English is not my first language but I am shure you suppose to write "Local" and not "Lokal"

ido2267 commented Sep 18, 2018

I am from Israel and English is not my first language but I am shure you suppose to write "Local" and not "Lokal"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment