Automating my site update with gitlab ci/cd
Published on: 23.11.2023
I like the indieweb moto "manual until it hurts" but there's a certain joy to automating things from time to time. In this blogpost I will go through my simple setup of updating my 11ty site hosted on neocities via gitlab ci/cd.
Neocities is a hosting service for static websites, with both free and paying options. It also works as a social network where user can follow updates of each others blogs. What's special about it is the web nostaliga vibe of most pages that it hosts. It aims to bring back a smaller and more personal web of blogs and webpages where users can craft custom htlm and css to express themselves in their unique way, free from advertisement and tracking algorithms.
In addition to the web interface, it offers an easy CLI to update the site from your command line. I've been using it to update my 11ty-generated static site and it's pretty easy, straightforward and fast: cd into your build folder and run neocities push .
, which will recursively upload the local directory to your site. The only thing that bothered me with the flow was that i then had to commit the code separately to update my gitlab repo. Wouldn't it be nice to do both in one step? Searching online I found mostly configurations for deploying via github actions, and only this one for doing it on gitlab, but it wasn't working for my setup so I had to adapt it. Now I'm sharing the new process for anyone who may need it in the future.
First, the pipeline should do two things: build the site and deploy it to neocities. We describe it in our .gitlab-ci.yml file like this:
# declare stages of the pipeline
stages:
- build
- deploy
The build step is a standard 11ty build, where we use a node image to install dependencies, and run the build script (defined as npx @11ty/eleventy
in our package.json). In the end we store the build files (in my case inside a folder called 'public') in an artifact, which we'll need to reference in the next step.
build:
stage: build
image: node:20.10.0
before_script:
# install dependencies
- npm install
script:
# build the site
- npm run build
# use artifacts to make build available in the next stage
artifacts:
paths:
- public
For the deploy step we need to push the new build to our neocities host. For this we need to use a token, which we can get from our site setting in our neocities account. Copy the token and create a protected, masked, file type ci/cd variable for your repo at Settings > CI/CD Settings > Variables. Let's name our varialbe NEOCITIES_TOKEN and set the environment to 'neocities'. Now back to our .gitlab-ci.yml.
The CLI is written in Ruby, and we install it with gem install neocities
. After installing and declaring the environment, we have to read the environment variable we just stored, move it to our gem's config and use it to update our site. Jump in the artifact directory that was created in the first step and run neocities push .
. Code for this stage below:
neocities:
stage: deploy
image: ruby:3.0.2
before_script:
- gem install neocities
# select neocities environment
environment: neocities
script:
# Place user-provided token
- if [ -z "$NEOCITIES_TOKEN" ];
then echo No neocities token found && false;
else mkdir -p $HOME/.config/neocities && mv $NEOCITIES_TOKEN $HOME/.config/neocities/config; fi
# change directory into the build artifact
- cd public
# publish new site
- neocities push .
rules:
# This ensures that only pushes to the default branch will trigger a deploy
- if: $CI_COMMIT_REF_NAME == $CI_DEFAULT_BRANCH
Now everytime i push new code to my main branch it triggers a site update. Like this I don't risk having an outdated repo, and can update both the repo and website in one step. Full code for the ci file below, and keep making the web yours.
stages:
- build
- deploy
build:
stage: build
image: node:20.10.0
before_script:
- npm install
script:
- npm run build
artifacts:
paths:
- public
neocities:
stage: deploy
image: ruby:3.0.2
before_script:
- gem install neocities
environment: neocities
script:
- if [ -z "$NEOCITIES_TOKEN" ]; then echo No neocities token found && false; else mkdir -p $HOME/.config/neocities && mv $NEOCITIES_TOKEN $HOME/.config/neocities/config; fi
- cd public
- neocities push .
rules:
- if: $CI_COMMIT_REF_NAME == $CI_DEFAULT_BRANCH
