Turn Local/VPS Into Git Version Controll.
Bài đăng này đã không được cập nhật trong 10 năm
Vấn đề:
- Ta có 1 VPS (Virtual Private Server), bình thường nếu muốn deploy một project lên VPS có rất nhiều cách:
- 
Push lên Version controll (Git, Bitbucket, SVN...) sau đó lên VPS clone về.
 - 
Mỗi framework đều cung cấp một tool để deploy rất đơn giản và tiện dụng ta có thể sử dụng các tools này.
 - 
Dùng sshc (SSH copy) copy cả folder source thông qua SSH
 - 
Nhưng có một cách nào để Push trực tiếp code từ client lên VPS và có thể quản lý được từng version cũng như branch của nó. 
 - 
 
- Memo Git:
$ git config --global push.default matching
# sau khi chạy câu lệnh trên thì ta đã setup việc push theo đúng branch
# Nghĩa là chỉ cần git push -f là nó sẽ tuwh push lên Github với branch hiên tại theo remote origin
Trả lời:
- Tạo Git repository trên VPS.
 - Nghĩa là tất cả các thao tác với Github ta sẽ có thể thực hiện trên chính VPS của chúng ta
 - Thay vì store source ở Github ta sẽ store source ở chính VPS.
 - Các thao tác bên dưới làm tương tự nếu trên Localhost.
 
A : Setup on Git server site
step 1: Create a Git repository:
- Ta sẽ khởi tạo một Git repository trên máy local(hoặc vps) ~/Repositories/abc.
 - thư mục ~/Repositories/abc là thư mục đóng vai trò làm Git Version Controll.
 - thư mục ~/Repositories/abc quản lý source chứ không phải là nơi lưu trữ source.
 - Mỗi project sẽ có một repository tương ứng để quản lý source của project đó, vậy nên ta sẽ tạo nhiều reporitory khi làm việc với nhiều project.
 - ~/Repositories/abc ( quản lý source của project abc).
 - ~/Repositories/xyz (quản lý source của project xyz).
 - Demo với project có tên là "abc"
 
$ cd ~/
$ mkdir Repositories
$ cd Repositories
$ mkdir abc
$ cd abc
$ git init --bare
step 2: Hooks
- Mỗi Git repository (trong mỗi thư mục abc hay xyz) sau khi chạy 
git init --baresẽ có một thư mục làhooks - thư mục 
hookschứa những file để handle những action khi bạn request lên server. - Bạn có thể customize những file trong thư mục 
hooksđể thực hiện một action theo ý bạn. - file 
pre-receivesẽ được gọi khi bạn thực hiện các thao tácpush,update. - file 
post-receivesẽ được gọi khi ban thực hiện xong thao tácpush. - Các file trong 
hooksđều là shell script và được execute bởi bash. 
step 3: Defind where are you want to store your source.
- Ta sẽ sử dụng file 
post-receiveđể định nghĩa ta sẽ lưu trữ source ở đâu sau khipushtừ client. - Tạo file 
post-receive - File 
post-receiveđơn giản là một shell script sẽ được execute bởi bash shell. - Chúng ta hoàn toàn có thể code file 
post-receivebằng code Ruby khi đó đặt Shebang của file bằng Ruby#!/usr/bin/env ruby, nhưng chỉ để tham khảo, ta vẫn sẽ dùng bash script nhé. 
#!/usr/bin/env ruby
# post-receive
# 1. Read STDIN (Format: "from_commit to_commit branch_name")
from, to, branch = ARGF.read.split " "
# 2. Only deploy if master branch was pushed
if (branch =~ /master$/) == nil
    puts "Received branch #{branch}, not deploying."
    exit
end
# 3. Copy files to deploy directory
deploy_to_dir = File.expand_path('../deploy')
`GIT_WORK_TREE="#{deploy_to_dir}" git checkout -f master`
puts "DEPLOY: master(#{to}) copied to '#{deploy_to_dir}'"
# 4.TODO: Deployment Tasks
# i.e.: Run Puppet Apply, Restart Daemons, etc
 $ vim post-receive
- Nội dung file 
post-receive 
#!/bin/sh
git --work-tree=/~/project_store_place --git-dir=/~/Repositories/git_repository checkout -f
- 
-work-tree: vị trí lưu trữ source - 
-git-dir: vị trí của Git repository. - 
/~/project_store_place: Sửa theo nơi mà bạn muốn lưu trữ source - 
/~/Repositories/git_repository: Vị trí mà bạn đã tạo Git repository ởstep 1 - 
Change mode của file
post-receive 
$ sudo chmod +x post-receive
B: Setup on Git client site
- Giả sử bạn tạo một project mới và sẽ push lên server (nếu đã có một project rồi thì cũng tương tự)
 
Step 1: Create new project.
$ cd ~/
$ mkdir my_abc_project
$ cd my_abc_project
$ git init
$ vim first_commit_file
Step 2: Configure the remote path of our repository
- Định nghĩa remote path
 git remote add live ssh://user@mydomain/~/Repositories/abclivelà tên của remote.user: user mà bạn đã sử dụng để tạo Git repository ở bước 1 (bình thường ta dùng chính user của hệ thốnggitđể thao tác nhưng bạn cũng có thể dùng một user khác, user git được tạo khi bạn cài gitapt-get install git).domain: nếu ở local bạn ghi làlocalhostnếu ở vps bạn ghi là domain của vps đó ví dụ133.242.153.83~/Repositories/abcchính là Git repository bạn đã khởi tạo trên server từ bước 1 của phần Git server
- Nếu muốn sửa một remote đã có
git remote set-url live ssh://leminhtuan@localhost/~/Repositories/abc
 
Step 3: Push
- ta sẽ push code lên server nhé.
 
$ git add -A
$ git commit -m "My project is ready"
$ git push live master
- Bây giờ vào thư mục mà bạn đã định nghĩa để lưu trữ source trong file 
post-receive, bạn sẽ thấy code của project sẽ được lưu tại đó. 
Thanks.
Thank you so much for reading. Please give me some feedbacks or leave a comment so that i could be better myself. (thankyou)
All rights reserved