Skip to content

Commit 214ccdc

Browse files
committed
start
0 parents  commit 214ccdc

File tree

4 files changed

+265
-0
lines changed

4 files changed

+265
-0
lines changed

.gitignore

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
composer.lock
2+
node_modules/
3+
vendor/
4+
5+
# Numerous always-ignore extensions
6+
*.diff
7+
*.err
8+
*.log
9+
*.orig
10+
*.rej
11+
*.swo
12+
*.swp
13+
*.vi
14+
*.zip
15+
*~
16+
*.sql
17+
*.tar.gz
18+
19+
# OS or Editor folders
20+
*.tmp
21+
*.code-workspace
22+
._*
23+
*.cache
24+
.DS_Store
25+
.idea
26+
.project
27+
.settings
28+
.tmproj
29+
.vscode
30+
*.esproj
31+
*.sublime-project
32+
*.sublime-workspace
33+
*.code-workspace
34+
*.vscode
35+
nbproject
36+
Thumbs.db

Dockerfile

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
FROM debian:stable-slim
2+
3+
RUN apt-get update \
4+
&& apt-get install -y subversion rsync git php-cli \
5+
&& apt-get clean -y \
6+
&& rm -rf /var/lib/apt/lists/*
7+
8+
COPY wp-plugin-deploy.php /wp-plugin-deploy.php
9+
10+
ENTRYPOINT ["/wp-plugin-deploy.php"]

action.yml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
name: 'WordPress Plugin Deploy by NextgenThemes'
2+
description: 'Deploy to the WordPress Plugin Repository'
3+
author: 'nextgenthemes' # inspired by 10up action
4+
branding:
5+
icon: 'upload-cloud'
6+
color: 'blue'
7+
8+
inputs:
9+
svn_user:
10+
required: true
11+
svn_pass:
12+
required: true
13+
version:
14+
description: Is required unless you just update readme and assets
15+
required: false
16+
workdir:
17+
description: Optional relative path to where the main plugin file is.
18+
required: false
19+
build_dirs:
20+
description: Comma separated list of folders to include in distribution.
21+
required: false
22+
switch:
23+
description: Use --readme-only or --dry-run
24+
required: false
25+
26+
runs:
27+
using: "docker"
28+
image: "Dockerfile"
29+
args:
30+
- --svn-user=${{ inputs.svn_user }}
31+
- --svn-pass=${{ inputs.svn_pass }}
32+
- --version=${{ inputs.version }}
33+
- --build-dirs=${{ inputs.build_dirs }}
34+
- --workdir=${{ inputs.workdir }}
35+
- ${{ inputs.switch }}
36+
37+

wp-plugin-deploy.php

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
#!/usr/bin/env php
2+
<?php
3+
use function \escapeshellarg as e;
4+
5+
$workdir = arg_with_default('workdir', false);
6+
7+
if ( $workdir ) {
8+
chdir( getcwd() . "/$workdir");
9+
}
10+
11+
if ( getenv( 'GITHUB_ACTION' ) ) {
12+
sys('git config --global --add safe.directory /github/workspace');
13+
}
14+
15+
$slug = basename(getcwd());
16+
$cwd = getcwd();
17+
$git_dir = sys('git rev-parse --show-toplevel');
18+
$subdir = trim(str_replace($git_dir, '', $cwd), '/');
19+
$svn_user = arg_with_default('svn-user', false);
20+
$svn_pass = arg_with_default('svn-pass', false);
21+
$build_dirs = arg_with_default('build-dirs', false);
22+
$version = arg_with_default('version', false);
23+
$svn_url = "https://plugins.svn.wordpress.org/$slug/";
24+
$tmp_dir = '/tmp/wp-deploy';
25+
$svn_dir = "$tmp_dir/svn-$slug";
26+
$gitarch_dir = "$tmp_dir/gitarchive-$slug";
27+
$readme_only = has_arg('readme-only');
28+
$dry_run = has_arg('dry-run');
29+
30+
if ( $readme_only ) {
31+
$commit_msg = 'Update readme and assets from current dir with automation script';
32+
} else {
33+
$version = required_arg('version');
34+
$commit_msg = "Update to $version with automation script";
35+
}
36+
37+
if ( $build_dirs ) {
38+
$build_dirs = explode(',', $build_dirs);
39+
}
40+
41+
sys('rm -rf ' . e($tmp_dir) );
42+
43+
var_export(
44+
compact(
45+
'slug',
46+
'cwd',
47+
'git_dir',
48+
'subdir',
49+
'svn_url',
50+
'build_dirs',
51+
'tmp_dir',
52+
'version',
53+
'workdir',
54+
'readme_only',
55+
'dry_run'
56+
)
57+
);
58+
59+
# Checkout just trunk and assets for efficiency
60+
# Tagging will be handled on the SVN level
61+
echo '➤ Checking out wp.org repository...';
62+
sys( 'svn checkout --depth immediates ' . e($svn_url) . ' ' . e($svn_dir) );
63+
64+
chdir($svn_dir);
65+
sys('svn update --set-depth infinity assets');
66+
sys('svn update --set-depth infinity trunk');
67+
68+
echo '➤ Copying files...' . PHP_EOL;
69+
70+
if ( $readme_only ) {
71+
$last_svg_tag = sys('svn ls '.e("$svn_dir/tags").' | tail -n 1');
72+
sys('svn update --set-depth immediates '.e("$svn_dir/tags/$last_svg_tag"));
73+
copy( "$cwd/readme.txt", "$svn_dir/tags/$last_svg_tag/readme.txt" );
74+
copy( "$cwd/readme.txt", "$svn_dir/trunk/readme.txt" );
75+
76+
if ( $version ) {
77+
sys('svn update --set-depth immediates '.e("$svn_dir/tags/$version"));
78+
copy( "$cwd/readme.txt", "$svn_dir/tags/$version/readme.txt" );
79+
}
80+
} else {
81+
mkdir($gitarch_dir);
82+
sys('git --git-dir='.e("$git_dir/.git").' archive '.e("$version:$subdir").' | tar x --directory='.e($gitarch_dir));
83+
sys('rsync -rc '.e("$gitarch_dir/").' '.e("$svn_dir/trunk").' --delete --delete-excluded');
84+
85+
foreach ( $build_dirs as $build_dir ) {
86+
$build_dir = trim( $build_dir );
87+
88+
if ( ! file_exists( "$cwd/$build_dir" ) ) {
89+
echo 'Build dir '.e("$cwd/$build_dir").' does not exists.' . PHP_EOL;
90+
exit(1);
91+
}
92+
93+
sys('rsync -rc '.e("$cwd/$build_dir").' '.e("$svn_dir/trunk/").' --delete');
94+
}
95+
}
96+
97+
sys('rsync -rc '.e("$cwd/.wordpress-org/").' '.e("$svn_dir/assets").' --delete');
98+
99+
# Add everything and commit to SVN
100+
# The force flag ensures we recurse into subdirectories even if they are already added
101+
echo '➤ Preparing files...' . PHP_EOL;
102+
sys('svn add . --force --quiet');
103+
104+
# SVN delete all deleted files
105+
# Also suppress stdout here
106+
sys("svn status | grep '^\!' | sed 's/! *//' | xargs -I% svn rm %@ --quiet");
107+
108+
# Copy tag locally to make this a single commit
109+
if ( ! $readme_only ) {
110+
echo '➤ Copying tag...' . PHP_EOL;
111+
112+
if ( 'trunk' !== $version ) {
113+
sys('svn cp trunk '.e("tags/$version"));
114+
}
115+
}
116+
117+
# Fix screenshots getting force downloaded when clicking them
118+
# https://developer.wordpress.org/plugins/wordpress-org/plugin-assets/
119+
system('svn propset svn:mime-type image/png assets/*.png');
120+
system('svn propset svn:mime-type image/jpeg assets/*.jpg');
121+
122+
sys('svn status');
123+
if ( $dry_run ) {
124+
echo '➤ Dry run exit' . PHP_EOL;
125+
exit(1);
126+
}
127+
$commit_cmd = 'svn commit -m '.e($commit_msg).' ';
128+
if ( $svn_user && $svn_pass ) {
129+
$commit_cmd .= ' --no-auth-cache --non-interactive --username '.e($svn_user).' --password '.e($svn_pass);
130+
}
131+
echo '➤ Committing files...' . PHP_EOL;
132+
sys($commit_cmd);
133+
134+
echo '✓ Plugin deployed!';
135+
136+
function has_arg( string $arg ): bool {
137+
$getopt = getopt( '', [ $arg ] );
138+
return isset($getopt[ $arg ]);
139+
}
140+
141+
function required_arg( string $arg ): string {
142+
143+
$getopt = getopt( '', [ "$arg:" ] );
144+
145+
if ( empty($getopt[ $arg ]) ) {
146+
echo "need --$arg=x";
147+
exit(1);
148+
}
149+
150+
return $getopt[ $arg ];
151+
}
152+
153+
function arg_with_default( string $arg, $default ): string {
154+
155+
$getopt = getopt( '', [ "$arg::" ] );
156+
157+
if ( empty($getopt[ $arg ]) ) {
158+
return $default;
159+
}
160+
161+
return $getopt[ $arg ];
162+
}
163+
164+
function sys( string $command, array $args = [] ): ?string {
165+
166+
foreach ( $args as $k => $v ) {
167+
$command .= " --$k=" . escapeshellarg($v);
168+
}
169+
170+
echo "Executing: $command" . PHP_EOL;
171+
$out = system( $command, $exit_code );
172+
echo PHP_EOL;
173+
174+
if ( 0 !== $exit_code || false === $out ) {
175+
echo 'Error, output: ';
176+
var_dump($out);
177+
echo "Exit Code: $exit_code." . PHP_EOL;
178+
exit($exit_code);
179+
}
180+
181+
return $out;
182+
}

0 commit comments

Comments
 (0)