使用PHP自動部署GIT程式碼

WBOY
發布: 2016-07-29 09:15:40
原創
767 人瀏覽過

最近在使用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

. Looking something like

 

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) Considerations

the binary (

git in this case)

The user that is attempting to execute

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">&lt;code data-lang=&quot;php&quot;&gt;&lt;span&gt;&lt;?php &lt;span&gt;define&lt;span&gt;(&lt;span&gt;'PRIVATE_KEY'&lt;span&gt;, &lt;span&gt;'XXXXXXXXXXXXXXXXxxx'&lt;span&gt;); &lt;span&gt;if &lt;span&gt;(&lt;span&gt;$_SERVER&lt;span&gt;[&lt;span&gt;'REQUEST_METHOD'&lt;span&gt;] &lt;span&gt;=== &lt;span&gt;'POST' &lt;span&gt;&amp;&amp; &lt;span&gt;$_REQUEST&lt;span&gt;[&lt;span&gt;'thing'&lt;span&gt;] &lt;span&gt;=== &lt;span&gt;PRIVATE_KEY&lt;span&gt;) &lt;span&gt;{ &lt;span&gt;echo &lt;span&gt;shell_exec&lt;span&gt;(&lt;span&gt;&quot;git pull&quot;&lt;span&gt;); &lt;span&gt;} &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;</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">&lt;code data-lang=&quot;php&quot;&gt;&lt;span&gt;&lt;?php &lt;span&gt;... &lt;span&gt;echo &lt;span&gt;shell_exec&lt;span&gt;(&lt;span&gt;&quot;/full/path/to/bin/git pull 2&gt;&amp;1&quot;&lt;span&gt;); &lt;span&gt;... &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;</pre><div class="contentsignin">登入後複製</div></div>"Host key verification failed"

Next, you need to do an intial git pull with the apache user to make sure the remote is added to the apache user'ss🜎

<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

 as

 the ap

<code data-lang="text">chown -R ssh_user:www repository/
</code>
登入後複製

This creates the keys and puts them in their expected location with the proper permissions applied.

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">&lt;code data-lang=&quot;text&quot;&gt;chmod -R g+s repository/ &lt;/code&gt;</pre><div class="contentsignin">登入後複製</div></div> 以上就介紹了使用PHP自動部署GIT程式碼,包含了方面的內容,希望對PHP教學有興趣的朋友有幫助。

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板