-
Notifications
You must be signed in to change notification settings - Fork 0
/
new_git_setup
executable file
·171 lines (126 loc) · 8.48 KB
/
new_git_setup
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
New setup:
https://www.digitalocean.com/community/tutorials/how-to-set-up-automatic-deployment-with-git-with-a-vps
How To Set Up Automatic Deployment with Git with a VPS
Posted Aug 8, 2013 333.4k views Git Configuration Management Ubuntu
Introduction
For an introduction to Git and how to install, please refer to the introduction tutorial.
This article will teach you how to use Git when you want to deploy your application. While there are many ways to use Git to deploy our application, this tutorial will focus on the one that is most straightforward. I assume you already know how to create and use a repository on your local machine. If not, please refer to this tutorial.
When you use Git, the workflow generally is toward version control only. You have a local repository where you work and a remote repository where you keep everything in sync and can work with a team and different machines. But you can also use Git to move your application to production.
Server Setup
Our fictitious workspace:
Your server live directory: /var/www/domain.com
Your server repository: /var/repo/site.git
What should we do if we want to push to site.git and at the same time make all the content available at /var/www/domain.com?
Creating Our Repository
Login to your VPS from command line and type the following:
cd /var
mkdir repo && cd repo
mkdir site.git && cd site.git
git init --bare
--bare means that our folder will have no source files, just the version control.
Hooks
Git repositories have a folder called 'hooks'. This folder contains some sample files for possible actions that you can hook and perform custom actions set by you.
Git documentation define three possible server hooks: 'pre-receive', 'post-receive' and 'update'. 'Pre-receive' is executed as soon as the server receives a 'push', 'update' is similar but it executes once for each branch, and 'post-receive' is executed when a 'push' is completely finished and it's the one we are interested in.
In our repository if you type:
ls
You will see a few files and folders, including the 'hooks' folder. So let's go to 'hooks' folder:
cd hooks
Now, create the file 'post-receive' by typing:
cat > post-receive
When you execute this command, you will have a blank line indicating that everything you type will be saved to this file. So let's type:
#!/bin/sh
git --work-tree=/var/www/domain.com --git-dir=/var/repo/site.git checkout -f
When you finish typing, press 'control-d' to save. In order to execute the file, we need to set the proper permissions using:
chmod +x post-receive
You can see on the documentation that 'git-dir' is the path to the repository. With 'work-tree', you can define a different path to where your files will actually be transferred to.
The 'post-receive' file will be looked into every time a push is completed and it's saying that your files need to be in /var/www/domain.com.
Local Machine
Let's create our local repository. You should change the path and name to whichever you choose. If you are on a VPS, just type:
exit
And create your repo:
cd /my/workspace
mkdir project && cd project
git init
Then we need to configure the remote path of our repository. Tell Git to add a remote called 'live':
git remote add live ssh://[email protected]/var/repo/site.git
Here we should give the repository link and not the live folder.
Let's assume that we have some great work ready in this folder. We should do the usual steps of adding the files and commit with a message:
git add .
git commit -m "My project is ready"
Just to remember, the dot after 'git add' means you are adding all files to stage. After 'git commit' we have '-m' which means we will type a message. To complete, we just 'push' everything to the server. We use the 'live' alias that we used when setting the remote.
git push live master
Counting objects: 7, done.Delta compression using up to 4 threads.Compressing objects: 100% (7/7), done.Writing objects: 100% (7/7), 10.56 KiB, done.Total 7 (delta 0), reused 0 (delta 0)To ssh://[email protected]/var/repo/site.git* [new branch] master -> master
Here we tell Git to push to the 'live' remote on the 'master' branch. To understand more about branches and how to use it you can read this tutorial.
Beta
What if you don't want to deploy everything in one step? Maybe you want to test it first and have a beta directory.
One of the ways to do that is create another repository. Let's log in again in our VPS and create our directory:
cd /var/www/
mkdir beta
To create our repository:
cd /var/repo
mkdir beta.git && cd beta.git
git init --bare
Again we should create the 'post-receive' file because we want to see our project in the beta directory:
cd hooks
cat > post-receive
Type the content of the file:
#!/bin/sh
git --work-tree=/var/www/beta --git-dir=/var/repo/beta.git checkout -f
When you finish typing, press 'control-d' to save. In order to execute the file, we need to set the proper permissions using:
chmod +x post-receive
Let's go back to our local repository:
exit
cd /my/workspace/project
So now we can set another remote pointing to our beta repository:
git remote add beta ssh://[email protected]/var/repo/beta.git
With this, we can have a two step process. First we push to beta and check, and if everything is fine we push to live:
git add .
git commit -m "New version"
git push beta master
And later:
git push live master
Going Live From the Server
Maybe you have a team working in the same project and you want that others can also decide that it's time to go live. To do this, we can link the beta and live repository on the server. Log in to your VPS and type:
cd /var/repo/beta.git
git remote add live ../site.git
So now you can push from beta to live on the server:
cd /var/repo/beta.git
git push live master
Congratulations! Your VPS is now set to automatically deploy with Git!
Old Git setup::
Steps (NOT THE RIGHT WAY, RESEARCH AND FIGURE OUT HOW THIS SHOULD BE DONE)
1) Install git
2) Create a directory on the server for the html files/site. Probably something like: /usr/share/nginx/html/seticorp.com in the case of digitalocean.com, or /var/www if a regular server, or something else if a rails app
3) Adjust the permission and ownership. Something like: chown R www-data:jim directory, sudo chmod -R 770 seticorp.com/ (Later we'll change some of the file permission that we don't want the webserver to see back.)
4) 'git init' in the directory
5) On the local/development machine, without creating the directory, above where you want the directory, run clone ssh://[email protected]/usr/share/nginx/html/seticorp.com
4) On the remote origin, run git status (should be master and clean)
5) git checkout -b deploy Otherwise you'll see:
error: src refspec deploy does not match any.
error: failed to push some refs to 'ssh://[email protected]/usr/share/nginx/html/jamesmcbride.us'
6) Git status
7) Checkout branch deploy (git checkout deploy)
8) Change/add/remove files in this directory
9) git add .
10) git commit -am "Write message about changes"
11) git push origin deploy
12) In the website directory on the server, (for example, "/var/www/thecarringtonfirm.com") checkout the main branch if it's not aleady ('git status', then if needed 'git checkout master')
13) type 'git merge deploy'
Steps (NOT THE RIGHT WAY, RESEARCH AND FIGURE OUT HOW THIS SHOULD BE DONE)
1) Install git
2) Create a directory on the server for the html files/site. Probably something like: /usr/share/nginx/html/seticorp.com in the case of digitalocean.com, or /var/www if a regular server, or something else if a rails app
3) Adjust the permission and ownership. Something like: chown R www-data:jim directory, sudo chmod -R 770 seticorp.com/ (Later we'll change some of the file permission that we don't want the webserver to see back.)
4) 'git init' in the directory
5) On the local/development machine, without creating the directory, above where you want the directory, run clone ssh://[email protected]/usr/share/nginx/html/seticorp.com
4) On the remote origin, run git status (should be master and clean)
5) git checkout -b deploy Otherwise you'll see:
error: src refspec deploy does not match any.
error: failed to push some refs to 'ssh://[email protected]/usr/share/nginx/html/jamesmcbride.us'
6) Git status
7) Checkout branch deploy (git checkout deploy)
8) Change/add/remove files in this directory
9) git add .
10) git commit -am "Write message about changes"
11) git push origin deploy
12) In the website directory on the server, (for example, "/var/www/thecarringtonfirm.com") checkout the main branch if it's not aleady ('git status', then if needed 'git checkout master')
13) type 'git merge deploy'