kb.erickguedes.com
GitHub Actions: CI/CD Moderno

Self-Hosted Runners

Aula 4 de 5

Quando Usar Runners Próprios

Runners GitHub (ubuntu-latest):
├── Gratuito: 2000 min/mês (público: ilimitado)
├── SO: Ubuntu, Windows, macOS
├── Padrão: 2-core CPU, 7GB RAM
└── Efêmero: sempre limpo

Self-hosted Runner:
├── Hardware personalizado (GPU, RAM, CPU)
├── Rede interna (acesso a recursos corporativos)
├── Custo fixo (não por minuto)
└── Armazenamento persistente (caching local)

Setup Runner

# 1. GitHub UI: Settings > Actions > Runners > New self-hosted runner
# 2. Escolher SO e arquitetura
# 3. Executar no servidor:

mkdir actions-runner && cd actions-runner
curl -o actions-runner-linux-x64-2.317.0.tar.gz \
  -L https://github.com/actions/runner/releases/download/v2.317.0/actions-runner-linux-x64-2.317.0.tar.gz
tar xzf actions-runner-linux-x64-2.317.0.tar.gz

# Configurar (token único por runner)
./config.sh --url https://github.com/minha-org/meu-repo \
  --token ABCDEF1234567890 \
  --labels gpu,linux,large \
  --name runner-prod-1 \
  --work _work

# Instalar como serviço
sudo ./svc.sh install
sudo ./svc.sh start
sudo ./svc.sh status

Workflow com Runner Específico

name: Deploy on-premise

jobs:
  deploy:
    runs-on: [self-hosted, linux, production]
    steps:
      - uses: actions/checkout@v4
      - run: ./deploy.sh

  ml-training:
    runs-on: [self-hosted, gpu]
    steps:
      - run: nvidia-smi
      - run: python train.py

Runner Groups

# Organizar runners em grupos (Settings > Actions > Runner groups)
# Grupos: Production, Development, GPU, Windows
# Job usa label do grupo:
jobs:
  test:
    runs-on: [self-hosted, production]

Containerização de Runners

# Runner em container (Docker)
docker run -d \
  --name github-runner \
  -e REPO_URL=https://github.com/org/repo \
  -e RUNNER_TOKEN=ABC123 \
  -e RUNNER_LABELS=container,docker \
  -v /var/run/docker.sock:/var/run/docker.sock \
  myoung34/github-runner:latest

# Escalar com Docker Compose / Kubernetes

Escalabilidade

# Escalonamento automático com actions-runner-controller (K8s)
apiVersion: actions.summerwind.dev/v1alpha1
kind: HorizontalRunnerAutoscaler
metadata:
  name: runner-autoscaler
spec:
  scaleTargetRef:
    name: runner-deployment
  minReplicas: 2
  maxReplicas: 20
  metrics:
    - type: TotalNumberOfQueuedAndInProgressWorkflowRuns
      repositoryNames:
        - meu-app

Segurança

# Runner NUNCA deve ter acesso irrestrito
# ❌ Ruim: runner com sudo sem senha e acesso a produção
# ✅ Bom: runner com acesso apenas ao necessário

# Actions recomendadas para runners self-hosted:
# - actions/checkout@v4
# - actions/upload-artifact@v4

# ⚠️ Risco: PRs de forks rodam no seu runner!
# Solução: actions/runner-groups pra produção
# Segurança: não rodar PRs de forks em self-hosted
on:
  pull_request_target:  # em vez de pull_request
    types: [labeled]

  # Ou verificar se é do próprio repositório
jobs:
  safe:
    if: github.event.pull_request.head.repo.full_name == github.repository
    runs-on: [self-hosted]

Self-hosted runners são ideais para: acesso a rede interna, hardware especializado (GPU), menor custo em escala e caches persistentes. Exigem mais segurança e manutenção.