-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathdeploy.sh
executable file
·333 lines (283 loc) · 9.38 KB
/
deploy.sh
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#!/bin/bash
# Main config
DIR=$(pwd)
PLUGINSLUG=$(basename "$DIR")
MAINFILE="$PLUGINSLUG.php"
# SVN user
SVNUSER=$1
# Verbose mode
VERBOSE=false
# Deploy mode
DEPLOY=false
# Skip assets
SKIP_ASSETS=false
# Script name
SCRIPT_NAME=$(basename "$0")
# Git branch
GITBRANCH="main"
# Parse arguments
for arg in "$@"; do
case "$arg" in
-d|--deploy)
DEPLOY=true
;;
-v|--verbose)
VERBOSE=true
;;
-sa|--skip-assets)
SKIP_ASSETS=true
;;
-b|--branch)
GITBRANCH="$2"
shift
;;
-mf|--mainfile)
MAINFILE="$2"
shift
;;
-p|--path)
DIR="$2"
shift
;;
-s|--slug)
PLUGINSLUG="$2"
shift
;;
-h|--help)
echo "Deploy WordPress plugin to the official repository."
echo "Dry-run mode is enabled by default. Use -d or --deploy to deploy."
echo "Usage: ./$SCRIPT_NAME <svn-user> [-d|--deploy] [-v|--verbose] [-sa|--skip-assets] [-b|--branch <branch>] [-mf|--mainfile <mainfile.php>] [-p|--path <plugin-path>] [-s|--slug <plugin-slug>]"
exit 0
;;
*)
# Assume the first non-flag argument is PARAM1
if [[ -z "$SVNUSER" ]]; then
SVNUSER="$arg"
fi
;;
esac
done
# Validate required parameter
if [[ -z "$SVNUSER" ]]; then
echo "Error: Missing required parameter <svn-user>."
echo "Usage: ./$SCRIPT_NAME <svn-user> [-d|--deploy] [-v|--verbose] [-sa|--skip-assets] [-b|--branch <branch>] [-mf|--mainfile <mainfile.php>] [-p|--path <plugin-path>] [-s|--slug <plugin-slug>]"
exit 1
fi
# Git config
GITPATH="$DIR/"
# SVN config
SVNPATH="/tmp/$PLUGINSLUG" # path to a temp SVN repo. No trailing slash required and don't add trunk.
SVNURL="http://plugins.svn.wordpress.org/$PLUGINSLUG/" # Remote SVN repo on wordpress.org, with no trailing slash
# Function to execute or echo commands based on deploy mode
execute_or_echo() {
local command="$1"
shift
local args=("$@")
# Determine the command type (e.g., git, svn)
case "$command" in
git)
# In dry-run mode, allow all git commands except commit, tag, push
if ! $DEPLOY && [[ "${args[0]}" == "commit" || "${args[0]}" == "tag" || "${args[0]}" == "push" ]]; then
echo "[DRY-RUN] $command ${args[*]}"
else
if $VERBOSE; then
echo "$command ${args[*]}"
fi
"$command" "${args[@]}"
fi
;;
svn)
# In dry-run mode, allow all svn commands except commit
if ! $DEPLOY && [[ "${args[0]}" == "commit" ]]; then
echo "[DRY-RUN] $command ${args[*]}"
else
if $VERBOSE; then
echo "$command ${args[*]}"
fi
"$command" "${args[@]}"
fi
;;
gh)
# In dry-run mode, disallow all gh commands
if ! $DEPLOY; then
echo "[DRY-RUN] gh ${args[*]}"
else
if $VERBOSE; then
echo "gh ${args[*]}"
fi
"$command" "${args[@]}"
fi
;;
*)
# For other commands, actually execute them
if $VERBOSE; then
echo "$command ${args[*]}"
fi
"$command" "${args[@]}"
;;
esac
}
echo ".........................................."
echo ""
if $DEPLOY; then
echo "Deployment"
else
echo "Dry-run - use --deploy to deploy"
fi
echo ""
echo ".........................................."
echo ""
# Check if subversion is installed before running
if ! which svn >/dev/null; then
echo "Command 'svn' not found. Exiting."
exit 1
fi
# Check version in readme.txt is the same as plugin file
NEWVERSION1=$(grep "^Stable tag:" "$GITPATH"/readme.txt | awk -F' ' '{print $NF}')
NEWVERSION2=$(grep -E "^[[:space:]]*\*?[[:space:]]*Version:" "$GITPATH"/"$MAINFILE" | awk -F' ' '{print $NF}')
echo "readme.txt version: $NEWVERSION1"
echo "$MAINFILE version: $NEWVERSION2"
# Check if version in readme.txt & $MAINFILE don't match
if [ "$NEWVERSION1" != "$NEWVERSION2" ]; then
echo "Version in readme.txt & $MAINFILE don't match. Exiting."
exit 1
fi
# Check if git tag exists
if git show-ref --tags --quiet --verify -- "refs/tags/$NEWVERSION1"; then
echo "Version $NEWVERSION1 already exists as git tag. Exiting."
exit 1
fi
# Prompt for commit message
echo -e "Enter a commit message for this new version: \c"
read -r COMMITMSG
# Check that GITBRANCH exists
if ! git show-ref --verify --quiet "refs/heads/$GITBRANCH"; then
echo "Branch $GITBRANCH does not exist. Exiting."
exit 1
fi
# If gh cli is installed, check if user is authenticated
if which gh > /dev/null; then
# If the user is not authenticated, instruct to run 'gh auth login' and exit
if ! gh auth status > /dev/null 2>&1; then
echo "You are not authenticated. Please run 'gh auth login'. Exiting."
exit 1
fi
fi
# Keep the current branch so that we can switch back to it later
CURRENTBRANCH=$(git rev-parse --abbrev-ref HEAD)
# Switch to $GITBRANCH branch
execute_or_echo git checkout "$GITBRANCH"
# Commit changes
execute_or_echo git commit -am "$COMMITMSG"
# Tag new version in git
execute_or_echo git tag -a "$NEWVERSION1" -m "Tagging version $NEWVERSION1"
# Push changes to origin
execute_or_echo git push origin "$GITBRANCH"
execute_or_echo git push origin "$GITBRANCH" --tags
# check if gh cli is installed
if which gh > /dev/null; then
# Create a GitHub release using the GitHub API
echo "Creating GitHub release for tag $NEWVERSION1..."
# Define the GitHub repository owner and name
GITHUB_OWNER=$(git config --get remote.origin.url | sed -E 's#(https://github.com|[email protected]:)([^/]+)/.*#\2#')
GITHUB_REPO=$(git config --get remote.origin.url | sed -E 's#(https://github.com|[email protected]:)[^/]+/([^/]+).git#\2#')
# Define the release path
RELEASEPATH="/tmp/$PLUGINSLUG-release/"
# Define the zip file name
ZIPFILE="$PLUGINSLUG.zip"
# Delete the release path if it doesn't exist
if [ -d "$RELEASEPATH" ]; then
execute_or_echo rm -fr "$RELEASEPATH"
fi
# Create the release path
execute_or_echo mkdir -p "$RELEASEPATH"
# Create a zip file of the plugin, excluding all hidden files and *.sh files
execute_or_echo rsync -r --exclude=".*" --exclude="*.sh" "$GITPATH" "$RELEASEPATH"
# Use tar to create the zip file
execute_or_echo tar -czf "/tmp/$ZIPFILE" -C "$RELEASEPATH" .
# Delete the release path
execute_or_echo rm -fr "$RELEASEPATH"
# use gh cli to create a release
execute_or_echo gh release create v"$NEWVERSION1" \
--title "Release v$NEWVERSION1" \
--notes "Auto-deployed from tag $NEWVERSION1" \
--repo "$GITHUB_OWNER/$GITHUB_REPO" \
"/tmp/$ZIPFILE"
# Delete the zip file
execute_or_echo rm -f "/tmp/$ZIPFILE"
else
echo "Command 'gh' not found. Skipping GitHub release creation."
fi
# Delete the local SVN repo if it exists
if [ -d "$SVNPATH" ]; then
execute_or_echo rm -fr "$SVNPATH"
fi
# Create local copy of SVN repo
execute_or_echo svn co "$SVNURL" "$SVNPATH"
#Init directories assets, tags, trunk if they do not exist
if [ ! -d "$SVNPATH"/assets ]; then
execute_or_echo mkdir "$SVNPATH"/assets
fi
if [ ! -d "$SVNPATH"/tags ]; then
execute_or_echo mkdir "$SVNPATH"/tags
fi
if [ ! -d "$SVNPATH"/trunk ]; then
execute_or_echo mkdir "$SVNPATH"/trunk
fi
# Clear SVN repo trunk only if it is not empty
if [ -n "$(ls -A "$SVNPATH"/trunk/ 2>/dev/null)" ]; then
execute_or_echo svn rm "$SVNPATH"/trunk/*
else
echo "Trunk is already empty. Skipping removal."
fi
# Export HEAD of branch from git to SVN trunk
execute_or_echo git checkout-index -a -f --prefix="$SVNPATH"/trunk/
# Ignore GitHub-specific files
execute_or_echo svn propset svn:ignore "deploy.sh
.DS_Store
.vscode
.git
.gitignore" "$SVNPATH/trunk/"
# Add only readme.txt to SVN trunk
execute_or_echo cd "$SVNPATH"/trunk/
execute_or_echo svn add readme.txt
# Create new SVN tag
execute_or_echo cd "$SVNPATH"
execute_or_echo svn copy trunk/ tags/"$NEWVERSION1"/
# Add all new files in the tag folder
execute_or_echo cd "$SVNPATH"/tags/"$NEWVERSION1"
execute_or_echo bash -c "
svn status |
grep '^?' |
awk '{print \$2}' |
xargs -I {} svn add {}
"
if ! $SKIP_ASSETS; then
# Change to the assets folder
execute_or_echo cd "$SVNPATH"/assets/
# Pause the script until the user presses a key
echo "Adjust assets in $SVNPATH/assets/ and press any key to continue..."
read -rn 1 -s
# Add all new files in the assets folder
execute_or_echo bash -c "
svn status |
grep '^?' |
awk '{print \$2}' |
xargs -I {} svn add {}
"
fi
# Commit trunk, tag and assets changes in one step
execute_or_echo cd "$SVNPATH"
execute_or_echo svn commit --username="$SVNUSER" -m "$COMMITMSG"
# Go back to current directory
execute_or_echo cd "$GITPATH"
# Clean up temporary directory
execute_or_echo rm -fr "${SVNPATH:?}/"
# Switch back to the original branch
execute_or_echo git checkout "$CURRENTBRANCH"
if ! $DEPLOY; then
echo "*** Dry-run complete. No changes were made. ***"
else
echo "*** Deployment complete. ***"
fi
echo ""