Giriş: Repo-nun İçində CI/CD

GitHub Actions — GitHub-a daxil olan avtomatlaşdırma platformasıdır. Repo-da .github/workflows/ qovluğuna YAML faylı qoyursunuz və hər push, PR, cədvəl və ya əl ilə başlatma zamanı GitHub-un virtual maşınlarında (runner) əmrləriniz işləyir. Açıq repo-lar üçün pulsuzdur.

Event

Workflow-u nə başladır: push, pull_request, schedule, workflow_dispatch.

Runner

ubuntu-latest, windows-latest, macos-latest və ya öz serveriniz (self-hosted).

Action

Hazır addım: actions/checkout, actions/setup-python — Marketplace-də minlərlə.

1. Workflow Quruluşu

SəviyyəNədirXüsusiyyət
WorkflowBir YAML faylıBir və ya bir neçə job
JobBir runner-də işləyən addımlar qrupuJob-lar defolt olaraq paralel; needs: ilə ardıcıl
StepBir əmr (run) və ya action (uses)Eyni job-un step-ləri eyni fayl sistemini paylaşır

2. İlk Workflow: Python Testləri

# .github/workflows/ci.yml
name: CI

on:
  push:
    branches: [main]
  pull_request:

jobs:
  test:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        python: ["3.11", "3.12", "3.13"]     # 3 paralel job
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-python@v5
        with:
          python-version: ${{ matrix.python }}
          cache: pip
      - run: pip install -r requirements.txt
      - run: ruff check .
      - run: pytest -q

  build:
    needs: test                       # yalnız testlər keçəndə
    if: github.ref == 'refs/heads/main'
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - run: docker build -t myapp:${{ github.sha }} .

${{ ... }} — expression sintaksisi: github.sha, github.ref, matrix.*, secrets.* kontekstlərinə çatmaq üçün.

3. Secret-lər və İcazələr

Repository secrets: Settings → Secrets and variables → Actions. Log-da avtomatik *** ilə gizlədilir.

Environments: production mühitinə ayrıca secret-lər və məcburi reviewer.

GITHUB_TOKEN: hər run üçün avtomatik yaranan token; permissions: ilə minimuma endirin.

OIDC: id-token: write ilə AWS/Azure-a secret-siz giriş.

permissions:
  contents: read

jobs:
  deploy:
    runs-on: ubuntu-latest
    environment: production          # təsdiq gözləyir
    steps:
      - uses: actions/checkout@v4
      - name: Serverə deploy
        env:
          SSH_KEY: ${{ secrets.DEPLOY_SSH_KEY }}
        run: |
          install -m 600 /dev/null ~/.ssh_key && echo "$SSH_KEY" > ~/.ssh_key
          rsync -az -e "ssh -i ~/.ssh_key -o StrictHostKeyChecking=accept-new" ./site/ deploy@web01:/var/www/site/

Fork-dan gələn PR-lar secret-lərə çata bilmir — bu qəsdəndir. pull_request_target event-ini yad kodu işlətmək üçün istifadə etməyin. Üçüncü tərəf action-ları tam commit SHA ilə sabitləyin.

4. Faydalı İmkanlar

on:
  schedule:
    - cron: "0 3 * * 1"          # hər bazar ertəsi 03:00 UTC
  workflow_dispatch:              # Actions tab-ında "Run workflow" düyməsi
    inputs:
      env:
        type: choice
        options: [staging, production]

concurrency:                      # eyni branch-da köhnə run-ı ləğv et
  group: deploy-${{ github.ref }}
  cancel-in-progress: true

jobs:
  report:
    runs-on: ubuntu-latest
    timeout-minutes: 10
    steps:
      - run: ./generate-report.sh > report.html
      - uses: actions/upload-artifact@v4       # nəticə faylını saxla
        with: { name: weekly-report, path: report.html }
      - if: failure()                          # yalnız xəta olanda
        run: echo "::error::Hesabat yaradılmadı"

5. Real Nümunə: Bu Portalın Workflow-u

Bu sayt GitHub Pages-dədir və .github/workflows/site.yml hər push-da belə işləyir:

  1. python3 _dev/build.py check — ikonlar, kataloq, quiz sualları, daxili linklər.

  2. Build yenidən işlədilir və git diff boş olmalıdır — yaradılmış fayllar mənbə ilə uyğun gəlir.

  3. Sayt fayllarından Pages artefaktı yaradılır.

  4. main-də hər şey yaşıldırsa — actions/deploy-pages ilə dərc.

Ayrıca həftəlik workflow xarici linkləri yoxlayır, amma deploy-u bloklamır — kənar saytın müvəqqəti xətası saytın dərcini dayandırmamalıdır.