lib/App/Dapper/Deploy.pm

filter: Statementstmt 22.2% 34 / 153 Branchbran 0.0% 0 / 86 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 61.1% 11 / 18 Podpod 85.7% 6 / 7 totaltotal 17.7% 51 / 287 CC 51 SCAR 52.8
CC
51
Cov
13%
github_pages
462.0
s3
156.0
run
132.0
CRAP 196.5
« lib/App/Dapper/Agents.pm ↑ Summary lib/App/Dapper.pm »
1 package App::Dapper::Deploy;
2  
3 =head1 NAME
4  
5 App::Dapper::Deploy - Publish a site to Amazon S3, GitHub Pages, or anywhere.
6  
7 =head1 DESCRIPTION
8  
9 C<dapper deploy> builds the site, checks it, and publishes the output
10 directory. Where it goes is set under C<deploy> in C<_config.yml>:
11  
12 deploy:
13 s3:
14 bucket: example.com
15 region: us-east-1 # optional
16 profile: personal # optional: an AWS CLI profile
17 cloudfront: E1A2B3C4D5E6F7 # optional: a distribution to refresh
18 cache_control: max-age=300 # optional
19 github_pages:
20 repo: git@github.com:me/site.git # default: this repository's origin
21 branch: gh-pages # the default
22 cname: www.example.com # optional
23 command: rsync -av --delete _output/ me@example.com:/var/www/site/
24  
25 With one target, C<dapper deploy> uses it; with several, name one:
26 C<dapper deploy s3>. Credentials are never stored in C<_config.yml>: S3 uses
27 the AWS CLI's own configuration (C<aws configure>), and GitHub Pages uses
28 git's.
29  
30 =over 4
31  
32 =item B<s3>
33  
34 Runs C<aws s3 sync --delete>, which uploads only files that changed and
35 removes files that are gone, then (with C<cloudfront>) asks CloudFront to
36 refresh its copies.
37  
38 =item B<github_pages>
39  
40 Commits the output to a branch (adding C<.nojekyll>, and C<CNAME> if set)
41 and pushes it. The branch keeps its history.
42  
43 =item B<command>
44  
45 Runs a command of your own. C<DAPPER_OUTPUT> holds the output directory.
46  
47 =back
48  
49 The site is not deployed if C<dapper check> finds errors, unless
50 C<--force> is given. C<--dry-run> shows what would happen without changing
51 anything. C<--no-build> deploys the output directory as it is.
52  
53 =cut
54  
55 190 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
56 190 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
57 190 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
58 190 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
59  
60 190 ▶use File::Temp qw/ tempdir /;
Subroutine BEGIN: called CC=1 SCAR=0.0
61 190 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
62 190 ▶use File::Spec::Functions qw/ catfile /;
Subroutine BEGIN: called CC=1 SCAR=0.0
63 190 ▶use File::Path ();
Subroutine BEGIN: called CC=1 SCAR=0.0
64 190 ▶use Cwd qw/ abs_path /;
Subroutine BEGIN: called CC=1 SCAR=0.0
65  
66 190 ▶use App::Dapper::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
67 190 ▶use App::Dapper::Check;
Subroutine BEGIN: called CC=1 SCAR=0.0
68  
69 190 my @TARGETS = qw/ s3 github_pages command /;
70  
71 =head2 run
72  
73 Takes a Dapper object, the target to deploy to (or C<undef> to pick the only
74 one), and options (C<dry_run>, C<force>, C<no_build>). Builds, checks and
75 deploys the site.
76  
77 =cut
78  
79 0 ▶sub run {
Subroutine run: not called CC=11 SCAR=48.8
Pod documented
80 0 my ($dapper, $target, %options) = @_;
81  
82 0 $dapper->read_project();
83 0 my $settings = $dapper->{site}->{deploy};
84 0 ▶ die "error: there is nowhere to deploy to; add deploy settings to $dapper->{config} (see \"Deploying with dapper deploy\" in the README)\n"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $settings eq "HASH" and %$settings 0 0 0
Condition0%0/3
ref $settings eq "HASH" and %$settings
!ll&&!rl&&r
000
MC/DC0%0/2
ref $settings eq "HASH" and %$settings
ref $settings eq "HASH" %$settings
Truth table0%0/3
A and B
A= ref $settings eq "HASH"B= %$settings
ABresult
0X0
100
111
85 unless ref $settings eq 'HASH' and %$settings;
86  
87 0 my @configured = grep { exists $settings->{$_} } @TARGETS;
88 0 my @unknown = grep { my $t = $_; !grep { $_ eq $t } @TARGETS } sort keys %$settings;
89 0 ▶ die "error: unknown deploy target \"$unknown[0]\" in $dapper->{config}; use " . join(', ', @TARGETS) . "\n" if @unknown;
Branch: 1 branch
BranchTrueFalseTotal
if @unknown 0 0 0
90  
91 0 ▶ if (not defined $target) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $target) 0 0 0
92 0 ▶ die "error: $dapper->{config} has several deploy targets (" . join(', ', @configured)
Branch: 1 branch
BranchTrueFalseTotal
if @configured > 1 0 0 0
93 . "); say which: dapper deploy <target>\n" if @configured > 1;
94 0 $target = $configured[0];
95 }
96 0 ▶ die "error: there are no \"$target\" deploy settings in $dapper->{config}\n" unless exists $settings->{$target};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $settings->{$target} 0 0 0
97  
98 # Drafts and pages dated in the future are never published
99 0 local $dapper->{drafts} = 0;
100  
101 0 ▶ if (not $options{no_build}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($options{"no_build"}) 0 0 0
102 0 $dapper->build();
103  
104 0 my $report = App::Dapper::Check::run($dapper);
105 0 ▶ if (not $report->{ok}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($report->{"ok"}) 0 0 0
106 0 my $errors = join('', map { " $_->{file}: $_->{message}\n" } @{$report->{errors}});
107 0 die "error: dapper check found " . scalar(@{$report->{errors}}) . " error(s), so the site was not deployed"
108 0 ▶ . " (use --force to deploy anyway):\n$errors" unless $options{force};
Branch: 1 branch
BranchTrueFalseTotal
unless $options{"force"} 0 0 0
109 0 warn "warning: deploying even though dapper check found errors:\n$errors";
110 }
111 }
112  
113 0 ▶ die "error: the output directory \"$dapper->{output}\" doesn't exist; build the site first\n" unless -d $dapper->{output};
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dapper->{"output"} 0 0 0
114  
115 0 my %deploy = (s3 => \&s3, github_pages => \&github_pages, command => \&command);
116 0 return $deploy{$target}->($dapper, $settings->{$target}, \%options);
117 }
118  
119 =head2 find_command
120  
121 Takes a command name and returns true if it is on the C<PATH>.
122  
123 =cut
124  
125 0 ▶sub find_command {
Subroutine find_command: not called CC=1 SCAR=6.9
Pod documented
126 0 my ($name) = @_;
127 0 return scalar grep { -x catfile($_, $name) } File::Spec->path();
128 }
129  
130 =head2 run_command
131  
132 Takes a Dapper object and a command (as a list), prints it, and runs it.
133 Dies if it fails.
134  
135 =cut
136  
137 0 ▶sub run_command {
Subroutine run_command: not called CC=3 SCAR=24.8
Pod documented
138 0 my ($dapper, @command) = @_;
139  
140 0 ▶ $dapper->_log('+ ' . join(' ', map { /\s/ ? "'$_'" : $_ } @command));
Branch: 1 branch
BranchTrueFalseTotal
/\s/ ? : 0 0 0
141 0 ▶ system(@command) == 0
Branch: 1 branch
BranchTrueFalseTotal
system(@command) == 0 or die 'error: ' . join(" ", @command[0, 1]) . ' failed (' . exit_status($?) . ")\n" 0 0 0
142 or die "error: " . join(' ', @command[0, 1]) . " failed (" . exit_status($?) . ")\n";
143 }
144  
145 # exit_status($?) - Why a command failed, in words.
146 0 ▶sub exit_status {
Subroutine exit_status: not called CC=3 SCAR=24.8
Pod undocumented
147 0 my ($status) = @_;
148 0 ▶ return "it could not be run: $!" if $status == -1;
Branch: 1 branch
BranchTrueFalseTotal
if $status == -1 0 0 0
149 0 ▶ return "it was stopped by signal " . ($status & 127) if $status & 127;
Branch: 1 branch
BranchTrueFalseTotal
if $status & 127 0 0 0
150 0 return "exit status " . ($status >> 8);
151 }
152  
153 =head2 s3
154  
155 Deploys to Amazon S3 with the AWS CLI.
156  
157 =cut
158  
159 0 ▶sub s3 {
Subroutine s3: not called CC=12 SCAR=50.5
Pod documented
160 0 my ($dapper, $settings, $options) = @_;
161  
162 die "error: deploy.s3 in $dapper->{config} needs a bucket\n"
163 0 ▶ unless ref $settings eq 'HASH' and defined $settings->{bucket} and length $settings->{bucket};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $settings eq "HASH" and defined $settings->{"bucket"} and length $settings->{"bucket"} 0 0 0
Condition0%0/6
ref $settings eq "HASH" and defined $settings->{"bucket"} and length $settings->{"bucket"}
!ll&&!rl&&r
000
ref $settings eq "HASH" and defined $settings->{"bucket"}
!ll&&!rl&&r
000
MC/DC0%0/3
ref $settings eq "HASH" and defined $settings->{"bucket"} and length $settings->{"bucket"}
ref $settings eq "HASH" defined $settings->{"bucket"} length $settings->{"bucket"}
Truth table0%0/4
A and B and C
A= ref $settings eq "HASH"B= defined $settings->{"bucket"}C= length $settings->{"bucket"}
ABCresult
0XX0
10X0
1100
1111
164 0 ▶ die "error: deploying to S3 needs the AWS CLI (https://aws.amazon.com/cli/); install it and run \"aws configure\"\n"
Branch: 1 branch
BranchTrueFalseTotal
unless find_command("aws") 0 0 0
165 unless find_command('aws');
166  
167 0 my @common;
168 0 ▶ push @common, '--region', $settings->{region} if defined $settings->{region};
Branch: 1 branch
BranchTrueFalseTotal
if defined $settings->{"region"} 0 0 0
169 0 ▶ push @common, '--profile', $settings->{profile} if defined $settings->{profile};
Branch: 1 branch
BranchTrueFalseTotal
if defined $settings->{"profile"} 0 0 0
170  
171 0 (my $bucket = $settings->{bucket}) =~ s{^s3://}{};
172 0 my @sync = ('aws', 's3', 'sync', $dapper->{output}, "s3://$bucket", '--delete', '--exclude', '.dapper-reload', @common);
173 0 ▶ push @sync, '--cache-control', $settings->{cache_control} if defined $settings->{cache_control};
Branch: 1 branch
BranchTrueFalseTotal
if defined $settings->{"cache_control"} 0 0 0
174 0 ▶ push @sync, '--dryrun' if $options->{dry_run};
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"dry_run"} 0 0 0
175 0 run_command($dapper, @sync);
176  
177 0 ▶ if (defined $settings->{cloudfront}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $settings->{"cloudfront"}) 0 0 0
178 my @invalidate = ('aws', 'cloudfront', 'create-invalidation', '--distribution-id', $settings->{cloudfront},
179 0 '--paths', '/*', @common);
180 0 ▶ if ($options->{dry_run}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{'dry_run'}) { } 0 0 0
181 0 $dapper->_log('Would run: ' . join(' ', @invalidate));
182 }
183 else {
184 0 run_command($dapper, @invalidate);
185 }
186 }
187  
188 0 ▶ $dapper->_log($options->{dry_run} ? "Dry run: nothing was uploaded." : "Deployed to s3://$bucket");
Branch: 1 branch
BranchTrueFalseTotal
$options->{'dry_run'} ? : 0 0 0
189 0 return 1;
190 }
191  
192 =head2 github_pages
193  
194 Deploys to a GitHub Pages branch with git.
195  
196 =cut
197  
198 0 ▶sub github_pages {
Subroutine github_pages: not called CC=21 SCAR=61.4
Pod documented
199 0 my ($dapper, $settings, $options) = @_;
200  
201 0 ▶ $settings = {} unless ref $settings eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $settings eq "HASH" 0 0 0
202 0 ▶ die "error: deploying to GitHub Pages needs git\n" unless find_command('git');
Branch: 1 branch
BranchTrueFalseTotal
unless find_command("git") 0 0 0
203  
204 0 my $repo = $settings->{repo};
205 0 ▶ if (not defined $repo) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $repo) 0 0 0
206 0 $repo = `git remote get-url origin 2>/dev/null`;
207 0 chomp $repo;
208 0 ▶ die "error: deploy.github_pages in $dapper->{config} needs a repo (or run dapper in a git repository with an origin)\n"
Branch: 1 branch
BranchTrueFalseTotal
unless length $repo 0 0 0
209 unless length $repo;
210 }
211 0 ▶ my $branch = defined $settings->{branch} ? $settings->{branch} : 'gh-pages';
Branch: 1 branch
BranchTrueFalseTotal
defined $settings->{'branch'} ? : 0 0 0
212 0 my $output = abs_path($dapper->{output});
213  
214 0 my $work = tempdir(CLEANUP => 1) . '/site';
215  
216 # Start from the branch as it is, to keep its history; or a new branch
217 0 ▶ open(my $heads, '-|', 'git', 'ls-remote', '--heads', '--', $repo, "refs/heads/$branch")
Branch: 1 branch
BranchTrueFalseTotal
open my $heads, "-|", "git", "ls-remote", "--heads", "--", $repo, "refs/heads/$branch" or die "error: could not run git: $!\n" 0 0 0
218 or die "error: could not run git: $!\n";
219 0 my $exists = do { local $/; <$heads> };
220 0 ▶ close $heads or die "error: could not reach $repo\n";
Branch: 1 branch
BranchTrueFalseTotal
close $heads or die "error: could not reach $repo\n" 0 0 0
221  
222 # A shallow clone of a local path makes git warn, so clone those in full
223 0 ▶ my @depth = -d $repo ? () : ('--depth', '1');
Branch: 1 branch
BranchTrueFalseTotal
-d $repo ? : 0 0 0
224 0 ▶ if (length $exists) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $exists) { } 0 0 0
225 0 run_command($dapper, 'git', 'clone', '--quiet', @depth, '--branch', $branch, '--', $repo, $work);
226 }
227 else {
228 0 $dapper->_log("Starting a new $branch branch");
229 0 run_command($dapper, 'git', 'init', '--quiet', $work);
230 0 run_command($dapper, 'git', '-C', $work, 'checkout', '--quiet', '--orphan', $branch);
231 0 run_command($dapper, 'git', '-C', $work, 'remote', 'add', 'origin', $repo);
232 }
233  
234 # Replace the branch's files with the output
235 0 for my $entry (App::Dapper::Utils::read_dir($work)) {
236 0 ▶ next if $entry eq '.git';
Branch: 1 branch
BranchTrueFalseTotal
if $entry eq ".git" 0 0 0
237 0 my $path = "$work/$entry";
238 0 ▶ if (-d $path) { File::Path::remove_tree($path) } else { unlink $path }
Branch: 1 branch
BranchTrueFalseTotal
if (-d $path) { } 0 0 0
239 }
240 0 for my $entry (App::Dapper::Utils::read_dir($output)) {
241 0 ▶ next if $entry eq '.dapper-reload';
Branch: 1 branch
BranchTrueFalseTotal
if $entry eq ".dapper-reload" 0 0 0
242 0 App::Dapper::Utils::copy_recursive("$output/$entry", "$work/$entry");
243 }
244 0 App::Dapper::Utils::write_file("$work/.nojekyll", '');
245 0 ▶ App::Dapper::Utils::write_file("$work/CNAME", "$settings->{cname}\n") if defined $settings->{cname};
Branch: 1 branch
BranchTrueFalseTotal
if defined $settings->{"cname"} 0 0 0
246  
247 0 ▶ system('git', '-C', $work, 'add', '--all') == 0 or die "error: git add failed\n";
Branch: 1 branch
BranchTrueFalseTotal
system("git", "-C", $work, "add", "--all") == 0 or die "error: git add failed\n" 0 0 0
248 0 my $changes = `git -C '$work' status --porcelain`;
249 0 ▶ if (not length $changes) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $changes) 0 0 0
250 0 $dapper->_log("Nothing changed since the last deploy to $branch.");
251 0 return 1;
252 }
253  
254 0 my $count = () = $changes =~ /\n/g;
255 0 ▶ if ($options->{dry_run}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"dry_run"}) 0 0 0
256 0 $dapper->_log("Dry run: would commit $count changed file(s) to $branch of $repo:");
257 0 $dapper->_log($changes);
258 0 return 1;
259 }
260  
261 0 ▶ my $name = defined $dapper->{site}->{name} ? $dapper->{site}->{name} : 'the site';
Branch: 1 branch
BranchTrueFalseTotal
defined $dapper->{'site'}{'name'} ? : 0 0 0
262 # Commit as Dapper where git has no name and email set (in a container, say)
263 0 my $email = `git -C '$work' config user.email`;
264 0 ▶ my @identity = length $email ? () : ('-c', 'user.name=Dapper', '-c', 'user.email=dapper@localhost');
Branch: 1 branch
BranchTrueFalseTotal
length $email ? : 0 0 0
265 0 run_command($dapper, 'git', '-C', $work, @identity, 'commit', '--quiet', '-m', "Deploy $name ($count file(s) changed)");
266 0 run_command($dapper, 'git', '-C', $work, 'push', '--quiet', 'origin', "HEAD:$branch");
267  
268 0 $dapper->_log("Deployed $count changed file(s) to $branch of $repo");
269 0 return 1;
270 }
271  
272 =head2 command
273  
274 Deploys by running a command from C<_config.yml>.
275  
276 =cut
277  
278 0 ▶sub command {
Subroutine command: not called CC=6 SCAR=37.4
Pod documented
279 0 my ($dapper, $command, $options) = @_;
280  
281 0 ▶ die "error: deploy.command in $dapper->{config} must be a command to run\n"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $command and not ref $command and length $command 0 0 0
Condition0%0/6
defined $command and not ref $command and length $command
!ll&&!rl&&r
000
defined $command and not ref $command
!ll&&!rl&&r
000
MC/DC0%0/3
defined $command and not ref $command and length $command
defined $command not ref $command length $command
Truth table0%0/4
A and not(B) and C
A= defined $commandB= not ref $commandC= length $command
ABCresult
0XX0
10X0
1100
1111
282 unless defined $command and !ref $command and length $command;
283  
284 0 ▶ if ($options->{dry_run}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"dry_run"}) 0 0 0
285 0 $dapper->_log("Dry run: would run: $command");
286 0 return 1;
287 }
288  
289 0 local $ENV{DAPPER_OUTPUT} = $dapper->{output};
290 0 $dapper->_log("+ $command");
291 0 ▶ system($command) == 0 or die "error: the deploy command failed (" . exit_status($?) . ")\n";
Branch: 1 branch
BranchTrueFalseTotal
system($command) == 0 or die 'error: the deploy command failed (' . exit_status($?) . ")\n" 0 0 0
292 0 $dapper->_log('Deployed.');
293 0 return 1;
294 }
295  
296 190 1;
« lib/App/Dapper/Agents.pm ↑ Summary lib/App/Dapper.pm »