-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy
More file actions
119 lines (112 loc) · 3.17 KB
/
Copy pathdeploy
File metadata and controls
119 lines (112 loc) · 3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/php
<?php
$help = "usage: deploy [branch|tag] project\nif branch|tag is not specified " .
"it will use master as a fallback\n";
$production_dirs = array(
'assemblyexpress' => 'assemblyexpress',
'pentalogix' => 'pentalogix-www2',
'order-manager' => 'quotetools',
'order-manager-pl' => 'order-manager-pl',
'stencils' => 'stencils-www',
'pcbunlimited' => 'pcbunlimited'
);
/**
* @return string Current branch name if it is local branch or empty
* string if it is a (no branch) state
*/
function get_current_branch($git_repo) {
$output = `cd $git_repo; git symbolic-ref HEAD`;
if(strpos($output, '/') > -1) {
$parts = explode('/', trim($output));
return $parts[2];
} else {
return "";
}
}
function branch_exists($git_repo, $branch_name) {
$cmd = "cd $git_repo; git branch;";
$output = `$cmd`;
$branches = explode("\n", trim($output));
foreach ($branches as $key => $branch) {
$branch = trim($branch);
if(strpos($branch, "*") > -1) {
$current = explode(' ', $branch, 2);
$branch = trim($current[1]);
}
$branches[$key] = $branch;
if ($branch === $branch_name) {
return true;
}
}
return false;
}
function tag_exists($git_repo, $tag_name) {
$cmd = "cd $git_repo; git tag;";
$output = `$cmd`;
$tags = explode("\n", trim($output));
foreach ($tags as $key => $tag) {
$tag = trim($tag);
$tags[$key] = $tag;
if ($tag === $tag_name) {
return true;
}
}
return false;
}
if ($argc > 1) {
if ($argc > 2) {
$branch = $argv[1];
$project = $argv[2];
} else {
$project = $argv[1];
$branch = 'master';
}
$log = "/tmp/deployment-$project.log";
$website_dir = "/home/devsites/$project";
$git_repo = "/var/home/webdev/git-repos/$project.git";
$source = "/home/devsites/$project/";
$destination = "coho1:/home/agustin/websites/$production_dir";
if (!is_dir($website_dir)) {
fwrite(STDOUT, "error: $website_dir doesn't exist or is not a directory\n");
fwrite(STDOUT, $help);
exit(-1);
}
if (!is_dir($git_repo)) {
fwrite(STDOUT, "error: Git repo $git_repo not found\n");
exit(-1);
}
if (!branch_exists($git_repo, $branch) and !tag_exists($git_repo, $branch)) {
fwrite(STDOUT, "error: the branch or tag your trying to use does not " .
"exist in the servers repository\n");
exit(-1);
}
if (!array_key_exists($project, $production_dirs)) {
fwrite(STDOUT, "error: wrong configuration. Production dir not found\n");
exit(-1);
}
$log_file = fopen($log, "a+");
fwrite($log_file, '[' . date("F j, Y, g:i a") . "]\n");
$production_dir = $production_dirs[$project];
//$production_dir = 'xtreme';
$current_branch = get_current_branch($git_repo);
if($current_branch != $branch) {
$switch = "sw-git-branch $project $branch";
$output = `$switch`;
fwrite(STDOUT, "switching to branch/tag $branch\n");
fwrite(STDOUT, $output);
fwrite($log_file, $output);
}
fwrite(STDOUT, "Starting deployment to production server...\n");
fwrite(STDOUT, "file syncing started...\n");
$rsync = "rsync -arvuz " .
"--exclude-from=/var/home/webdev/git-repos/rsync_exclude.txt" .
" $source" .
" $destination";
$output = `$rsync`;
fwrite(STDOUT, $output);
fwrite($log_file, $output);
fclose($log_file);
} else {
fwrite(STDOUT, $help);
}
?>