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:

stages:
  - build
  - deploy
build:
  stage: build
  image: jojomi/hugo
  script:
  - hugo version
  - git submodule update --init --recursive
  - hugo -d public_html --gc --minify
  artifacts:
    paths:
    - public_html
  only:
  - master
deploy:
  stage: deploy
  image: alpine:latest
  tags:
  - private
  before_script:
    - apk update
    - apk add lftp
  script:
    - 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"
    - curl -d user=$PUSHUSER -d token=$PUSHTOKEN -d device=p20lite -d title="IFCONFIG" -d message="upload completed" https://api.pushover.net/1/messages.json
  only:
  - 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.