[英]Deploy the bitbucket code directly on production server
我正在使用bitbucket
存儲我們的項目。 現在的問題是該項目非常大,其中有more then 20k files
。 我的服務器具有FTP connection
因此我無法在服務器上復制並粘貼完整的項目代碼。 所以我想直接將我的代碼從bitbucket部署到生產服務器。
我可以連接到bitbucket服務器並進行git pull
但是我真的很想找到一些使之automate
東西。
我正在尋找一個簡單快速的解決方案,但我找不到任何適用於bitbucket的東西,所有解決方案似乎都是為github設計的...
有任何service
或tool
可以自動化該過程嗎?
有一些方法可以做到這一點,我傾向於遵循並適應這種Turotial
通過使用BitBucket Webhooks,這需要注意一些事項
1)git security愚蠢,通常創建回購的用戶是唯一會影響回購的用戶,因此您必須確保apache用戶或組(對於Ubuntu Server www-data:www-data)具有使用git repo的能力(通過使用,我的意思是它需要能夠拉出/簽出/提交)
2)您的Web服務器必須處於聯機狀態,自動部署才能工作
3)您的網站掛鈎接收文件將位於您的網站上,請確保它安全,您可以使用.htpasswd安裝程序進行此操作。 如果您在設置Webhook時在BitBucket中使用.htpasswd設置,則可以在URL EG http://<username>:<passwd>@yourdomain.com/path/to/deployment.php
但是您的webhook php文件應如下所示:
<?php
$repo_dir = '/home/<username>/<repo-name>.git';
$web_root_dir = '/home/<username>/www';
// Full path to git binary is required if git is not in your PHP user's path. Otherwise just use 'git'.
$git_bin_path = 'git';
$update = false;
// Parse data from Bitbucket hook payload
$payload = json_decode($_POST['payload']);
if (empty($payload->commits)){
// When merging and pushing to bitbucket, the commits array will be empty.
// In this case there is no way to know what branch was pushed to, so we will do an update.
$update = true;
} else {
foreach ($payload->commits as $commit) {
$branch = $commit->branch;
if ($branch === 'production' || isset($commit->branches) && in_array('production', $commit->branches)) {
$update = true;
break;
}
}
}
if ($update) {
// Do a git checkout to the web root
exec('cd ' . $repo_dir . ' && ' . $git_bin_path . ' fetch');
exec('cd ' . $repo_dir . ' && GIT_WORK_TREE=' . $web_root_dir . ' ' . $git_bin_path . ' checkout -f');
// Log the deployment
$commit_hash = shell_exec('cd ' . $repo_dir . ' && ' . $git_bin_path . ' rev-parse --short HEAD');
file_put_contents('deploy.log', date('m/d/Y h:i:s a') . " Deployed branch: " . $branch . " Commit: " . $commit_hash . "\n", FILE_APPEND);
}
?>
確保它在公共html目錄中,並且不是由Larvel加載的,它應該是原始訪問的php文件。
Laravel forge非常適合將代碼從git部署到生產環境。 它還處理服務器配置和服務器設置。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.