-
Notifications
You must be signed in to change notification settings - Fork 2
/
deploy.php
executable file
·179 lines (149 loc) · 5.38 KB
/
deploy.php
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php
namespace Deployer;
use Dotenv\Dotenv;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//// Dependencies
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
require 'recipe/common.php';
require realpath(__DIR__) . '/vendor/autoload.php';
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//// Tasks
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
$autoload = array_diff(
scandir(realpath(__DIR__) . '/config/tasks/', SCANDIR_SORT_ASCENDING),
['..', '.', '.DS_Store']
);
if (!empty($autoload)) {
array_map(function ($task) {
require_once realpath(__DIR__) . '/config/tasks/' . $task;
}, $autoload);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//// Functions
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
* Searches $file for $before and replaces it with $after
*
* @param string $file
* @param string $before
* @param string $after
* @return bool
*/
function searchreplaceinfile(string $file, string $before, string $after)
{
$seperator = "~";
$before = str_replace($seperator, "\\" . $seperator, $before);
$after = str_replace($seperator, "\\" . $seperator, $after);
$cmd = "sed -i '' 's" . $seperator . $before . $seperator . $after . $seperator . "g' \"$file\"";
$stage = get('stage', 'local');
if ($stage == "local") {
return runLocally($cmd);
} else {
return run($cmd);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//// Environments
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
$env_path = realpath(__DIR__) . "/config/";
$dotenv = Dotenv::createImmutable($env_path, ".env");
$dotenv->load();
$dotenv->required([
'WP_USER',
'WP_EMAIL',
'WP_SITENAME',
'WP_LOCALURL',
'REPOSITORY',
'LOCAL_DB_HOST',
'LOCAL_DB_NAME',
'LOCAL_DB_USER',
'LOCAL_DB_PASS',
])->notEmpty();
$hosts = [
"STAGING",
"PRODUCTION"
];
foreach ($hosts as $env) {
$stage = strtolower($env);
$host = null;
if (isset($_ENV[$env . "_HOST"])) {
$host = host($stage)
->hostname($_ENV[$env . "_HOST"])
->user($_ENV[$env . "_DEPLOY_USER"])
->stage($stage)
->forwardAgent(true)
->set('branch', $_ENV[$env . "_BRANCH"])
->set('stage_url', $_ENV[$env . "_STAGE_URL"])
->set('deploy_path', $_ENV[$env . "_DEPLOY_PATH"]);
}
}
// Include reference to localhost
localhost($_ENV["LOCAL_HOST"]);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//// Below be dragons - tread carefully!
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Define the project root
set('abspath', realpath(__DIR__));
// Define a list of files that should be shared between deployments
set('shared_files', [
'wp-config.php',
'.htaccess',
'robots.txt'
]);
// Allow interaction for Git clone
set('git_tty', true);
// Define a directory that is shared between deployments
set('shared_dirs', [
'content/uploads'
]);
// Define web user writeable directories
set('writable_dirs', [
'content/uploads',
'content/cache'
]);
// Use ACL to extend existing permissions
set('writable_mode', 'acl'); // chmod, chown, chgrp or acl.
// Set apache config options
set('http_user', 'www-data');
set('http_group', 'www-data');
// Every release should be datetime stamped
set('release_name', date('YmdHis'));
// register the repo
set('repository', $_ENV["REPOSITORY"]);
// register the local wp url
set('local_url', $_ENV["WP_LOCALURL"]);
// Try to use git cache where applicable
set('git_cache', true);
// Disable usage data
set('allow_anonymous_stats', false);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//// Master runbook
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
task('deploy', [
'deploy:info',
'deploy:lock',
'deploy:release',
'deploy:update_code',
'deploy:shared',
'deploy:writable',
'composer-install',
'deploy:clear_paths',
'deploy:symlink',
'deploy:unlock',
'cleanup',
'signoff',
'success'
])->desc('Deploy your project');
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//// Hide uncommon tasks from the CLI
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
task('deploy:clear_paths')->setPrivate();
task('deploy:copy_dirs')->setPrivate();
task('deploy:prepare')->setPrivate();
task('deploy:release')->setPrivate();
task('deploy:shared')->setPrivate();
task('deploy:symlink')->setPrivate();
task('deploy:update_code')->setPrivate();
task('deploy:vendors')->setPrivate();
task('deploy:writable')->setPrivate();
task('cleanup')->setPrivate();