使用PHP自动部署GIT代码,php部署git
使用PHP自动部署GIT代码,php部署git
最近在使用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 seemed like a simple enough process. BitBucket has a service that 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 this...
<code class="language-php" data-lang="php"><span class="cp"><?php <span class="nb">define<span class="p">(<span class="s1">'PRIVATE_KEY'<span class="p">, <span class="s1">'XXXXXXXXXXXXXXXXxxx'<span class="p">); <span class="k">if <span class="p">(<span class="nv">$_SERVER<span class="p">[<span class="s1">'REQUEST_METHOD'<span class="p">] <span class="o">=== <span class="s1">'POST' <span class="o">&& <span class="nv">$_REQUEST<span class="p">[<span class="s1">'thing'<span class="p">] <span class="o">=== <span class="nx">PRIVATE_KEY<span class="p">) <span class="p">{ <span class="k">echo <span class="nb">shell_exec<span class="p">(<span class="s2">"git pull"<span class="p">); <span class="p">} </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>
Didn'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 trying to get something like this set up.
(Missed) Considerations
the binary (git
in this case)
The user that is attempting to execute git pull
is the apache user (www
in our case). This user did not happen to have git
in their path.
This took a while to track down because the exec()
family of functions simply fail silently 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.
After I realized this I logged in and found the full path of the git binary with which git
, which is /full/path/to/bin/git
.
<code class="language-php" data-lang="php"><span class="cp"><?php <span class="o">... <span class="k">echo <span class="nb">shell_exec<span class="p">(<span class="s2">"/full/path/to/bin/git pull 2>&1"<span class="p">); <span class="o">... </span></span></span></span></span></span></span></span></code>
Now it was reporting the next issue...
permissions
<code class="language-text" data-lang="text">error: cannot open .git/FETCH_HEAD: Permission denied </code>
The apache user also needs read and write access to the entire repository.
<code class="language-text" data-lang="text">chown -R ssh_user:www repository/ </code>
It's also a good idea to make sure any files/directories inherit this ownership if being created by others by setting the group sticky bit.
<code class="language-text" data-lang="text">chmod -R g+s repository/ </code>
"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's known_hosts
file
<code class="language-text" data-lang="text">sudo -u www git pull </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 down the path of attempting to use the GIT_SSH
environment 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 ssh uses to determine the safety of a given key. It requires some specific permissions regarding the user that is attempting to use the key.
An easier way I discovered was to give the apache user a home directory (via /etc/passwd
) and a .ssh
directory and then run the ssh-keygen
command as the apache user (www
)
<code class="language-text" data-lang="text">sudo -u www ssh-keygen -t rsa </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.
<code class="language-php" data-lang="php"><span class="cp"><span class="nb"><span class="p"><span class="s1"><span class="p"><span class="s1"><span class="p"><span class="k"><span class="p"><span class="nv"><span class="p"><span class="s1"><span class="p"><span class="o"><span class="s1"><span class="o"><span class="nv"><span class="p"><span class="s1"><span class="p"><span class="o"><span class="nx"><span class="p"><span class="p"><span class="k"><span class="nb"><span class="p"><span class="s2"><span class="p"><span class="p"> </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>

ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

AI Hentai Generator
AIヘンタイを無料で生成します。

人気の記事

ホットツール

メモ帳++7.3.1
使いやすく無料のコードエディター

SublimeText3 中国語版
中国語版、とても使いやすい

ゼンドスタジオ 13.0.1
強力な PHP 統合開発環境

ドリームウィーバー CS6
ビジュアル Web 開発ツール

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)

ホットトピック

静的結合(静的::) PHPで後期静的結合(LSB)を実装し、クラスを定義するのではなく、静的コンテキストで呼び出しクラスを参照できるようにします。 1)解析プロセスは実行時に実行されます。2)継承関係のコールクラスを検索します。3)パフォーマンスオーバーヘッドをもたらす可能性があります。

JWTは、JSONに基づくオープン標準であり、主にアイデンティティ認証と情報交換のために、当事者間で情報を安全に送信するために使用されます。 1。JWTは、ヘッダー、ペイロード、署名の3つの部分で構成されています。 2。JWTの実用的な原則には、JWTの生成、JWTの検証、ペイロードの解析という3つのステップが含まれます。 3. PHPでの認証にJWTを使用する場合、JWTを生成および検証でき、ユーザーの役割と許可情報を高度な使用に含めることができます。 4.一般的なエラーには、署名検証障害、トークンの有効期限、およびペイロードが大きくなります。デバッグスキルには、デバッグツールの使用とロギングが含まれます。 5.パフォーマンスの最適化とベストプラクティスには、適切な署名アルゴリズムの使用、有効期間を合理的に設定することが含まれます。

PHPの魔法の方法は何ですか? PHPの魔法の方法には次のものが含まれます。1。\ _ \ _コンストラクト、オブジェクトの初期化に使用されます。 2。\ _ \ _リソースのクリーンアップに使用される破壊。 3。\ _ \ _呼び出し、存在しないメソッド呼び出しを処理します。 4。\ _ \ _ get、dynamic属性アクセスを実装します。 5。\ _ \ _セット、動的属性設定を実装します。これらの方法は、特定の状況で自動的に呼び出され、コードの柔軟性と効率を向上させます。

大企業または有名なオープンソースプロジェクトによって開発されたGOのどのライブラリが開発されていますか? GOでプログラミングするとき、開発者はしばしばいくつかの一般的なニーズに遭遇します...

GiteEpages静的Webサイトの展開が失敗しました:404エラーのトラブルシューティングと解像度Giteeを使用する

Python:Hourglassグラフィック図面と入力検証この記事では、Python NoviceがHourglass Graphic Drawingプログラムで遭遇する可変定義の問題を解決します。コード...

Beegoormフレームワークでは、モデルに関連付けられているデータベースを指定する方法は?多くのBEEGOプロジェクトでは、複数のデータベースを同時に操作する必要があります。 Beegoを使用する場合...
