最近在使用Coding的程式碼託管,順便設定了WebHook自動部署,過程還挺艱辛的,主要還是沒搞懂Linux的權限控制,不過好在弄好了,分享一下獲益最深的一篇文章,供大家參考,原文是英文版的,我的英文也不行,勉強能看懂,大家湊合著看吧
原文連結:http://jondavidjohn.com/git-pull-from-a-php-script- not-so-simple/
I intended to set up a repository (hosted on BitBucket) to initiate a pull on a dev server when new commits are pushed up.
It smeds new commits are pushed up.. will fire off a POST request as a post-receive hook. So I set up a receiving php script to check a randomized token and then initiate the
git pull
git pull
。 't end up being as simple as I had anticipated...
There were a few considerations that I did not take into account. Documenting them here will hopefully help you avoid some obstacles in 做
(Missed) Considerationsthe binary (git in this case)
ache case). This user did not happen to have
git in their path.
This took a while to track down beca the ly because they only report STDOUT and not STDERR. To get the function to report STDERR you can route it into STDOUT by adding 2->&1
at the end of your command. 名詞
at the end of your com of the git binary with which git, which is
/full/path/to/bin/git.🎼 ee
The apache user also needs read and write access to the entire repository.<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false"><code data-lang="php"><span><?php
<span>define<span>(<span>'PRIVATE_KEY'<span>, <span>'XXXXXXXXXXXXXXXXxxx'<span>);
<span>if <span>(<span>$_SERVER<span>[<span>'REQUEST_METHOD'<span>] <span>=== <span>'POST'
<span>&& <span>$_REQUEST<span>[<span>'thing'<span>] <span>=== <span>PRIVATE_KEY<span>)
<span>{
<span>echo <span>shell_exec<span>(<span>"git pull"<span>);
<span>}
</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></code></pre><div class="contentsignin">登入後複製</div></div>
It's also a good idea to make sure any files/director inherit in ownies sper 片段 <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false"><code data-lang="php"><span><?php
<span>...
<span>echo <span>shell_exec<span>(<span>"/full/path/to/bin/git pull 2>&1"<span>);
<span>...
</span></span></span></code></pre><div class="contentsignin">登入後複製</div></div>
"Host key verification failed"
<code data-lang="text">error: cannot open .git/FETCH_HEAD: Permission denied </code>
ssh key
Another consideration created by this command being run by the apache user is the ssh key it uses to communicate with the remote repository.
First, I went downdown the remote repository.
First, I went downdown the page of atlting variable to set the
ssh -i
option to tell it to use a specific ssh key I had generated with the ssh user. I never got this to work, most likely because there are a lot of rules a given key. It requires some specific permissions regarding the user that is attempting to use the key.
An way I discovered was to give the apache user and a .ssh
directory and then run the ssh-keygen
command
asthe ap
<code data-lang="text">chown -R ssh_user:www repository/ </code>
Then I added the key as a read-only key for the BitBucket repository and everything worked as expected.<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false"><code data-lang="text">chmod -R g+s repository/
</code></pre><div class="contentsignin">登入後複製</div></div>
以上就介紹了使用PHP自動部署GIT程式碼,包含了方面的內容,希望對PHP教學有興趣的朋友有幫助。