-
Notifications
You must be signed in to change notification settings - Fork 95
Expand file tree
/
Copy pathdeploy
More file actions
executable file
·175 lines (139 loc) · 5.18 KB
/
Copy pathdeploy
File metadata and controls
executable file
·175 lines (139 loc) · 5.18 KB
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
#!/usr/bin/env perl
use v5.14;
use Cwd qw( chdir getcwd );
use File::Basename qw( basename dirname );
use File::Path qw( make_path );
use File::pushd qw( pushd );
use File::Spec::Functions qw( catfile );
use FindBin qw( $Bin );
=head1 NAME
deploy - populate the Perl.com staging repo with the latest content
=head1 SYNOPSIS
# in the tpf/perldotcom repo top level directory
% bin/deploy
=head1 DESCRIPTION
There are two repos for Perl.com. The public one, I<tpf/perldotcom>,
is where all the work happens. The second one,
I<tpf/perl.com-staging>, is the one that Perl NOC serves as the
website.
To convert the public repo to the public site, you run this program.
This essentially does a bunch of sanity checks about files and directories
then lets Hugo build a site into the I<tpf/perl.com-staging> directory.
=head2 Run it through C<make deploy>, not directly
This program is only the Hugo build-and-push step. The full deploy also
has to regenerate the (git-ignored) JSON metadata and contributors list
first, and move the C<deployed> tag afterward. All of that lives in the
C<deploy> target in the F<Makefile>. Running C<bin/deploy> on its own
skips it and ships stale metadata.
To enforce that, this program refuses to run unless the environment
variable C<PERLDOTCOM_DEPLOY_VIA_MAKE> is set, which the C<make deploy>
recipe does for you. If you genuinely need to bypass the wrapper, set it
by hand:
PERLDOTCOM_DEPLOY_VIA_MAKE=1 bin/deploy
=cut
unless ( $ENV{PERLDOTCOM_DEPLOY_VIA_MAKE} ) {
die <<'END_REFUSAL';
Refusing to run bin/deploy directly.
Run `make deploy` instead: it regenerates the JSON metadata and
contributors list first, then runs this script, then updates the
`deployed` tag. Running bin/deploy on its own ships stale metadata.
To bypass on purpose: PERLDOTCOM_DEPLOY_VIA_MAKE=1 bin/deploy
END_REFUSAL
}
my $perldotcom_repo = dirname($Bin);
my $perldotcom_parent = dirname($perldotcom_repo);
# Find the staging repo. We expect it to be at the same level
my $staging_github = 'git@github.com:tpf/perl.com-staging.git';
my $staging_repo = basename($staging_github) =~ s/\.git\z//r;
my $staging_clone = catfile( $perldotcom_parent, $staging_repo );
my $dest_dir = catfile( $staging_clone, 'perl.com' );
# If we don't have the staging clone, get it (or not, if you say no)
if ( !-d $staging_clone ) {
say "Didn't find <$staging_clone>";
local $SIG{ALRM} = sub { say "\nPrompt timeout. Exiting"; exit 1 };
alarm(10);
print "Clone <$staging_github> now? [yN] ";
chomp( my $answer = <STDIN> );
alarm(0);
unless ( $answer =~ /\A y/xi ) {
say "Not cloning. Exiting.";
exit 1;
}
git( 'clone', '--depth', 1, $staging_github, $staging_clone );
}
else {
my $dir = pushd($staging_clone);
git( 'pull', '--rebase', 'origin', 'master' );
}
my @command = (
'docker', 'run', '--rm',
'-v', "$perldotcom_repo:/src",
'-v', "$staging_clone/perl.com:/src/public",
'hugomods/hugo:debian-reg-non-root-0.160.1',
'build',
);
# start in the tpf/perldotcom directory so the external commands
# start from the right place
die "Could not change to <$perldotcom_repo>: $!\n"
unless ( chdir $perldotcom_repo );
say "Now in ", getcwd();
# Check that we are up-to-date
if ( git_is_behind_master() ) {
die "This clone is behind GitHub! `git pull` and try again\n";
}
elsif ( git_is_ahead_master() ) {
die "This clone is ahead of GitHub! `git pull` and try again\n";
alarm(10);
print "Push now? [yN] ";
chomp( my $answer = <STDIN> );
alarm(0);
if ( $answer =~ /\A y/xi ) {
git('push');
}
else {
# maybe we die here because we want GitHub to have everything
# to make the public site
say "Not pushing. Remember to do that later.";
}
}
else {
say "This clone is up-to-date and pushed";
}
# ensure the destination directory is there
make_path $dest_dir;
die "Destination dir <$dest_dir> does not exist!\n" unless -d $dest_dir;
chomp( my $perldotcom_commit = `git rev-parse HEAD` );
# still in the tpf/perldotcom directory, and make the website in
# the staging directory
say 'About to run ' . join q{ }, @command;
die "Hugo build failed with status: $?\n" if system(@command);
# now work inside the staging dir
die "Could not change to <$staging_clone>: $!\n"
unless ( chdir $staging_clone );
say "Now in ", getcwd();
# move the 404 template because Hugo can't generate it there
my $error_dir = 'perl.com/error';
make_path $error_dir, { error => \my $errors };
if ( @$errors > 0 ) {
my $error = $errors->[0]{$error_dir};
die "Destination dir <$error_dir> is not there: $error\n";
}
rename 'perl.com/404.html' => catfile( $error_dir, '404.html' );
if ( $ENV{'NO_GIT'} ) {
warn "NO_GIT is set, so stopping here\n";
exit 0;
}
git('status');
git( 'add', '.' );
git( 'commit', '-m', "generate site (tpf/perldotcom $perldotcom_commit)" );
git('push');
sub git { system 'git', @_ }
sub git_status_master {
my ($line) = map { /## master\Q.../ } `git status -sb`;
}
sub git_is_behind_master {
git_status_master() =~ m/\[behind\s\d\]/;
}
sub git_is_ahead_master {
git_status_master() =~ m/\[ahead\s\d\]/;
}