I've seen git these days with git. There is a very handy feature to automate deployment to the server. And then I look, look, look, search, search, search, know this thing called hook script, when we push to the Git platform, git platform to monitor the push command, to trigger the server's hook script, implementation of server code updates, not every time on the server to pull the code.
I was in this time encountered a lot of problems, but a search on the internet, TMD on those several articles turn around, simply can not solve my problem. Now I'll talk to you slowly.
Basically is this meaning, can't say professional vocabulary, hahaha.
Let's talk about the real implementation process.
1. Put your project on the Git platform. This doesn't have to be taught. Need to teach on their own to Baidu, haha. After all, my article is to teach you Web hooks.
2. In the server view apache2 (I use the Apache service) execution user, execution code: PS-EF | grep apache2 #我的服务名是apache2, here according to their own real writing, such as some is httpd.
As pictured above, my Apache running user is www-data.
So when you execute a shell script using PHP (which I use PHP for flexibility), you use the Www-data user to execute the shell. The problem I'm having is right here, the permissions are incorrect. So we have to unify this user to perform all the operations.
In the server also use this user to configure the GIT public key, such as:
Sudo-u www-data ssh-keygen-t rsa-c "xxxxx@xxxxx.com"
After you configure the public key, your git has permission to execute pull under this user.
3. Use Apache to execute code on the user cloning Git platform, such as: Sudo-u www-data git clone git@xxxxxxx
Of course, there may be permission issues, said Www-data users do not have read and write permissions, you need to read and write permissions to the directory to Www-data users, here is not to explain.
When the clone is complete, we start step fourth:
4. Create an interface, address what their own configuration is good, interface to write a section of the code to execute the shell
I am here:
<pre name= "code" class= "HTML" ><?php
shell_exec ("CD/YOUR/GIT/DIR; Sudo-u www-data git pull 2<&1 ");
As a simple remark, we have configured this interface on the GIT platform and we can use the hook.
When you push locally, the server calls this interface to perform the sudo-u www-data git pull implementation automatic deployment.
In the platform can be set a password, password field, he is a JSON when your interface is actually used, it needs to be judged whether your git platform is calling your interface, if not you can block it out.
My explanation is here.
Don't believe you try?
I am because of this permission problem for a long time, if there are problems I can not, you will Google Baidu research it.
Haha, by the way done a blog to us to see, lest the next encounter problems have TMD is that several ...
All right, here we go.
Thank you all for coming.