🌐 Detecting your location…
📢 Advertisement — Configure AdSense in Appearance → Customize → AdSense Settings

GitHub Actions CI/CD-Leitfaden 2026: Automatisieren Sie Tests und Bereitstellung

⏱️2 min read  ·  434 words
GitHub Actions CI/CD Guide 2026: Automate Testing and Deployment

GitHub-Aktionenist die am weitesten verbreitete CI/CD-Plattform im Jahr 2026. Kostenlos für öffentliche Repos und 2.000 Minuten/Monat für private Repos, automatisiert es Tests, Erstellung und Bereitstellung direkt aus Ihrem GitHub-Repository. Dieser Leitfaden deckt alles vom ersten Workflow bis zur Produktionsbereitstellung ab.

Kernkonzepte

  • Arbeitsablauf:YAML-Datei in.github/workflows/
  • Ereignis:Auslöser (Push, PR, Zeitplan, manuell)
  • Beruf:Satz von Schritten, die auf einem Läufer
  • ausgeführt werden Schritt:Einzelner Befehl oder Aktion
  • Läufer:VM, auf der Ihr Job ausgeführt wird (Ubuntu, Windows, macOS)

Erster Workflow: Test auf Push

# .github/workflows/test.yml
name: Test

on:
  push:
    branches: [main, develop]
  pull_request:
    branches: [main]

jobs:
  test:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4

      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: '3.12'

      - name: Install dependencies
        run: |
          pip install -r requirements.txt

      - name: Run tests
        run: pytest --cov=. --cov-report=xml

      - name: Upload coverage
        uses: codecov/codecov-action@v4

Matrix-Builds (Testen mehrerer Versionen)

jobs:
  test:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        python-version: ['3.11', '3.12', '3.13']

    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-python@v5
        with:
          python-version: ${{ matrix.python-version }}
      - run: pip install -r requirements.txt && pytest

Bereitstellung in der Produktion am Tag

name: Deploy

on:
  push:
    tags: ['v*']

jobs:
  deploy:
    runs-on: ubuntu-latest
    environment: production

    steps:
      - uses: actions/checkout@v4

      - name: Build Docker image
        run: |
          docker build -t myapp:${{ github.ref_name }} .
          docker tag myapp:${{ github.ref_name }} registry.example.com/myapp:latest

      - name: Push to registry
        run: |
          echo ${{ secrets.REGISTRY_TOKEN }} | docker login registry.example.com -u ci --password-stdin
          docker push registry.example.com/myapp:latest

      - name: Deploy via SSH
        uses: appleboy/ssh-action@v1
        with:
          host: ${{ secrets.SERVER_HOST }}
          username: deploy
          key: ${{ secrets.SSH_PRIVATE_KEY }}
          script: |
            docker pull registry.example.com/myapp:latest
            docker compose up -d

Caching-Abhängigkeiten (schnellere Builds)

      - name: Cache pip
        uses: actions/cache@v4
        with:
          path: ~/.cache/pip
          key: ${{ runner.os }}-pip-${{ hashFiles('requirements.txt') }}

      - name: Cache node_modules
        uses: actions/cache@v4
        with:
          path: node_modules
          key: ${{ runner.os }}-node-${{ hashFiles('package-lock.json') }}

Geheimnisse verwenden

Speichern Sie API-Schlüssel in GitHub-Einstellungen > Geheimnisse und Variablen > Aktionen. Zugriff in Workflows mit${{ secrets.MY_KEY }}. Geheimnisse werden niemals in Protokollen gedruckt.

      - name: Send deploy notification
        run: |
          curl -X POST ${{ secrets.SLACK_WEBHOOK }} \
            -H 'Content-type: application/json' \
            --data '{"text": "Deployed ${{ github.ref_name }}"}'

Fazit

GitHub Actions macht eine separate CI/CD-Infrastruktur überflüssig. Beginnen Sie mit einem einfachen Test-Workflow, fügen Sie Matrix-Builds hinzu und gehen Sie dann zur automatisierten Bereitstellung über, die durch Tags ausgelöst wird. Caching verkürzt die Bauzeit um 50–80 %.

✍️ Leave a Comment

Your email address will not be published. Required fields are marked *

🌐 Read in:🇬🇧 English🇩🇪 Deutsch🇧🇷 Português🇸🇦 العربية🇮🇳 हिन्दी🇧🇩 বাংলা