There are thousands of files in the website. It is impossible to re-upload the entire website using FTP every time dozens of files are changed. However, it is not an easy task to remember which files have been changed each time. Moreover, these modified files are scattered in many different folders, making it more troublesome to find them one by one and upload them.
So how do you accurately and conveniently upload only those modified files every time?
PS. My current approach is to install git on the local development environment, install git on the remote server, and then push here and pull there. But you still need to telnet or ssh to the remote end to pull. Is there a more automatic method? After the push here, the server will automatically update? Want to hear everyone's suggestions.
The simplest method is to upload using git. git comes with hooks, which can realize automatic deployment.
I have done the same thing as the subject before, but it is easy to mess up the project when multiple people collaborate.
Thanks to @Cocbin for the reminder, based on which I carefully studied the hook principle of Git, and then did the following according to my project:
First of all, because I deployed the project on the github website, I don’t need to set up complicated hooks myself, but just use the webhook that comes with github. Webhook will trigger a callback URL you set when you push to GitHub, and then you can perform the pull operation in this URL.
In addition, on your server, do not use the developer's own ssh key to pull. Instead, generate a deploy key and put it in github. This key does not require manual password input and is specifically used to pull content from github. In this way, automatic deployment is completely completed.
I also looked at Hudson and Jenkins, both of which are very good tools. If you are doing a Java project, you should use Jenkins, because after Hudson was acquired by Oracle, it gradually became no longer open. But because my project is in PHP, I can’t afford to install another set of Java 8 for this, so I didn’t consider it. In fact, there is also a special Jenkins for PHP, but it still feels too heavy for me. Maybe I will wait until the project is bigger in the future. It’s time to consider using it.
The above are some experiences, I hope it will be helpful to students who have the same problem.
---------- This is the gorgeous dividing line --------------------------
Added:
The actual operation process is far more complicated than imagined. Here is the code:
At this time, when there is a push upload, github will call back, but the problem is that the user name apache is used during the callback, not the user name you used when sshing in, so you will find that the user name apache does not exist for that directory. Operation permissions:
error: cannot open .git/FETCH_HEAD: Permission denied
So you need to add ssh_user to the apache group first:
Then modify the permissions:
The reason why it is placed in this directory is because the default directory for the apache user is /var/www. Of course, you can change it to another directory, which may be safer.
Then:
It will generate a
known_hosts
文件,然后如果你是用的ssh方式,你还需要把上面生成的deploy key里的两个文件id_rsa
和id_rsa.pub
copy to the .ssh folder in apache. Then execute the following command several times to ensure that no errors are reported again. Then you can try to push a new version from the client to see if the automatic deployment can be successful.================ Domestic version =====================
Because Github's server is not in China, it often causes troubles of being unable to push/pull, so consider migrating to git.oschina.net in China. The migration process will not be described in detail. There are just a few things you need to pay attention to:
About webhooks code:
A crucial step needs to be executed. I was stuck here and couldn’t find the reason for a long time:
Everyone knows that git is version control
Let me talk about the methods I have used:
1. First, you must have a version control service and rsync service
2. Migrate the stable version code that needs to be deployed from the version server to a temporary folder
3. Move the rsync server Specify the directory to the temporary folder
4. Use the rsync command to synchronize the server directory to your deployment directory
5. Write a shell: update the version code to the temporary folder (incremental), and do some update operations on the temporary folder files (if needed), rsync synchronizes temporary folder files to deployment directory (incremental)
After completing the above process, you only need to run the shell in item 5 above every time you submit code.
Although it is a bit troublesome, it is relatively effective. If you don’t like it, don’t spray it.