Do you run your own static site / blog? Do you still have a shared web hosting service you pay for? Would you like to jump into continuous deployment using just your web host?
.gitignore
issuesI host my blog my domain. I use Gatsby to build this blog. The source code is hosted on Bitbucket. Once built, the static files are hosted on Webhosting Hub .
Today, my process is follows:
While this works, I was looking to automate Steps 6 to 9. Once I push the source changes, I would like to have the web host files updated with the new build .I have setup Netlify pipelines for my Booknotes site which does exactly that.
However once I realized that thanks to Git + cPanel, one push deployment can be done, I wanted to try it out. Why - I like the idea of trying something new - I like the idea of relying /maintaining one less service - for my small needs.
Visually, this is the flow I want. Basically, what cPanel calls ”Push Deployment”
Then
git config --local user.name
git config --local user.email
If the username and email are not set, you can choose to set it or use the global email. To set it, use the same commands with the values you want to set
git config --local user.name "My Name"
git config --local user.email "my_git_email@example.com"
Make a note of this email address.
$ ssh-keygen -t rsa -b 4096 -C "my_git_email@example.com"
eval "$(ssh-agent -s)"
open ~/.ssh/config
AddKeysToAgent yes
UseKeychain yes
IdentityFile ~/.ssh/<your file name> ```
ssh-add ~/.ssh/<your file name>
Update the Clone URL you had copied in the last step of Section 1 with the port.
For example, if your URL was ssh://username@domain.com/home/{username}/repositories/{reponame}
and your port is 2222
, your new URL will be ssh://username@domain.com:2222/home/{username}/repositories/{reponame}
Go to your local repository and add this as a remote. To do this, run the command
git remote add <remotename> <cloneurl>
.
<remotename>
needs to be unique. Like many others, I use ‘cpanel’ for cPanel, and ‘origin’ for Bitbucket.
<cloneurl>
is the clone URL you copied over form Git Version Control.
Note: the instructions mentioned in cPanel name the remote as ‘origin’. However, I use ‘origin’ for my Github/Bitbucket accounts, and name this remote as ‘cpanel’
Push your changes to the cpanel repository.
git push -u cpanel main
This should fail at this point with a timeout error
.gitignore setup for checking out publci only
.gitignore
issuesGenerally, for Gatsby repos, the ‘public’ directory is included in the .gitignore file and is ignored and these files are not pushed to the repo. In my case, I want my Bitbucket repo to stay clean, as they are. However I want the public files to get pushed to the cPanel repo. For this, I have to use a slightly clunky mechanism - branches.
git checkout -b deployment
.git push cpanel deployment
.The official way using a .cpanel.yml
did not work for me. I kept seeing the following error:
[username@domain hooks]$ ./post-receive
./post-receive: line 17: /dev/stdin: Operation not permitted
Digging on forums, I realized others had similar issue
Instead, inspired by here and here, I just re-use the post-receive git hook to
#!/bin/sh
# post-receive hook
echo "Received a push to repo. Checking out deployment branch via work tree"
git --work-tree=/home/{username}/repositories/{reponame} --git-dir=/home/{username}/repositories/{reponame}/.git checkout -f deployment
echo "copy updated files from repo public to deploy path - public_html/blog"
export DEPLOYPATH=/home/{username}/public_html/blog/
/bin/cp -R -u /home/{username}/repositories/{reponame}/public/* $DEPLOYPATH
echo "copy done"
In your script, replace {username}
with your username,{reponame}
with your repository name, and set the DEPLOYPATH to where you want to deploy.
home/{username}/repositories/{reponame}/.git/hooks
chmod +x /home/{username}/repositories/{reponame}/.git/hooks/post-receive
Now, when you push a file to cPanel from your local repository using git push cpanel deployment
, this will automatically move the files over to where you want it to be.
I haven’t implemented it yet. https://forums.cpanel.net/threads/git-update-notification.645649/ has more information to do this.
Here is what my process looks like.
git checkout main
gatsby build
. Test buildgit push origin main
git checkout deployment
git merge master
git commit -m "update: new blog post"
git push cpanel deployment
And there you go! Easy-peasy.
I would love to hear from you (tweet @suprada) if you have an even better way of leveraging cpanel!
April 25, 2021