Created
September 14, 2020 18:52
-
-
Save covertnine/79b35b05a96484533bccf63205fd1769 to your computer and use it in GitHub Desktop.
Setting up GitHooks for WordPress related repos
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This is a brief summary of our private wiki for setting up Githooks on our servers for managing multiple WordPress sites with custom themes and plugins. | |
Setup | |
This process is a little bit involved, but hopefully we can simplify it all via a few scripts on site creation. Basically, we set up a bare repo on the remote (the server) for each plugin or theme, then create a git post-receive hook that triggers on push to that repo, which runs a script to update all the websites on the server that use that plugin or theme. | |
For this process, I'll walk through the necessary steps to deploy the c9-admin plugin to sites on our.serverhost.net | |
1) Create a bare repo on the server | |
Bare repositories live in /srv/git. You can either create a bare repo on init or on clone. In this case, we will create a bare repo call c9-admin.git by running: | |
ssh [email protected] | |
cd /srv/git | |
sudo git clone --bare https://github.com/covertnine/c9-admin.git c9-admin.git | |
Since we have to use sudo to do the git pull, the owner of the newly-created repo will be root, which we don't want. To fix that, run: | |
sudo chown -R git:git c9-admin.git | |
`` | |
` | |
### 2) Create the post-receive hook | |
Next, create a hook to be triggered on push to the `c9-admin.git` repo (Or use nano if you're a cool kid like Tim instead of Vim) | |
cd c9-admin.git/hooks sudo vim post-receive | |
Here, we just copy in a bash script that loops through a list of named websites and updates the specified theme/plugin directory. In the case of the c9-starter theme: | |
!/bin/bash | |
GIT_DIR=“/srv/git/c9-starter.git” BRANCH=“develop” WEBSITES=“siteone.covertnine.com sitetwo.covertnine.com sitethree.covertnine.com sitefour.covertnine.com sitefive.covertnine.com sitesix.covertnine.com” | |
while read oldrev newrev ref; do | |
# only checking out the master (or whatever branch you would like to deploy) | |
if [ "$ref" = "refs/heads/$BRANCH" ]; then | |
for WEBSITE in $WEBSITES; do | |
TARGET="/the/path/to-your/${WEBSITE}/www/wp-content/plugins/c9-admin" | |
echo "Ref $ref received. Deploying ${BRANCH} branch to ${TARGET}..." | |
fixsitepermissions $WEBSITE && git --work-tree=$TARGET --git-dir=$GIT_DIR checkout -f $BRANCH && fixsitepermissions $WEBSITE | |
done | |
else | |
echo "Ref $ref received. Doing nothing: only the ${BRANCH} branch may be deployed on this server." | |
fi | |
done | |
The fixsitepermissions command above is a custom script we have to properly set WordPress user/group/world permissions so we can properly write. | |
Once again, we've created a file that's owned by root, so we need to change the ownership back to git. We also want to add execution permissions. If we don't fix these front-end permissions issues, the process currently fails silently. | |
sudo chown git:git post-receive sudo chmod +x post-receive | |
### 3) Tweak permissions on the remote | |
We're close now, but let's doublecheck that git has adequate group permissions for the websites specified. From above, you'll see that we're looping through this list of websites: `WEBSITES="siteone.covertnine.com sitetwo.covertnine.com sitethree.covertnine.com sitefour.covertnine.com sitefive.covertnine.com sitesix.covertnine.com"`. These are owned by a user that takes the form c9<prefix>, e.g., c9siteone, c9sitetwo, c9sitethree. To check that git is a member of these user groups, run | |
groups git | |
currently, the output is: | |
git : git c9siteone c9sitetwo c9sitethree c9sitefour | |
So we're good. But if we wanted to add git to another group, e.g., c9sitefive, we would have to run: | |
sudo usermod -a -G c9sitefive git | |
Presumably, this is one of many aspects that we can automated away in the future. | |
### 4) Connect the bare repo to your local repo | |
Leave the server (`exit`) then navigate to your local repo (`c9-admin`). In my case, it would be | |
cd /Users/myusername/Local Sites/sitename/app/public/wp-content/plugins/c9-admin | |
I can check list my remote connections by running: | |
git remote -v | |
output: origin https://[email protected]/covertnine/c9-admin.git (fetch) origin https://[email protected]/covertnine/c9-admin.git (push) | |
Right now, it's just the github repo. I'll add the bare repo on our.serverhost.net by running: | |
git remote add stage [email protected]:/srv/git/c9-admin.git | |
### 5) Pull changes from the github repo and push them to the server repo | |
Now, I should be able to just do a pull from the github repo (origin) and a push to the staging repo (stage) | |
git pull origin develop From https://github.com/covertnine/c9-admin * branch develop → FETCH_HEAD Already up to date. | |
Now to deploy! Drumrollllll | |
git push stage develop | |
Everything up-to-date | |
Hmm, nothing happened. Well, I know that everything isn't up to date in those destination directories, but git thinks it is, because the `post-receive` hook is only triggered if some update has been made to the working tree in the bare repo itself. Basically, we need to create a new commit. To do that without arbitrarily changing files, we can commit with the `allow-empty` flag | |
git commit –allow-empty -m “empty commit for deploy” | |
Then when we run `git push stage develop` we get this output: | |
Enumerating objects: 1, done. | |
Counting objects: 100% (1/1), done. | |
Writing objects: 100% (1/1), 190 bytes | 190.00 KiB/s, done. | |
Total 1 (delta 0), reused 0 (delta 0) remote: Ref refs/heads/develop received. | |
Deploying develop branch to /the/path/to-your/siteone.website.com/www/wp-content/plugins/c9-admin… | |
remote: fatal: This operation must be run in a work tree remote: Ref refs/heads/develop received. | |
Deploying develop branch to /the/path/to-your/sitetwo.website.com/www/wp-content/plugins/c9-admin… | |
remote: Switched to branch 'develop' | |
remote: Ref refs/heads/develop received. | |
Deploying develop branch to /the/path/to-your/sitethree.website.com/www/wp-content/plugins/c9-admin… | |
remote: Already on 'develop' To our.serverhost.net:/srv/git/c9-admin.git | |
054b965..dcda48a develop -> develop | |
``` | |
Notice, the sitethree deploy failed with the message remote: fatal: This operation must be run in a work tree. This is because the directory c9-admin did not exist in our.serverhost.net. | |
In this case, I just create an empty directory at /the/path/to-your/our.serverhost.net/www/wp-content/plugins/c9-admin and rerun the command. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment