Requirements:
- I have a computer at home/office.
- We have a VPS on web.
- On VPS we have a location for our website i.e. /var/www/example.com
- I need a version control system.
- I don't want to develop and upload changes manually.
- I want to push a specific branch i.e. www to vps for deployment.
How can I achieve my goal? Find below the answer:
1. Lets create an example directory first.
Lets create a temp directory and then create sub-directories under it.
dev = My development computer
mahesh@mknsoft.com ~
[06:43:00]⌘ mkdir test && cd test
mahesh@mknsoft.com ~/test
[06:43:14]⌘ mkdir vps dev www
vps = Virtual Private Server on Internet
www = i,e, /var/www/example.com/
2. Setup a --bare git repo in vps.
mahesh@mknsoft.com ~/test
[06:43:33]⌘ cd vps
mahesh@mknsoft.com ~/test/vps
[06:45:51]⌘ git init --bare
Initialized empty Git repository in /Users/mahesh/test/vps/
3. Setup git repo in dev, add remote and do initial commit.
mahesh@mknsoft.com (BARE:master) ~/test/vps
[06:45:59]⌘ cd ../dev
mahesh@mknsoft.com ~/test/dev
[06:49:16]⌘ git init
Initialized empty Git repository in /Users/mahesh/test/dev/.git/
mahesh@mknsoft.com (master #) ~/test/dev
[06:49:31]⌘ git remote add origin ../vps
mahesh@mknsoft.com (master #) ~/test/dev
[06:50:15]⌘ echo "Test line 1" >> file.txt
mahesh@mknsoft.com (master #) ~/test/dev
[06:50:40]⌘ git add .
mahesh@mknsoft.com (master #) ~/test/dev
[06:50:50]⌘ git commit -m "Initial commit."
[master (root-commit) 1683234] Initial commit.
1 file changed, 1 insertion(+)
create mode 100644 file.txt
mahesh@mknsoft.com (master) ~/test/dev
[06:51:07]⌘ git push origin master
Counting objects: 3, done.
Writing objects: 100% (3/3), 231 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To ../vps
* [new branch] master -> master
4. How to publish?
At this point we are still unable to publish anything inside www directory. Now what can I do? Humm..., this is where hooks come to picture. Lets create a hook (post-receive) inside vps to track new pushes.
mahesh@mknsoft.com (master) ~/test/dev
[06:51:21]⌘ cd ../vps/hooks
mahesh@mknsoft.com (BARE:master) ~/test/vps/hooks
[07:02:04]⌘ touch post-receive
mahesh@mknsoft.com (BARE:master) ~/test/vps/hooks
[07:02:28]⌘ chmod +x post-receive
Lets get inside post-receive and write some code as per requirement.
mahesh@mknsoft.com (BARE:master) ~/test/vps/hooks
[07:02:49]⌘ vim post-receive
1 #!/bin/bash
2
3 read oldrev newrev refname
4 echo "Old revision : $oldrev"
5 echo "New revision : $newrev"
6 echo "Reference name: $refname"
7
8 BRANCH=${refname#refs/heads/}
9
10 echo "Pushed branch : $BRANCH"
11
12 if [ $BRANCH = www ]; then
13 export GIT_WORK_TREE=/Users/mahesh/test/www
14 git checkout -f www
15 echo "Published."
16 else
17 echo "Push on branch www to publish"
18 fi
mahesh@mknsoft.com (BARE:master) ~/test/vps/hooks
[07:10:50]⌘ cd ../../dev
mahesh@mknsoft.com (master) ~/test/dev
[07:14:13]⌘ git branch www && git checkout www
Switched to branch 'www'
mahesh@mknsoft.com (www) ~/test/dev
[07:14:44]⌘ git push origin www
Total 0 (delta 0), reused 0 (delta 0)
remote: Old revision : 0000000000000000000000000000000000000000
remote: New revision : 168323487f78ac6e03022ed4b456e1c0e95af21b
remote: Reference name: refs/heads/www
remote: Pushed branch : www
remote: Switched to branch 'www'
remote: Published.
To ../vps
* [new branch] www -> www
mahesh@mknsoft.com (www) ~/test/dev
[07:15:28]⌘ ls ../www
file.txt
Note: If you push in master branch, new updates will not be published as condition specified in post-receive source code.