ホームページ  >  記事  >  バックエンド開発  >  PHP を使用して GIT コードを自動的にデプロイする

PHP を使用して GIT コードを自動的にデプロイする

WBOY
WBOYオリジナル
2016-07-29 09:15:40725ブラウズ

最近、Coding のコード ホスティングを使用しており、WebHook の自動デプロイメントを設定しました。これは、主に Linux の権限制御をまだ理解していないためです。しかし、幸いにもそれを理解できたので共有させてください。参考までに、原文は英語ですので、なんとか理解できます

原文リンク: http://jondavidjohn.com/ git-pull-from-a-php-script-not-so-simple/

私は、新しいコミットがプッシュアップされたときに開発サーバー上でプルを開始するリポジトリ (BitBucket でホストされている) をセットアップするつもりでした。

それBitBucket には受信後フックとして POST リクエストを発行するサービスがあるため、ランダム化されたトークンをチェックしてから git pull を開始するように設定しました。 code>. 次のようになります...<code>git pull. Looking something like this...

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

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</span> 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 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>

Now it was reporting the next issue...

permissions

<code 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 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 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 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 data-lang="text">sudo -u www ssh-keygen -t rsa
</code>
予想していたほど単純にはなりませんでした... ここで文書化する際に考慮していない考慮事項がいくつかありました。このようなセットアップを行う際の障害を回避するのに役立つことを願っています。

(欠落) 考慮事項

バイナリ (この場合は git

)

実行しようとしているユーザー

git pull

は Apache ユーザー (この場合は www

) です。このユーザーはたまたまパスに

git🎜 を持っていませんでした。 .🎜🎜🎜🎜🎜🎜🎜関数の🎜 exec()🎜 ファミリは STDERR ではなく STDOUT のみを報告するため、単に通知せずに失敗するため、追跡するのに時間がかかりました。 STDERR コマンドの最後に🎜 2->&1🎜 を追加することで、STDOUT にルーティングできます。🎜🎜🎜🎜🎜🎜これに気づいた後、ログインして、 git binary with🎜 that git、つまり🎜 /full/path/to/bin/git.🎜🎜🎜🎜
<code data-lang="php"><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></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>
🎜🎜今、次の問題を報告していました。 ..🎜🎜権限🎜🎜rrreee🎜🎜Apacheユーザーには、リポジトリ全体への読み取りおよび書き込みアクセスも必要です。🎜🎜rrreee🎜🎜また、他のユーザーによって作成されたファイル/ディレクトリがこの所有権を確実に継承するようにすることもお勧めします。グループ スティッキー ビットを設定しています。🎜🎜rrreee🎜🎜「ホスト キーの検証に失敗しました」🎜🎜 次に、Apache ユーザーで最初の git pull を実行して、リモートが Apache ユーザーの🎜 known_hosts🎜 file🎜🎜🎜🎜rrreee🎜🎜 ssh キー🎜🎜 Apache ユーザーによって実行されるこのコマンドによって生じるもう 1 つの考慮事項は、リモート リポジトリとの通信に使用される ssh キーです。🎜🎜まず、パスをたどりました。 🎜 <code>GIT_SSH code>🎜 環境変数を使用して🎜 <code>ssh -i🎜 オプションを設定し、ssh ユーザーで生成した特定の ssh キーを使用するように指示しようとしました。私はこれを機能させることができませんでした。おそらく、ssh が特定のキーの安全性を判断するために使用するルールが多数あるためです。キーを使用しようとしているユーザーに関して特定の権限が必要です。🎜🎜🎜🎜🎜🎜私が発見したより簡単な方法は、Apache ユーザーにホーム ディレクトリ (🎜 /etc/passwd 経由) と🎜 .ssh🎜 ディレクトリを与えてから、🎜 を実行することでした。 ssh-keygen🎜 コマンド🎜 as🎜 Apache ユーザー (www)🎜🎜🎜🎜🎜🎜🎜🎜🎜rrreee🎜🎜 これにより、キーが作成され、キーを使用して予想される場所に配置されます。適切な権限が適用されました。🎜🎜次に、そのキーを BitBucket リポジトリの読み取り専用キーとして追加しました。すべてが期待どおりに機能しました。🎜rrreee 🎜 上記では、PHP を使用して GIT コードを自動的にデプロイする方法を、内容の側面も含めて紹介しています。PHP チュートリアルに興味のある友人に役立つことを願っています。 🎜 🎜 🎜
声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。