12/08/2018, 11:04

Turn Local/VPS Into Git Version Controll.

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

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 --bare sẽ có một thư mục là hooks
  • thư mục hooks chứ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-receive sẽ được gọi khi bạn thực hiện các thao tác push, update.
  • file post-receive sẽ được gọi khi ban thực hiện xong thao tác push.
  • 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 khi push từ 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-receive bằ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/abc
    • live là 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ống git để 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 git apt-get install git).
    • domain : nếu ở local bạn ghi là localhost nếu ở vps bạn ghi là domain của vps đó ví dụ 133.242.153.83
    • ~/Repositories/abc chí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)

0