Automating Hugo on CPanel Hosting

Automating Hugo on CPanel Hosting

After producing this site in Hugo and fundamentally really happy with the new process that I built for myself. The let down was my hosting company and it seems to be unable to provide me with a consistent way to connect to FTP which I had thought I’d fixed here.

A month on from writing that post I seem to have the issues again and have gone over the same steps again to not really get an answer from them I started to look into other ways of uploading my content. Now there was the first way in which I log into CPanel and then just upload the public folder that I would have zipped up and then extracted and copy it to the right directory. This could work but also runs the risk of being dragged to the wrong place or deleting the wrong files. My other option it seems was to use Git.

You can set your own Git repository using the CPanel and then upload your code and as long as your pushing directly to your server you can use a yml file to trigger a build.

The Instructions

Setting up your Git Repository

Log into your CPanel and see if you have Git Version Control of which you should have as long as your running version 74 I think it was introduced and above.

Then you want to create a new repository and I chose to start a new one and not clone another repository. Then you pick the path you want your repository to be stored on your server and give it a name and then hit create.

After that, it will give you some instructions I followed the existing project and took the command to add the new remote git directory.

Once I added the remote directory of which it asked for my CPanel Password in order for me to connect to I ran this command to send my Hugo files to the server.

git push -u origin master

Once I had completed this I went into the CPanel and Files Manager to see to check the files had been committed they had.

Setting up SSH Keys

As I am the only person that has access to my PC I wanted to set up keys that meant I didn’t have to enter my password every time I wanted to commit something to my repository so instead of messing around with the SSH keys that were already on the server I decided to regenerate my keys locally and upload them.

Having installed Git I opened Git Bash and ran this command to generate my new SSH Keys

ssh-keygen -o

Of which, because I didn’t want a password when asked for a password I hit enter twice and let it generate my keys. Once complete I logged back into my CPanel and went to manage keys and imported my new Public key and authorized it. With that done I opened Visual Studio Code to find it sync with my project happily without any password.

Setting up the Auto Build

Part of the reason for me hosting my own repo instead of just using GitHub is because by pushing changes to the server on every push you can get the server to complete actions that you can’t currently when pulling from a repository to yours.

So I added a bin folder to the working files of my Hugo Site and went onto the Hugo Releases Page and the Hugo Linux-32bit.gz and extracted the Hugo executable for Linux and put it in my bin folder.

Once committed to making sure that it worked I opened Terminal that I could access through CPanel and run the Hugo command to make sure it worked. I did this by just running

hugo version

That way I wasn’t interfering with anything to make sure it would run on my server.

.cpanel.yml file

Back in Visual Code I created a .cpanel.yml file to tell the server what to do after every commit I push and that was build my site.

 ---
deployment:
  tasks:
  - ./bin/hugo 

The only task we set on deployment is for to go into the bin folder of the repository and run Hugo. This would create the public folder that I would need to then copy over to the right folder again this isn’t ideal so I added this code to my config.toml file of my Hugo site. publishdir = "path that you want to send the final build"

As your looking at your file manager you want to copy that file path.

You can add any hugo commands after hugo if you don’t want the normal build for example: ./bin/hugo --minify

Conclusion

Is this ideal? in all honesty no just in a few days of using it, I’ve seen the git repo take up more space then I had thought it would with the different commits I’ve done to test it. But is it the method I am going to be staying with? For the time being, having your website instantly update as soon as you save a file and send it is kind of amazing to me. The only thing I may do from time to time is a double check I haven’t got any stale content by deleting the contents of my public folder and then rebuilding. However, the way I commit my site I don’t feel I will need to do that very often. And after testing, I doubt the size of my git is going to be an issue either. Along with the fact this way it gives me a controlled environment for my website as the version of Hugo that I am using can remain the same and not be updated if I don’t feel it needs it.

Update

I found out that the above works in Linux but in Windows, my deploy script in my cpanel.yml file failed to run. The reason for this is Windows constantly wanted to remove execute permissions for hugo that was stored in the bin folder of my repository. What I did to solve this was uploaded a copy of hugo to my CPanel bin folder and just put the absolute path for hugo and that solved the issue as the hugo file is no longer part of the repository of working files it won’t be adjusted.

Windows .cpanel.yml

deployment:
  tasks:
    - /home/<username>/bin/hugo