-
Notifications
You must be signed in to change notification settings - Fork 24
/
try
executable file
·124 lines (94 loc) · 4.11 KB
/
try
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
#!/usr/bin/php
<?php
require_once __DIR__.'/vendor/autoload.php';
# URL to your jenkins server (without http)
$jenkins_server = 'localhost:8080';
# Path to the jenkins cli.jar - download it from http://your.jenkins.instance/cli
$jenkins_cli_jar = '/usr/bin/jenkins-cli.jar';
# Jenkins job name
$default_jenkins_job = 'try';
$default_jenkins_job_prefix = $default_jenkins_job;
# Retrieve user
$user = getenv("USER");
# Working copy path (path to your local git repository)
$default_wc_path = '.';
#################################################################################################
# Define the usage spec
# See TryLib/Util/PHPOptions/README.md for more details
$usage_spec = "
try [options...] [subjob] [subjob] ...
--
h,help Show help
n,diff-only Create diff, but do not send to Hudson
v,verbose Verbose (show shell commands as they're run)
p,patch= Path to patch file to use instead of generating a diff
U,lines-of-context= Generate a diff with n lines of context (like git-diff's -U option)
s,staged Use staged changes only to generate the diff
w,safelist= Generate the patch for only the safelisted files
b,branch= Remote branch to diff and try against [main]
c,show-results Show final try job results
P,show-progress Print subtasks progressively as they complete
extra-param= Extra parameters to send to jenkins - format key=value
C,callback= Callback string to execute at the end of the try run.
Use \${status} and \${url} as placeholders for the try build status and url
Example: --callback 'echo \"**Try status : [\${status}](\${url})**\"'
jenkinsjob= Master Job Name in Jenkins [$default_jenkins_job]
jenkinsjobprefix= Subjobs prefix [$default_jenkins_job_prefix]
jenkinsserver= URL to the jenkins server [$jenkins_server]
wcpath= Working Copy Path [$default_wc_path]
";
# Parse options
$parser = new TryLib\Util\PHPOptions\Options($usage_spec);
list($options, $flags, $extra) = $parser->parse($argv);
# [Jenkins MasterProject only] - Get the subjobs list
$subjobs = array_slice($extra, 1);
$cmd_runner = new TryLib\CommandRunner($options->verbose);
$repo_manager = new TryLib\RepoManager\Git($options->wcpath, $cmd_runner);
$remote_branch = $options->branch;
$repo_manager->setRemoteBranch($remote_branch);
# [Jenkins Freestyle project]
#$jenkins_runner = new TryLib\JenkinsRunner\FreeStyleProject(
# $options->jenkinsserver,
# $jenkins_cli_jar,
# $options->jenkinsjob,
# $cmd_runner
#);
# [Jenkins MasterProject only]
$jenkins_runner = new TryLib\JenkinsRunner\MasterProject(
$options->jenkinsserver,
$jenkins_cli_jar,
$options->jenkinsjob,
$cmd_runner,
$options->jenkinsjobprefix
);
$safelist = $options->safelist;
if (is_string($safelist)) {
$safelist = array($safelist);
}
$pre_checks = array(
new TryLib\Precheck\GitCopyBehind(array($remote_branch)),
new TryLib\Precheck\GitReportUntracked(),
);
$repo_manager->runPrechecks($pre_checks);
$patch = $options->patch;
if (is_null($patch)) {
$patch = $repo_manager->generateDiff($options->staged, $safelist);
}
if ($options->diff_only) {
print 'Not sending job to Jenkins (-n) diff is here:' . $patch . PHP_EOL;
exit(0);
}
$jenkins_runner->setPatch(realpath($patch));
$jenkins_runner->setSshKey( getenv('HOME') . '/.ssh/try_id_rsa');
$jenkins_runner->setParam('branch', $remote_branch);
$jenkins_runner->setParam('guid', $user . time());
$extra_params = TryLib\Util\OptionsUtil::parseExtraParameters($options->extra_param);
foreach($extra_params as $param) {
$jenkins_runner->setParam($param[0], $param[1]);
}
$jenkins_runner->addCallback($options->callback);
# [Jenkins MasterProject only] Set the subjobs -
$jenkins_runner->setSubJobs($subjobs);
$jenkins_runner->startJenkinsJob($options->show_results, $options->show_progress);
# [Jenkins MasterProject only] - exit with the appropriate exit code - used only when tracking-progress or showing results
#($jenkins_runner->try_status === 'FAILURE')? exit(1) : exit(0);