Guest User

Untitled

a guest
Nov 6th, 2017
376
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.48 KB | None | 0 0
  1. Introduction
  2. For an introduction to Git and how to install, please refer to the introduction tutorial.
  3.  
  4. 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.
  5.  
  6. 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.
  7.  
  8. Server Setup
  9. Our fictitious workspace:
  10.  
  11. Your server live directory: /var/www/domain.com
  12.  
  13. Your server repository: /var/repo/site.git
  14.  
  15. 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?
  16.  
  17. Creating Our Repository
  18. Login to your VPS from command line and type the following:
  19.  
  20. cd /var
  21. mkdir repo && cd repo
  22. mkdir site.git && cd site.git
  23. git init --bare
  24. --bare means that our folder will have no source files, just the version control.
  25.  
  26. Hooks
  27. 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.
  28.  
  29. 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.
  30.  
  31. In our repository if you type:
  32.  
  33. ls
  34. You will see a few files and folders, including the 'hooks' folder. So let's go to 'hooks' folder:
  35.  
  36. cd hooks
  37. Now, create the file 'post-receive' by typing:
  38.  
  39. cat > post-receive
  40. 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:
  41.  
  42. #!/bin/sh
  43. git --work-tree=/var/www/domain.com --git-dir=/var/repo/site.git checkout -f
  44. When you finish typing, press 'control-d' to save. In order to execute the file, we need to set the proper permissions using:
  45.  
  46. chmod +x post-receive
  47. 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.
  48.  
  49. 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.
  50.  
  51. Local Machine
  52. 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:
  53.  
  54. exit
  55. And create your repo:
  56.  
  57. cd /my/workspace
  58. mkdir project && cd project
  59. git init
  60. Then we need to configure the remote path of our repository. Tell Git to add a remote called 'live':
  61.  
  62. git remote add live ssh://user@mydomain.com/var/repo/site.git
  63. Here we should give the repository link and not the live folder.
  64.  
  65. 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:
  66.  
  67. git add .
  68. git commit -m "My project is ready"
  69. 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.
  70.  
  71. git push live master
  72. 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://user@mydomain.com/var/repo/site.git* [new branch] master -> master
  73. 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.
  74.  
  75. Beta
  76. What if you don't want to deploy everything in one step? Maybe you want to test it first and have a beta directory.
  77.  
  78. One of the ways to do that is create another repository. Let's log in again in our VPS and create our directory:
  79.  
  80. cd /var/www/
  81. mkdir beta
  82. To create our repository:
  83.  
  84. cd /var/repo
  85. mkdir beta.git && cd beta.git
  86. git init --bare
  87. Again we should create the 'post-receive' file because we want to see our project in the beta directory:
  88.  
  89. cd hooks
  90. cat > post-receive
  91. Type the content of the file:
  92.  
  93. #!/bin/sh
  94. git --work-tree=/var/www/beta --git-dir=/var/repo/beta.git checkout -f
  95. When you finish typing, press 'control-d' to save. In order to execute the file, we need to set the proper permissions using:
  96.  
  97. chmod +x post-receive
  98. Let's go back to our local repository:
  99.  
  100. exit
  101. cd /my/workspace/project
  102. So now we can set another remote pointing to our beta repository:
  103.  
  104. git remote add beta ssh://user@mydomain.com/var/repo/beta.git
  105. 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:
  106.  
  107. git add .
  108. git commit -m "New version"
  109. git push beta master
  110. And later:
  111.  
  112. git push live master
  113. Going Live From the Server
  114. 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:
  115.  
  116. cd /var/repo/beta.git
  117. git remote add live ../site.git
  118. So now you can push from beta to live on the server:
  119.  
  120. cd /var/repo/beta.git
  121. git push live master
  122. Congratulations! Your VPS is now set to automatically deploy with Git!
Add Comment
Please, Sign In to add comment