My first GitLab CI/CD
If you can read this post, my GitLab CI/CD setup worked!
CI/CD
I've been reading about this CI/CD thing for a while, but it looked too specific for real programmers for my level of knowledge. Until I had a real excuse problem that can be solved with CI/CD: build my website with Hugo and deploy it with LFTP to my hosting provider.
Enter CI/CD!
For dummies
Quite often it is hard for me to grasp a concept than is far from my area of expertise. I usually need someone to explain the basics with very simple words, with some examples of what problem it tries to solve, and then it clicks 💡 and I can start the learning process.
In this post I describe how I setup CI/CD for my specific use case.
The pieces:
- self-hosted GitLab instance: the repository of my code
- GitLab runner: a container that registers itself to the GitLab instance and is associtated to the repository
- .gitlab-ci.yml file in the repository: instructions for the runner about the container and scripts to execute
- pushover to receive a notification when the deploy is completed
This is what my .gitlab-ci.yml looks like:
1stages:
2 - build
3 - deploy
4build:
5 stage: build
6 image: jojomi/hugo
7 script:
8 - hugo version
9 - git submodule update --init --recursive
10 - hugo -d public_html --gc --minify
11 artifacts:
12 paths:
13 - public_html
14 only:
15 - master
16deploy:
17 stage: deploy
18 image: alpine:latest
19 tags:
20 - private
21 before_script:
22 - apk update
23 - apk add lftp
24 script:
25 - lftp -e "set ssl:verify-certificate false; open $FTP_HOST; user $FTP_USER $FTP_PASSW; mirror -R -P 5 -p -n --delete ./public_html /www.ifconfig.it/hugo; bye"
26 - curl -d user=$PUSHUSER -d token=$PUSHTOKEN -d device=p20lite -d title="IFCONFIG" -d message="upload completed" https://api.pushover.net/1/messages.json
27 only:
28 - master
The process:
- I create new posts on a dev branch, one branch for each new post
- when the post is ready for publication, it is merged to the master branch
- when the master branch receives a commit, the GitLab runner creates the container and executes the scripts
- the html files are published with the changes within minutes from the commit
- a notification is sent to my phone to confirm the process was executed
Wrap up
This simple CI/CD pipeline made my workflow much easier. Now I can focus more on writing posts and less on the overhead build/deploy tasks. I plan to add in future some tests before the publication.
Links
- Configuring Hugo for GitLab CI/CD by William Smith
- Deploying a Hugo Static Site Using GitLab, CI/CD, & SSH