-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploy.php
302 lines (217 loc) · 6.1 KB
/
deploy.php
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
<?php
/**
* Functions.
*/
function escape_sequence( $code ) {
return "\e[" . $code . 'm';
}
function format_command( $value ) {
return escape_sequence( '36' ) . $value . escape_sequence( '0' );
}
function format_error( $value ) {
return escape_sequence( '31' ) . escape_sequence( '1' ) . 'Error:' . escape_sequence( '0' ) . ' ' . $value;
}
function run_command( $command, $expected_result_code = 0 ) {
echo format_command( $command ), PHP_EOL;
passthru( $command, $result_code );
if ( null !== $expected_result_code && $expected_result_code !== $result_code ) {
exit( $result_code );
}
return $result_code;
}
function start_group( $name ) {
echo '::group::', $name, PHP_EOL;
}
function end_group() {
echo '::endgroup::', PHP_EOL;
}
/**
* Get input.
*
* @link https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions#inputs
* @link https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepswith
* @link /~https://github.com/actions/checkout/blob/cd7d8d697e10461458bc61a30d094dc601a8b017/dist/index.js#L2699-L2717
* @param string $name
* @return string|array|false
*/
function get_input( $name ) {
$env_name = 'INPUT_' . strtoupper( $name );
return getenv( $env_name );
}
function get_required_input( $name ) {
$value = get_input( $name );
if ( false === $value || '' === $value ) {
echo format_error( escape_sequence( '90' ) . 'Input required and not supplied:' . escape_sequence( '0' ) . ' ' . $name );
exit( 1 );
}
return $value;
}
/**
* Default environment variables.
*
* @link https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/variables#default-environment-variables
*/
$repository = getenv( 'GITHUB_REPOSITORY' );
/**
* Setup.
*/
$username = get_required_input( 'username' );
$password = get_required_input( 'password' );
$slug = get_required_input( 'slug' );
$tag = get_required_input( 'tag' );
$version = ltrim( $tag, 'v' );
$svn_url = "https://plugins.svn.wordpress.org/$slug";
/**
* Filename.
*
* Notation: “{name}.{version}”.
*
* @link https://developer.wordpress.org/cli/commands/dist-archive/
*/
$filename = "$slug.$version.zip";
/**
* Files.
*/
$work_dir = tempnam( sys_get_temp_dir(), '' );
unlink( $work_dir );
mkdir( $work_dir );
$archives_dir = $work_dir . '/archives';
$plugins_dir = $work_dir . '/plugins';
$svn_dir = $work_dir . '/svn';
mkdir( $archives_dir );
mkdir( $plugins_dir );
mkdir( $svn_dir );
$plugin_dir = $plugins_dir . '/' . $slug;
$readme_file = $plugin_dir . '/readme.txt';
/**
* Start.
*/
start_group( 'ℹ️ Release to WordPress.org' );
echo '• ', escape_sequence( '1' ), 'Subversion URL:', escape_sequence( '0' ), ' ', $svn_url, PHP_EOL;
echo '• ', escape_sequence( '1' ), 'Subversion username:', escape_sequence( '0' ), ' ', $username, PHP_EOL;
echo '• ', escape_sequence( '1' ), 'Subversion password:', escape_sequence( '0' ), ' ', $password, PHP_EOL;
end_group();
/**
* GitHub release.
*/
start_group( '🐙 Check GitHub release' );
run_command( "gh release view $tag --repo $repository" );
end_group();
/**
* Download release.
*
* @link https://cli.github.com/manual/gh_release_download
*/
start_group( '📥 Download plugin' );
run_command( "gh release download $tag --pattern '$filename' --dir $archives_dir --repo $repository" );
end_group();
/**
* Unzip.
*/
start_group( '📦 Unzip plugin' );
run_command(
sprintf(
'unzip %s -d %s',
escapeshellarg( $archives_dir . '/' . $filename ),
escapeshellarg( $plugins_dir )
)
);
end_group();
/**
* Parse stable tag.
*/
$readme_content = file_get_contents( $readme_file );
$pattern = "/Stable tag: (.*)/";
$stable_tag = '';
if ( 1 === preg_match( $pattern, $readme_content, $matches ) ) {
$stable_tag = $matches[1];
}
if ( $stable_tag !== $version ) {
echo 'Stable tag in readme.txt is not equal to release version.';
exit( 1 );
}
/**
* Check tag existence.
*/
start_group( '🔎 Check tag existence' );
$svn_url_tag = "$svn_url/tags/$version";
$result_code = run_command( "svn info $svn_url_tag", null );
if ( 0 === $result_code ) {
echo "There is already a tag for version $version:. $svn_url_tag";
exit( 1 );
}
end_group();
/**
* Subversion.
*
* @link https://stackoverflow.com/a/122291
*/
start_group( '⬇ Subversion checkout WordPress.org' );
run_command( "svn checkout $svn_url $svn_dir --depth immediates" );
run_command( "cd $svn_dir" );
chdir( $svn_dir );
run_command( 'svn update trunk --set-depth infinity' );
end_group();
/**
* Synchronize.
*
* @link http://stackoverflow.com/a/14789400
* @link http://askubuntu.com/a/476048
*/
start_group( '🔄 Synchronize plugin' );
run_command(
sprintf(
'rsync --archive --delete --verbose %s %s',
escapeshellarg( $plugin_dir . '/' ),
escapeshellarg( $svn_dir . '/trunk/' )
)
);
end_group();
/**
* Subversion modifications.
*/
start_group( '💾 Subversion modifications' );
run_command( 'svn status' );
$output = shell_exec( 'svn status --xml' );
$xml = simplexml_load_string( $output );
if ( false === $xml ) {
echo 'A problem occurred while reading the `svn status --xml`.';
exit( 1 );
}
foreach ( $xml->target->entry as $entry ) {
$path = (string) $entry['path'];
$wc_status = (string) $entry->{'wc-status'}['item'];
switch ( $wc_status ) {
case 'missing':
run_command( "svn rm $path" );
break;
case 'modified';
// Modified entry will be commited.
break;
case 'unversioned':
run_command( "svn add $path" );
break;
default:
echo "Unsupport working copy status: $wc_status - $path.";
exit( 1 );
}
}
end_group();
/**
* Commit.
*/
start_group( '⬆ Subversion commit WordPress.org' );
run_command( "svn commit --message 'Update' --non-interactive --username '$username' --password '$password'" );
end_group();
/**
* Tag.
*/
start_group( '🏷️ Subversion tag WordPress.org' );
run_command( "svn cp $svn_url/trunk $svn_url/tags/$version --message 'Tagging version $version' --non-interactive --username '$username' --password '$password'" );
end_group();
/**
* Clean up.
*/
start_group( '🗑️ Clean up' );
run_command( "rm -f -R $work_dir" );
end_group();