Auto deploy Docker-Compose app from Github

November 14, 2026

Build image and push to private Github registry

Generate a Personal Access Token (PAT) with write:packages and read:packages permissions.

Add CI_REPOSITORY_TOKEN to your repository secrets by navigating to Settings > Secrets and variables > Actions > New repository secret.

Create a new workflow file .github/workflows/deploy.yml to build and push Docker images to GitHub Container Registry (GHCR) using BuildKit in a rootless and daemonless mode.

name: Deploy
on:
  push:
    tags:
      - '*'
jobs:
  build-image:
    runs-on: self-hosted
    steps:
      - name: Extract Docker image metadata
        id: meta
        uses: docker/metadata-action@v6
        with:
          images: ghcr.io/${{ github.repository }}

      - name: Login to GHCR
        if: github.event_name != 'pull_request'
        uses: docker/login-action@v4
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.CI_REPOSITORY_TOKEN }}

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v4

      - name: Build and push Docker image
        uses: docker/build-push-action@v7
        with:
          push: ${{ github.event_name != 'pull_request' }}
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}
          annotations: ${{ steps.meta.outputs.annotations }}

Install a webhook on the server

On the server, where the app is running install a small webhook server. This server is triggered from GitHub CI and deploys the app container.

apt-get update
apt-get install webhook

Check status of webhook

sudo systemctl status webhook

It will show inactive because You installed the Ubuntu/Debian webhook package, and its packaged systemd service expects /etc/webhook.conf to exist.

sudo nano /etc/webhook.conf

For your deployment:

[
  {
    "id": "deploy",
    "execute-command": "/opt/deploy.sh",
    "command-working-directory": "/opt/myapp",
    "trigger-rule": {
      "match": {
        "type": "value",
        "value": "YOUR_SECRET_TOKEN",
        "parameter": {
          "source": "url",
          "name": "token"
        }
      }
    }
  }
]

Then

sudo chmod 600 /etc/webhook.conf
sudo systemctl restart webhook
sudo systemctl status webhook

The webhook should be active at this point.

Create /opt/deploy.sh file with the instruction to pull image and deploy container.

set -euo pipefail

cd /opt/myapp

echo "Logging in to GitHub Container Registry..."
echo "$GHCR_TOKEN" | docker login ghcr.io \
  --username "$GHCR_USERNAME" \
  --password-stdin

echo "Pulling latest images..."
docker compose pull

echo "Applying database migrations..."
docker compose run --rm myapp python manage.py migrate --noinput

echo "Starting application..."
docker compose up -d --remove-orphans

echo "Checking container status..."
docker compose ps

echo "Deployment completed."

docker logout ghcr.io

Make it executable

sudo chmod +x /opt/deploy.sh

Github credentials are supplied by an env file.

sudo mkdir -p /etc/myapp
sudo nano /etc/myapp/ghcr.env

Put the contentes in the /etc/myapp/ghcr.env file

GHCR_USERNAME=username
GHCR_TOKEN=github_pat_xxxxxxxxxxxxxxxxx

Create a directory

sudo mkdir -p /etc/systemd/system/webhook.service.d

Then create a file inside the directory

sudo nano /etc/systemd/system/webhook.service.d/override.conf

Put the content in the file. Make sure port 9000 is not allowed from the public internet in your VM firewall.

Important: Do not run this before enabling the firewall.

[Service]
ExecStart=
ExecStart=/usr/bin/webhook -hooks /etc/webhook.conf -verbose -ip 0.0.0.0 -port 9000
EnvironmentFile=/etc/myapp/ghcr.env

Restart the webhook

sudo systemctl daemon-reload
sudo systemctl restart webhook

Now modify your routes.yaml file and add the service to connect to the webhook.

http:
  routers:
    myapp:
      rule: "Host(`yourdomain.com`)"
      entryPoints:
        - websecure
      service: app
      tls:
        certResolver: letsencrypt

    deploy-webhook:
      rule: "Host(`yourdomain.com`) && PathPrefix(`/hooks/`)"
      entryPoints:
        - websecure
      service: webhook-service
      tls:
        certResolver: letsencrypt

  services:
    app:
      loadBalancer:
        servers:
          - url: "http://myapp:8000"

    webhook-service:
      loadBalancer:
        servers:
          - url: "http://host.docker.internal:9000"

Also add extra_hosts config in your Traefik config in the Docker compose file. Without this it cannot reach the webhook running on local machine.

services:
  traefik:
    extra_hosts:
      - "host.docker.internal:host-gateway"

After that update your CI file to trigger the webhook after an image is built.

This WEBHOOK_DEPLOY_TOKEN value is set as secret value from /etc/webhook.conf file.

  trigger-webhook:
    runs-on: self-hosted
    container:
      image: ubuntu@sha256:2edbbc5dc405e9612ba3584ce95480277e3eb374407b5505fe26f17df77c7dbc
    needs: build-image

    steps:
      - name: Install curl
        run: |
          apt-get update
          apt-get install -y --no-install-recommends curl ca-certificates
          rm -rf /var/lib/apt/lists/*

      - name: Trigger webhook
        run: |
          curl --fail-with-body --request POST \
            --url "https://yourdomain.com/hooks/deploy" \
            --header "Content-Type: application/json" \
            --data '{"token":"${{ secrets.WEBHOOK_DEPLOY_TOKEN }}"}'