After reviewing the stack and setup for the blog over the last few days, I noticed something interesting the original CircleCI Config file:
1 | version: 2.1 |
Basically, the number of commands to build and deploy the server were kinda. . . long. Both in just the number of commands, but also in the deployment times:
Now, ~1 minute deployments to the S3 bucket are perfectly fine for the use case of this blog. However, for fun, I thought it would be neat to see if I could get that time down. Enter. . . Docker
Docker
Note; I won’t go into detail about Docker and containerization as that’s outside the scope of this blog. But images and Containers are our present and future, and we all need to be comfortable with them 🙂
So CircleCI spins up a Docker image and then runs the commands from the config and pushes the code to S3. To make the code push complete faster, I thought “why not have all the commands pre-ran/built in Docker?”.
As a good technologist, rather than build something entirely from scratch, I looked to see if there were any Docker images that already had Hexo pre-installed. I found this awesome image from James Spurin and read this great article as well, which put the wheels in motion for my next move. I could have easily used the Spurin Hexo Docker image, but the use-case for that image is clearly more to build the Hexo website within a Docker Container, whereas my use-case was to use a Docker Container to build and deploy the Hexo website.
Building a “custom” Docker Container
After taking a look at the CircleCI config file, I realized that I need the Docker image to simply do the following:
- Install Hexo
- Download the GitHub repo
- Generate the thecloudonmymind.com blog
- Push the generated website to AWS S3
I thought it would be a good opportunity to learn a little more Docker and spin up an image myself that does just that. So after a fair bit of testing on machine, I came up with this Docker Container!
To get this rolling, I had to perform the following:
- Setup a Docker Hub Account
- Create a Docker Hub Organization to house the Container
- Build the Docker image locally based on this repo I created.
- Tag and push the Docker Container to Docker Hub
The Dockerfile in that repo shows the components that make up and essentially “pre-load” the Container for CircleCI to use. My initial thoughts were that a Container could house the GitHub codebase too, but then for every commit to the blog repo I would need to re-build the Docker Container so that it has the latest changes on the Container. . .not super efficient and may make the Container startup time longer. . .
I also learned that in order to authorize Docker Hub to build the Containers you should do this within a GitHub organization (so as to not expose your private and public GitHub repos to Docker Hub). So I setup a Docker Hub Organization and Git Hub Organization based on this blog, and we’ll see where the future take us!
With the new pre-installed Hexo with AWS CLI Container now being available, the CircleCI Config file now looks like this:
1 | version: 2.1 |
And now takes around ~30 seconds to deploy. . .not too bad and definitely an improvement! 💪
Recap
- Built a custom pre-installed Hexo and AWS CLI Docker Container
- Took the CircleCI config steps from 9 commands to 3 commands
- Improved the website deployment from ~1 minute to ~30 seconds!