Skip to content

Instantly share code, notes, and snippets.

@ponych
Created May 2, 2018 12:56
Show Gist options
  • Save ponych/ac22d13c8f6acc85c8c4a35bc95462b4 to your computer and use it in GitHub Desktop.
Save ponych/ac22d13c8f6acc85c8c4a35bc95462b4 to your computer and use it in GitHub Desktop.
jenkins - php - shells
#!/usr/bin/env php
<?php
/**
* ./lumen.test.php branch_name git_commit
*/
$hostsTest = json_decode($_SERVER['HOST_TEST'] ,1); // [{"host":"app35","port":58422,"path":"/home/plus/lumen2"}]
$hostsProd = json_decode($_SERVER['HOST_PROD'] ,1);
$branch = $_SERVER['GIT_BRANCH'];
$git_commit = $_SERVER['GIT_COMMIT'];
echo "branch:\t".$branch."\n";
echo "git_commit:\t".$git_commit."\n";
if ('deploy' == $branch) {
echo "master branch\n";
$packName = substr($git_commit, 0 ,7);
packNsend($packName ,$hostsProd);
} elseif (strpos($branch, 'feature') !== false) {
$parts1 = explode('/', $branch);
if (count($parts1) < 2) {
'wrong place';
}
$parts2 = explode('_', $parts1[1]);
$digi = $parts2[0];
$packName = $parts1[0].'_'.$digi;
echo "packName:".$packName."\n";
packNsend($packName ,$hostsTest);
echo "deploy done\n";
}
function packNsend($packName , $hosts) {
echo "\$hosts\n";
print_r($hosts);
echo "tar -cjf {$packName}.tar.bz2 -C src ."."\n";
exec("tar -cjf {$packName}.tar.bz2 -C src .");
foreach ($hosts as $env) {
$target = $env['host'];
$port = $env['port'];
$path = rtrim($env['path'] ,'/');
echo "scp -P {$port} {$packName}.tar.bz2 plus@{$target}:{$path}/"."\n";
exec("scp -P {$port} {$packName}.tar.bz2 plus@{$target}:{$path}/");
echo "ssh -p {$port} plus@{$target} \"cd {$path};tar -jxf {$packName}.tar.bz2 --xform='s|^|{$packName}/|Si';rm -f {$packName}.tar.bz2 \""."n";
exec("ssh -p {$port} plus@{$target} \"cd {$path};tar -jxf {$packName}.tar.bz2 --xform='s|^|{$packName}/|Si';rm -f {$packName}.tar.bz2 \"");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment