Skip to content

Commit 2ee7baa

Browse files
committed
Merge pull request #41 from tamagokun/better_deploy_update
[Review] Improving deploy:update closes #39
2 parents 9ca70cb + 59342a3 commit 2ee7baa

File tree

7 files changed

+108
-42
lines changed

7 files changed

+108
-42
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
vendor
33
deploy
44
tests/test_release
5+
.idea

lib/Pomander/Cli.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
namespace Pomander;
33

4-
use phake\Application;
4+
use \phake\Application;
55

66
class Cli
77
{

lib/Pomander/Environment.php

+8-7
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ public function set($options)
3232
public function setup()
3333
{
3434
if($this->name == "development") $this->releases = false;
35-
if($this->branch) $this->revision = $this->branch;
3635
if ($this->releases === false) {
3736
$this->current_dir = $this->deploy_to;
3837
$this->releases_dir = $this->deploy_to;
@@ -58,9 +57,9 @@ public function __call($name, $arguments)
5857

5958
public function __get($prop)
6059
{
61-
if(array_key_exists($prop, $this->config)) return $this->config[$prop];
62-
63-
return null;
60+
if(!array_key_exists($prop, $this->config)) return null;
61+
$value = $this->config[$prop];
62+
return is_callable($value)? $value() : $value;
6463
}
6564

6665
public function __isset($prop) { return isset($this->config[$prop]); }
@@ -151,7 +150,8 @@ private function defaults()
151150
"url"=>"",
152151
"user"=>"",
153152
"repository"=>"",
154-
"revision"=>"origin/master",
153+
"revision"=>"",
154+
"branch"=>"master",
155155
"remote_cache"=>true,
156156
"releases"=>false,
157157
"keep_releases"=>false,
@@ -170,7 +170,8 @@ private function defaults()
170170
"rsync_cmd"=>"rsync",
171171
"rsync_flags"=>"-avuzPO --quiet",
172172
"db_backup_flags"=>"--lock-tables=FALSE --skip-add-drop-table | sed -e 's|INSERT INTO|REPLACE INTO|' -e 's|CREATE TABLE|CREATE TABLE IF NOT EXISTS|'",
173-
"db_swap_url"=>true
173+
"db_swap_url"=>true,
174+
"composer"=>false
174175
);
175176

176177
return $defaults;
@@ -199,7 +200,7 @@ private function init_scm_adapter()
199200

200201
private function inject_multi_role_after($role,$task_name)
201202
{
202-
info("injecting after $task_name");
203+
info("injecting after $task_name", '');
203204
after($task_name,function ($app) use ($task_name,$role) {
204205
if ( $app->env->next_role($role) ) {
205206
$app->reset();

lib/Pomander/Scm/Git.php

+68-12
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,90 @@
11
<?php
2+
23
namespace Pomander\Scm;
34

4-
class Git extends \Pomander\Scm
5+
use Pomander\Scm;
6+
7+
/**
8+
* Class Git
9+
* @package Pomander\Scm
10+
*/
11+
class Git extends Scm
512
{
13+
/**
14+
* @param $location
15+
* @return string
16+
*/
617
public function create($location)
718
{
8-
return "git clone {$this->repository} {$location}";
19+
return "git clone -q {$this->repository} {$location}";
920
}
1021

22+
/**
23+
* @return string
24+
*/
1125
public function update()
1226
{
13-
if (!empty($this->app->env->branch)) {
14-
$cmd = "git reset --hard --quiet && git checkout {$this->app->env->branch} --quiet && git pull --quiet";
15-
} elseif (!empty($this->app->env->revision)) {
16-
$cmd = "git reset --hard {$this->app->env->revision} --quiet";
17-
} else {
18-
$cmd = 'git reset --hard HEAD --quiet && git pull --quiet';
27+
$cmd = array();
28+
29+
// Fetch remote
30+
$remote = isset($this->app->env->remote)? $this->app->env->remote : "origin";
31+
$cmd[] = "git fetch -q {$remote}";
32+
$cmd[] = "git fetch --tags -q {$remote}";
33+
34+
// Search revision
35+
if(!empty($this->app->env->revision)) {
36+
$commit = $this->app->env->revision;
1937
}
38+
else {
39+
if(!empty($this->app["branch"])) {
40+
$commit = $this->get_commit_sha($this->app["branch"]);
41+
} elseif(!empty($this->app->env->branch)) {
42+
$commit = $this->get_commit_sha($this->app->env->branch);
43+
} else {
44+
$commit = 'HEAD';
45+
}
46+
}
47+
48+
// Reset HARD commit
49+
$cmd[] = "git reset -q --hard {$commit}";
2050

2151
if ($this->app->env->submodule !== false) {
22-
$cmd .= ' && git submodule update --init --recursive --quiet';
52+
$cmd[] = 'git submodule update --init --recursive --quiet';
2353
}
2454

25-
$cmd .= ' && git log --date=relative --format=format:"%C(bold blue)(%ar)%C(reset) // %an \'%s\' %C(bold green)(%h)%C(reset)" | head -1';
55+
$cmd[] = 'git clean -q -d -x -f';
56+
$cmd[] = 'git log --date=relative --format=format:"%C(bold blue)(%ar)%C(reset) %an \'%s\' %C(bold green)(%h)%C(reset)" | head -1';
2657

27-
return $cmd;
58+
return implode(' && ', $cmd);
2859
}
2960

61+
/**
62+
* @return string
63+
*/
3064
public function revision()
3165
{
32-
return "echo -e `git log --pretty=format:'%H' -n 1`";
66+
return "git rev-parse HEAD";
67+
}
68+
69+
/**
70+
* @param $ref
71+
* @return string|void
72+
*/
73+
public function get_commit_sha($ref)
74+
{
75+
// if specifying a remote ref, just grab the branch name
76+
if(strpos($ref, "/") !== false) {
77+
$ref = explode("/", $ref);
78+
$ref = end($ref);
79+
}
80+
81+
list($status, $commit) = run_local("git ls-remote {$this->app->env->repository} {$ref}");
82+
if($status > 0 || !$commit) abort("update", "failed to retrieve commit for {$ref}.");
83+
84+
$commit = array_shift($commit);
85+
$commit = substr($commit, 0, strpos($commit, "\t"));
86+
87+
return $commit;
3388
}
89+
3490
}

lib/tasks/composer.php

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
desc("Install dependencies with Composer");
66
task('install', function ($app) {
77
if($app->env->composer === false) return;
8+
info("composer", "install");
89
run(array(
910
"cd {$app->env->release_dir}",
1011
"([ -e 'composer.json' ] && which composer &>/dev/null)",
@@ -14,6 +15,7 @@
1415

1516
task('update', function ($app) {
1617
if($app->env->composer === false) return;
18+
info("composer", "update");
1719
run(array(
1820
"cd {$app->env->release_dir}",
1921
"([ -e 'composer.json' ] && which composer &>/dev/null)",

lib/tasks/deploy.php

+13-8
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
$cmd[] = $app->env->scm->create($app->env->deploy_to);
1616
} else {
1717
$deployed = run("if test -d {$app->env->current_dir}; then echo \"exists\"; fi", true);
18-
if(count($deployed)) return abort("setup", "application has already been setup.");
18+
if(count($deployed)) abort("setup", "application has already been setup.");
1919
$cmd[] = "mkdir -p {$app->env->releases_dir} {$app->env->shared_dir}";
2020
if($app->env->remote_cache === true) $cmd[] = $app->env->scm->create($app->env->cache_dir);
2121
}
@@ -27,20 +27,22 @@
2727
info("deploy","updating code");
2828
$cmd = array();
2929
if ($app->env->releases === false) {
30+
$frozen = run("if test -d {$app->env->deploy_to}; then echo \"ok\"; fi", true);
31+
if(empty($frozen)) abort("deploy", "deploy_to folder not found. you should run deploy:setup or deploy:cold first.");
3032
$cmd[] = "cd {$app->env->deploy_to}";
3133
$cmd[] = "{$app->env->scm->revision()} > REVISION";
3234
$cmd[] = $app->env->scm->update();
3335
} else {
3436
$app->env->release_dir = $app->env->releases_dir.'/'.$app->env->new_release();
3537
if ($app->env->remote_cache === true) {
3638
$frozen = run("if test -d {$app->env->cache_dir}; then echo \"ok\"; fi", true);
37-
if(empty($frozen)) return abort("deploy", "remote_cache folder not found. you should run deploy:setup or deploy:cold first.");
39+
if(empty($frozen)) abort("deploy", "remote_cache folder not found. you should run deploy:setup or deploy:cold first.");
3840
$cmd[] = "cd {$app->env->cache_dir}";
3941
$cmd[] = $app->env->scm->update();
4042
$cmd[] = "cp -R {$app->env->cache_dir} {$app->env->release_dir}";
4143
} else {
4244
$frozen = run("if test -d {$app->env->releases_dir}; then echo \"ok\"; fi", true);
43-
if(empty($frozen)) return abort("deploy", "releases folder not found. you should run deploy:setup or deploy:cold first.");
45+
if(empty($frozen)) abort("deploy", "releases folder not found. you should run deploy:setup or deploy:cold first.");
4446
$cmd[] = $app->env->scm->create($app->env->release_dir);
4547
$cmd[] = "cd {$app->env->release_dir}";
4648
$cmd[] = $app->env->scm->update();
@@ -80,10 +82,10 @@
8082
});
8183

8284
desc("First time deployment.");
83-
task('cold','deploy:setup','deploy:update','composer:update','deploy:finalize');
85+
task('cold','deploy:setup','deploy:update','composer:install','deploy:finalize');
8486

8587
});
86-
task('deploy','deploy:update','composer:update','deploy:finalize');
88+
task('deploy','deploy:update','composer:install','deploy:finalize');
8789

8890
//rollback
8991
desc("Rollback to the previous release");
@@ -93,11 +95,11 @@
9395

9496
if ($app->env->releases) {
9597
$releases = run("ls -1t {$app->env->releases_dir}", true);
96-
if(count($releases) < 2) return abort("rollback", "no releases to roll back to.");
98+
if(count($releases) < 2) abort("rollback", "no releases to roll back to.");
9799

98100
if ($app->env->release_dir == $app->env->current_dir) {
99101
$count = isset($app['releases'])? $app['releases'] : 1;
100-
if(count($releases) < $count + 1) return abort("rollback", "can't rollback that far.");
102+
if(count($releases) < $count + 1) abort("rollback", "can't rollback that far.");
101103
if($count > 1) info("rollback", "rolling back to {$releases[$count]}.");
102104
info("rollback", "pointing application to previous release.");
103105
$cmd[] = "ln -nfs {$app->env->releases_dir}/{$releases[$count]} {$app->env->current_dir}";
@@ -110,8 +112,11 @@
110112
}
111113
}
112114
} else {
115+
$frozen = run("if test -f {$app->env->release_dir}/REVISION; then echo \"ok\"; fi", true);
116+
if(empty($frozen)) abort("rollback", "no releases to roll back to.");
117+
113118
$revision = run("cat {$app->env->release_dir}/REVISION", true);
114-
if(!count($revision)) return abort("rollback", "no releases to roll back to.");
119+
if(!count($revision)) abort("rollback", "no releases to roll back to.");
115120

116121
$app->env->revision = $revision[0];
117122
$cmd[] = $app->scm->update();

tests/DeployTest.php

+15-14
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ public function testSetup()
2626
$app->invoke("norelease");
2727
$app->invoke("deploy:setup");
2828

29-
$this->assertFileExists($app->env->deploy_to."/.git");
3029
ob_end_clean();
30+
$this->assertFileExists($app->env->deploy_to."/.git");
3131
}
3232

3333
public function testUpdateCode()
@@ -51,9 +51,9 @@ public function testUpdateCode()
5151
$app->invoke("deploy:update");
5252
$new_sha = shell_exec("cd {$app->env->release_dir} && {$app->env->scm->revision()}");
5353

54+
ob_end_clean();
5455
$this->assertFileExists($app->env->release_dir);
5556
$this->assertTrue($sha == $new_sha);
56-
ob_end_clean();
5757
}
5858

5959
public function testDeployBranch()
@@ -72,20 +72,24 @@ public function testDeployBranch()
7272

7373
$app->env->branch = "gh-pages";
7474
$app->invoke("deploy:update");
75+
sleep(1);
7576
$app->reset();
76-
$current_branch = shell_exec("cd {$app->env->release_dir} && git rev-parse --abbrev-ref HEAD");
77+
$expected_sha = $app->env->scm->get_commit_sha($app->env->branch);
78+
$current_sha = shell_exec("cd {$app->env->release_dir} && {$app->env->scm->revision()}");
7779

80+
ob_end_clean();
7881
$this->assertFileExists($app->env->release_dir);
79-
$this->assertSame($app->env->branch, trim($current_branch));
82+
$this->assertSame($expected_sha, trim($current_sha));
8083

84+
ob_start();
8185
$app->env->branch = "";
8286
$app->env->revision = "0.3.5";
8387
$app->invoke("deploy:update");
8488

85-
$sha = shell_exec("cd {$app->env->release_dir} && git log --pretty=format:'%H' -n 1");
86-
$this->assertSame($sha, "46bbf9cfe5cfa3656f1246870ff98656e27761e7");
89+
$sha = shell_exec("cd {$app->env->release_dir} && {$app->env->scm->revision()}");
8790

8891
ob_end_clean();
92+
$this->assertSame(trim($sha), "46bbf9cfe5cfa3656f1246870ff98656e27761e7");
8993
}
9094

9195
public function testFinalize()
@@ -105,10 +109,10 @@ public function testFinalize()
105109
$app->invoke("deploy:update");
106110
$app->invoke("deploy:finalize");
107111

112+
ob_end_clean();
108113
$this->assertFileExists($app->env->current_dir);
109114
$this->assertTrue(is_link($app->env->current_dir));
110115
$this->assertTrue(readlink($app->env->current_dir) == $app->env->release_dir);
111-
ob_end_clean();
112116
}
113117

114118
public function testCleanUp()
@@ -134,9 +138,9 @@ public function testCleanUp()
134138
$app->invoke("deploy:update");
135139
$app->invoke("deploy:cleanup");
136140

141+
ob_end_clean();
137142
$this->assertFileExists($app->env->release_dir);
138143
$this->assertCount(2, glob($app->env->releases_dir."/*"));
139-
ob_end_clean();
140144
}
141145

142146
public function testCold()
@@ -154,6 +158,7 @@ public function testCold()
154158

155159
$app->invoke("deploy:cold");
156160

161+
ob_end_clean();
157162
$this->assertFileExists($app->env->release_dir);
158163
$this->assertFileExists($app->env->releases_dir);
159164
$this->assertFileExists($app->env->shared_dir);
@@ -163,8 +168,6 @@ public function testCold()
163168
$this->assertFileExists($app->env->current_dir);
164169
$this->assertTrue(is_link($app->env->current_dir));
165170
$this->assertTrue(readlink($app->env->current_dir) == $app->env->release_dir);
166-
167-
ob_end_clean();
168171
}
169172

170173
public function testDefault()
@@ -183,11 +186,10 @@ public function testDefault()
183186

184187
$app->invoke("deploy");
185188

189+
ob_end_clean();
186190
$this->assertFileExists($app->env->current_dir);
187191
$this->assertTrue(is_link($app->env->current_dir));
188192
$this->assertTrue(readlink($app->env->current_dir) == $app->env->release_dir);
189-
190-
ob_end_clean();
191193
}
192194

193195
public function testRollback()
@@ -216,12 +218,11 @@ public function testRollback()
216218

217219
$release = readlink($app->env->current_dir);
218220

221+
ob_end_clean();
219222
$this->assertFileExists($app->env->current_dir);
220223
$this->assertTrue(is_link($app->env->current_dir));
221224
$this->assertFileExists($release);
222225
$this->assertFalse(readlink($app->env->current_dir) == $app->env->release_dir);
223-
224-
ob_end_clean();
225226
}
226227

227228
// private

0 commit comments

Comments
 (0)