-
Notifications
You must be signed in to change notification settings - Fork 0
/
deploy.pl
65 lines (59 loc) · 2.53 KB
/
deploy.pl
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
#!/usr/bin/perl -l
use Cwd;
use strict;
use warnings;
use File::Basename;
use File::Copy;
use File::Path qw(make_path);
require "config.pm";
our (@repos);
foreach my $repo (@repos) {
# go to git repo
chdir($repo->{repo_path});
my @branches = @{ $repo->{branches} };
foreach my $item ( @branches ) {
# checkout branch and pull
my $change_branch_command = 'git checkout ' . $item->{branch_name};
qx{$change_branch_command};
qx{git stash -u && git reset --hard HEAD};
my $pull = qx{git pull};
my @output = split m/\r?\n/, $pull;
# print $output[0];
if ($output[0] ne 'Already up-to-date.') {
my $git_fetch = 'git fetch --all && git reset --hard origin/' . $item->{branch_name};
qx{$git_fetch};
# if the repo is not up to date, get the changed files as an array
my $git_diff = 'git diff --name-only HEAD HEAD~1';
my $git_dif_head_output = qx{$git_diff};
my @diff_output = split m/\r?\n/, $git_dif_head_output;
foreach my $output_line (@diff_output) {
# remove white spaces
$output_line =~ s/\s+//g;
#get correct paths
my($filename, $directories, $suffix) = fileparse($output_line,qr"\..[^.]*$");
my $git_file_path = $directories . $filename . $suffix;
if (-e $git_file_path){
if (not grep $_ eq $git_file_path, @{ $item->{ignore_files} || [] }){
my $server_dir = $item->{copy_to_path} . $directories;
eval { make_path($server_dir) };
if ($@) {
print "Couldn't create $server_dir: $@";
}
my $filepath = $directories . $filename . $suffix;
# print "copying to $filepath . $server_dir";
copy($filepath, $server_dir) or die "Failed to copy $filepath: $!\n";
}
}
elsif ($item->{delete_files}==1) {
my $filepath = $filename . $suffix;
my $server_dir = $item->{copy_to_path} . $directories;
# print $server_dir . $filepath;
unlink $server_dir . $filepath or warn $!;
}
}
}
#else {
# print 'branch is up to date';
# }
}
}