-
Notifications
You must be signed in to change notification settings - Fork 1
/
wpdeploy.sh
105 lines (88 loc) · 3.52 KB
/
wpdeploy.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
themeName="wcco"
# Set variables
####################
# Cpanel remote URL
RemoteName=$1
# Get present working directory
presentWorkingDirectory=`pwd`
# Get current branch user is on
currentLocalGitBranch=`git rev-parse --abbrev-ref HEAD`
# Temporary git branch for building and deploying
tempDeployGitBranch="cpanelDeploy/${currentLocalGitBranch}"
# KWB themes directory
ThemesDirectory="${presentWorkingDirectory}/app/themes/"
# Git checks
####################
# Halt if there are uncommitted files
if [[ -n $(git status -s) ]]; then
echo -e "[\033[31mERROR\e[0m] Found uncommitted files on current branch \"$currentLocalGitBranch\".\n Review and commit changes to continue."
git status
exit 1
fi
# Check if specified remote exist
git ls-remote "$RemoteName" &> /dev/null
if [ "$?" -ne 0 ]; then
echo -e "[\033[31mERROR\e[0m] Unknown git remote \"$RemoteName\"\n Visit \033[32mhttps://wpengine.com/git/\e[0m to set this up."
echo "Available remotes:"
git remote -v
exit 1
fi
# Directory checks
####################
# Halt if theme directory does not exist
if [ ! -d "$presentWorkingDirectory"/app/themes/"$themeName" ]; then
echo -e "[\033[31mERROR\e[0m] Theme \"$themeName\" not found.\n Set \033[32mthemeName\e[0m variable in $0 to match your theme in $ThemesDirectory"
echo "Available themes:"
ls $ThemesDirectory
exit 1
fi
####################
# Begin deploy process
####################
# Checkout new temporary branch
echo "Preparing theme on branch ${tempDeployGitBranch}..."
git checkout -b "$tempDeployGitBranch" &> /dev/null
# Create friendly gitignore
rm .gitignore &> /dev/null
echo -e "/*\n!wp-content/" > ./.gitignore
echo "!.cpanel.yml" >> ./.gitignore
# Copy meaningful contents of app into wp-content
mkdir wp-content && cp -rp app/plugins wp-content && cp -rp app/themes wp-content && cp app/.cpanel.yml .
# Go into theme directory
cd "$presentWorkingDirectory/wp-content/themes/$themeName" &> /dev/null
# Build theme assets
yarn install && yarn build
# Back to the top
cd "$presentWorkingDirectory"
# Cleanup wp-content
####################
# Remove Unnecesary Files
rm "$presentWorkingDirectory"/wp-content/themes/"$themeName"/.gitignore &> /dev/null
rm "$presentWorkingDirectory"/wp-content/themes/"$themeName"/package.json &> /dev/null
rm "$presentWorkingDirectory"/wp-content/themes/"$themeName"/webpack.config.js &> /dev/null
rm "$presentWorkingDirectory"/wp-content/themes/"$themeName"/yarn.lock &> /dev/null
# Remove Unnecesary Directories
rm -rf "$presentWorkingDirectory"/wp-content/themes/"$themeName"/node_modules &> /dev/null
rm -rf "$presentWorkingDirectory"/wp-content/themes/"$themeName"/assets &> /dev/null
rm -rf "$presentWorkingDirectory"/wp-content/themes/"$themeName"/webpack &> /dev/null
####################
# Push to Cpanel
####################
git ls-files | xargs git rm --cached &> /dev/null
cd "$presentWorkingDirectory"/wp-content/
find . | grep .git | xargs rm -rf
cd "$presentWorkingDirectory"
git add --all &> /dev/null
git commit -am "Cpanel build from: $(git log -1 HEAD --pretty=format:%s)$(git rev-parse --short HEAD 2> /dev/null | sed "s/\(.*\)/@\1/")" &> /dev/null
echo "Pushing to CPanel..."
# Push to a remote branch with a different name
# git push remoteName localBranch:remoteBranch
git push "$RemoteName" "$tempDeployGitBranch":master --force
####################
# Back to a clean slate
####################
git checkout "$currentLocalGitBranch" &> /dev/null
rm -rf wp-content/ &> /dev/null
rm .cpanel.yml &> /dev/null
git branch -D "$tempDeployGitBranch" &> /dev/null
echo "Done"