Building a self-hosted CI/CD pipeline
One of the things I wanted to sort out alongside the blog migration was proper CI/CD. Before this, deploying anything meant SSHing into a server, pulling a new image, and restarting a container by hand. It does the job but get’s boring quickly.
Starting point
The infrastructure I had going into this:
- Gitea running on its own LXC, serving as my self-hosted git host
- Nginx Proxy Manager on a separate LXC handling reverse proxy and SSL for everything
- One Portainer instance, managing all my Docker stacks
Everything lived in one Portainer environment, which is fine until you want to separate concerns. Media apps, dev tooling and more “prod” projects like a blog. All sharing the same host felt like it was going to cause problems eventually.
Gitea Actions and the runner
Gitea has a built-in Actions system compatible with GitHub Actions syntax. I was already running gitea/act_runner as a Docker container on the media-docker host, but it had never been used that much.
Getting it properly configured took a bit of work. The runner defaults to pulling actions like actions/checkout from GitHub, and hence we are capped by GitHub rate limit and requires a live network which I may not always have.
I decide to mirror the actions I needed into a Gitea org called actions and configure the runner to look there first. The runner config has a github_mirror option for exactly this, though in practice I ended up using fully qualified URLs in the workflow files to make it unambiguous and because it wasn’t working as well as I expected.
The workflow
Each repo gets a .gitea/workflows/ci.yml that calls a reusable action living in a dedicated deploy-action repo. The project-level workflow just declares four things: the base image name, the port, the subdomain, and the target Portainer environment. All the pipeline logic lives in the shared action.
When a push hits the main branch the pipeline does the following:
- Builds a Docker image and pushes it to Gitea’s built-in container registry
- Authenticates with Portainer and creates or redeploys the stack
- Authenticates with NPM and creates or updates the proxy host with the right SSL cert
Everything is driven by the repo config, so no manual steps anywhere
The Portainer and NPM steps use their respective APIs with curl and jq.
Portainer environments and the agent
The single Portainer instance became 3 separated environments. One for my media apps, arr stack, immich, drive-like alternatives, etc… A dev environment for testing projects and running my gitea runners and a Prod environment for things a bit more serious like this blog or my wife’s site.
Portainer agents make this easy to manage. Instead of opening Portainer’s full API to another host, you run a lightweight agent container that the main Portainer instance connects to. The agent on the dev host registers itself and from Portainer’s perspective it looks the same as a local environment.
NPM routing per environment
The tricky part of supporting multiple environments is that each one has a different IP, and NPM needs to route to the right one. The deploy action takes a portainer_environment input, and the NPM step uses that to pick the correct forwarding IP from a set of Gitea variables named PORTAINER_DEV_IP, PORTAINER_MEDIA_IP, and PORTAINER_PROD_IP. Changing which host a service runs on is now a one-line change in the workflow file.
What it looks like now
The ci.yaml file may look like the following
1name: Build and Deploy
2
3on:
4 push:
5 branches: [main]
6
7jobs:
8 build-and-deploy:
9 runs-on: ubuntu-latest
10 steps:
11 - name: Checkout
12 uses: https://domain/actions/checkout@v3
13
14 - name: Deploy
15 uses: https://domain/nax/deploy-action@main
16 with:
17 image_name: nacho-blog
18 port: 3001
19 subdomain: nacho.domain
20 portainer_environment: prod
21 portainer_stack_name: nacho-blog
22 git_token: ${{ secrets.GIT_TOKEN }}
23 portainer_password: ${{ secrets.PORTAINER_PASSWORD }}
24 npm_password: ${{ secrets.NPM_PASSWORD }}
It is not a perfect setup. There are rough edges, the runner occasionally needs a nudge, and the Docker network pool on the runner host needed expanding after running out of subnets. But for a homelab it is more than good enough, and adding a new service now takes minutes instead of manual work across multiple UIs.