2

BitBucket git リポジトリに PHP プロジェクトがあります。

私は小さな修正のために「開発」というブランチで働いているか、一時的な機能ブランチで働いています。デプロイの準備ができたら、それらのブランチを「マスター」にマージします。

ライブ サイトへのデプロイを同じくらい簡単にしたい (マスターへのマージと BitBucket へのプッシュ)。

しかし、セキュリティ上の懸念が増すため、サーバーがリポジトリにアクセスできないようにしたくありません。 セキュリティに関心がある場合は、リポジトリをできるだけ少ない場所に配置する必要があります。サーバーが侵害された場合、それは十分に悪い状況ですが、攻撃者が私の完全なリポジトリにアクセスできるようになると、さらに悪いことになります. この人は同意。

したがって、 https://stackoverflow.com/a/163769/470749で説明されているようgit archive masterに、のようなものを使用したいと思います。

git archive master「マスター」のプッシュを検出し、実行して最新のコードを(ただし、リポジトリとしてではなく)圧縮されたzipファイルにエクスポートし、それを(SCPおよび/またはRsync経由で)送信するフックを設定するにはどうすればよいですか?リモートサーバーは、それを新しいディレクトリに解凍し、(おそらくシンボリックリンクを変更して) サーバーをその新しいディレクトリにポイントしますか?

おまけの質問: 簡単な緊急ロールバックを有効にするにはどうすればよいですか? (以前のコミットにすぐに戻したい場合があると思います。)

4

1 に答える 1

1

最終的に完成したスクリプトに満足しています。

deploy.sh:

##This executable file will export your latest code from master (via "git archive") and will upload it 
##to the remote server and then call a script on the server to handle from there.
##----------------------------------------------------------------------------------------------------

source dev-ops/archive_and_upload.sh

##On the remote server, run a script to archive the existing production site files and then deploy the uploaded package.
ssh -i ~/.ssh/id_rsa myUserName@vientiane.dreamhost.com <<'ENDSSH'

set -e

cd /home/myUserName/myProjectName/latest

##Unzip the zip file, then delete it.
echo "Unzipping the package.zip..."
unzip -o package.zip && rm package.zip  

cd /home/myUserName/myProjectName/

nowTime=$(date -u +"%Y-%m-%d__%H:%M:%S")
echo "The archive will have this timestamp: " $nowTime

##Copy the "latest" folder to a dated "packages" subfolder.
cp -R latest/ packages/$nowTime 
echo "Copied the existing site to an archive."

##Install Laravel dependencies.
echo "Running Composer so that the remote server downloads and installs dependencies..."
cd packages/$nowTime 
php -d memory_limit=256M ~/bin/composer.phar install   

##Delete the "live" symlink and immediately create a new "live" symlink to the most recent subfolder within "packages".
echo "Updating the symlinks..."
cd /home/myUserName/myProjectName/
echo `pwd`
rm previous
mv live previous && ln -s packages/$nowTime live && ls -lah  

##Clear out the "latest" folder in preparation for next time.
echo "Deleting the contents of the 'latest' folder in preparation for next time..."
rm -rf latest/* && ls latest   
ENDSSH

echo "FINISHED DEPLOYING!"

archive_and_upload.sh:

##This executable file will export your latest code from master (via "git archive") and will upload it 
##to the remote server.
##----------------------------------------------------------------------------------------------------

##Clear out the contents of the previous export package.
rm -rf dev-ops/package/*   

##Export the "master" branch of this git repo. (The result is not a repo but is just code.)
git archive --format zip --output dev-ops/package/package.zip master  

##Send zip file to remote server.
scp -i ~/.ssh/id_rsa dev-ops/package/package.zip myUserName@vientiane.dreamhost.com:/home/myUserName/myProjectName/latest/package.zip 

revert_to_previous_package.sh:

ssh -i ~/.ssh/id_rsa myUserName@vientiane.dreamhost.com <<'ENDSSH'

set -e

cd /home/myUserName/myProjectName/

mv live rollingBack && mv previous live && mv rollingBack previous && ls -lah

ENDSSH

echo "ROLLED BACK!"

ご覧のとおり、Dreamhost サーバーが "live" というフォルダーからサービスを提供するように設定しました。これは、コード パッケージがアップロードされたときのタイムスタンプとして名前が付けられたサブフォルダーへの単なるシンボリック リンクです。また、ロールバックを容易にする「previous」と呼ばれる別のシンボリックリンクもあります (デプロイ後に問題に気づき、元に戻したい場合に備えて)。

于 2013-09-22T06:25:16.180 に答える