-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatic.php
151 lines (128 loc) · 4.3 KB
/
static.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
<?php
namespace Deployer;
require_once 'recipe/common.php';
require_once __DIR__ . '/common.php';
desc('Prepares a new release');
task('deploy:prepare', [
'deploy:info',
'deploy:setup',
'deploy:lock',
'deploy:release',
'deploy:local_build',
'deploy:rsync',
'deploy:shared',
'deploy:writable',
]);
desc('Publishes the release');
task('deploy:publish', [
'deploy:symlink',
'deploy:unlock',
'deploy:cleanup',
'deploy:success',
]);
// Deployment tasks
desc('Deploys your project');
task('deploy', [
'deploy:prepare',
'deploy:publish',
]);
/**
* Run commands to build website locally
*
* Checks out Git repo, runs build commands
*
* Configuration
* build_root: root directory to store build files (default is ~/.deployer)
* build_folder: directory that contains built website files
* build_commands: array of build commands to run on build files
*/
desc('Build website locally');
task('deploy:local_build', function () {
$git = get('bin/git');
$target = get('target');
$targetWithDir = $target;
if (!empty(get('sub_directory'))) {
$targetWithDir .= ':{{sub_directory}}';
}
// Set project root directory for build
$buildPath = get('build_root', getenv('HOME') . '/.deployer');
$buildFolder = get('build_folder');
// @see https://deployer.org/docs/7.x/recipe/deploy/info
$repo = get('what');
$buildPath = $buildPath . '/' . $repo;
set('build_path', $buildPath);
// Create local build directory
if (!file_exists($buildPath)) {
writeln('Creating build directory');
mkdir($buildPath, recursive: true);
}
// Remove previous local build
$files = (int) runLocally(sprintf('ls %s | wc -l', $buildPath));
if ($files > 0) {
run(sprintf('rm -rf %s/*', $buildPath));
writeln('Removed previous build');
}
// Checkout build
writeln('Cloning repository (Branch: <info>{{branch}}</info>)');
// Clone the required branch to the local build directory
runLocally("$git archive $targetWithDir | tar -x -f - -C $buildPath 2>&1");
writeln('Clone complete');
// Save git revision
set('revision', runLocally("$git rev-list $target -1"));
// Change working path to build folder, run build commands, then switch back
// @todo in Deployer 8 switch this to working_path or within()
writeln('Run build commands...');
$lastWorkingPath = getenv('DEPLOYER_ROOT');
try {
putenv('DEPLOYER_ROOT=' . $buildPath);
invoke('local_build');
} finally {
putenv('DEPLOYER_ROOT=' . $lastWorkingPath);
}
writeln('Build complete.');
});
// Placeholder local_build task, override this in your deploy.php file
task('local_build', function() {
writeln('Override this task in your deploy.php file');
});
/**
* Upload website build files to remote server
*
* Configuration
* build_folder: directory that contains built website files (optional)
* rsync_folder: directory to rsync files to, relative to release_path (optional)
*/
desc('Sync website build files to remote');
task("deploy:rsync", function() {
// Ensure build path has trailing slash
$buildPath = rtrim(get('build_path'), '/') . '/' . trim(get('build_folder'), '/') . '/';
if (empty($buildPath) || !is_dir($buildPath)) {
error('Source folder cannot be determined via build_path or build_folder! Please add the folder where your website files are built to via set("build_folder", "path")');
}
// Destination path to rsync files to
$rsyncFolder = ltrim(get('rsync_folder'), '/');
if (!empty($rsyncFolder)) {
$rsyncFolder = '/' . $rsyncFolder;
set('webroot', $rsyncFolder);
} else {
// Save _build_summary.json to current directory if rsync_folder not set
set('webroot', './');
}
// Rsync
writeln("Rsync build files to server from $buildPath to {{release_path}}");
upload($buildPath, "{{release_path}}$rsyncFolder", ["progress_bar" => true]);
writeln('Rsync complete.');
// Save git revision in REVISION file
$rev = get('revision');
run("echo $rev > {{release_path}}/REVISION");
});
/**
* Prepend npm use to run commands via NVM
*
* @param string $command
* @return string
*/
function nvm(string $command): string
{
return sprintf("source ~/.nvm/nvm.sh && nvm use && %s", $command);
}