-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpublish
executable file
·104 lines (89 loc) · 2.77 KB
/
publish
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
#!/usr/bin/env perl
use strict;
use v5.16;
my @configs = (
{
type => "text/html; charset=utf-8",
include => [qw(*.html *.atom* atom.xml *.rss10)],
},
{
type => "application/atom+xml; charset=utf-8",
include => [qw(*.xml)],
exclude => [qw(browserconfig.xml sitemap.xml)],
},
{
type => "application/xml; charset=utf-8",
include => [qw(browserconfig.xml sitemap.xml)],
},
{
type => "text/plain; charset=utf-8",
include => [qw(*.txt *.text */README */MANIFEST */Changes */sociable-1.0 *.md *mmd *.pm *.pl *.sql .ldap *.diff *.patch *.pod *.gpg)],
},
{
type => "application/x-gtar",
include => [qw(*.tar.gz *.tgz *.tar.Z *.tar.bz2 *.tbz2 *.tar.lz *.tlz. *.tar.xz *.txz)],
},
{
type => "application/manifest+json",
include => [qw(*.webmanifest)],
},
{
type => "font/woff",
include => [qw(*.woff)],
},
{
type => "font/woff2",
include => [qw(*.woff2)],
},
{
type => "text/yaml; charset=utf-8",
include => [qw(*.yaml *.yml)],
},
);
############################################################################
sub run { system(@_) == 0 or die "system @_ failed: $?" }
sub echo { say join ' ', map { /[\s*]/ ? "'$_'" : $_ } @_ }
my ($dir, $bucket, $distid, $script) = @ARGV;
die "Usage: $0 SRC_DIR BUCKET CLOUDFRONT_DISTID\n"
unless $dir & $bucket && $distid;
# Make sure we have a media type and include for each.
die "Missing type from one or more configs\n"
if grep { !$_->{type} || !$_->{include} } @configs;
my $do = \&run;
if ($script) {
$do = \&echo;
print "set -ex\n\n";
}
# Run each of the configs.
$do->( sync($_) ) for @configs;
# Sync everything else, excluding everything we just included, and letting
# the AWS client guess the media type.
$do->( sync({ exclude => [map { @{ $_->{include} } } @configs] }) );
# Invalidate the CloudFront cache.
$do->(qw(aws configure set preview.cloudfront true));
$do->(qw(aws cloudfront create-invalidation --distribution-id), $distid, qw(--paths /*));
############################################################################
sub sync {
my $p = shift;
# Core options.
my @cmd = qw(
aws s3 sync
--acl public-read
--sse
--metadata-directive=REPLACE
--delete
);
# Media type.
if (my $t = $p->{type}) {
push @cmd => '--content-type', $t;
}
# Files to include.
if (my $i = $p->{include}) {
push @cmd => qw(--exclude *);
push @cmd => '--include', $_ for @{ $i };
}
# Files to exclude.
push @cmd => '--exclude', $_ for @{ $p->{exclude} || [] };
# Command to run.
return @cmd, $dir, "s3://$bucket";
}