DevOps & Tools

Lightweight CI/CD with GitHub Actions and a Self-Hosted Runner

Build and deploy on your own server with a self-hosted runner: setup, a sample workflow and the security warnings that really matter.

Lightweight CI/CD with GitHub Actions and a Self-Hosted Runner
Table of contents
  1. Setting up the runner
  2. A sample workflow
  3. A serious security warning
  4. Sources

GitHub Actions is free for public repositories and runs builds on GitHub’s machines, but there is another option: your own runner on your own server, so builds and deploys happen right where the site runs.

Setting up the runner

In the repository, go to Settings → Actions → Runners → New self-hosted runner. GitHub shows the exact commands there (download, configure and a one-time token):

./config.sh --url https://github.com/ORG/REPO --token XXXXXXXX
./run.sh              # trial run
sudo ./svc.sh install # install as a permanent service
sudo ./svc.sh start

A sample workflow

The file .github/workflows/deploy.yml:

name: deploy
on:
  push:
    branches: [main]
jobs:
  build-and-deploy:
    runs-on: self-hosted
    steps:
      - uses: actions/checkout@v4
      - run: docker compose build
      - run: docker compose up -d
      - run: docker image prune -f

Every push to main is built and started on your own server, with no images shipped between machines.

A serious security warning

Every job means running arbitrary code on your server. Three lines of defence:

  1. Keep the repository private, and give write access only to collaborators you trust.
  2. Don’t run the runner as root; membership of the docker group is enough to build and run containers.
  3. Never attach a self-hosted runner to a public repository: anyone who opens a pull request gets their code executed on your server. GitHub’s own documentation warns about exactly this risk.

Sources

  • Self-hosted runner documentation: docs.github.com/actions/hosting-your-own-runners

About the author

Sina Farahmand

DevOps and back-end developer, into automation, CI/CD and command-line tools. If he has to do something twice, he writes a script for it.

All articles by Sina Farahmand

Comments

No comments yet. For technical questions, include the details (OS version, the exact error and the command you ran) to get a precise answer.

Leave a comment