GitHub Actions لسير عمل التعلم الآلي

أساسيات GitHub Actions للتعلم الآلي

4 دقيقة للقراءة

GitHub Actions هي أكثر منصة CI/CD شيوعاً لمشاريع التعلم الآلي مفتوحة المصدر. دعنا نفهم مفاهيمها الأساسية وكيف تنطبق على سير عمل ML.

المفاهيم الأساسية

# .github/workflows/ml-pipeline.yml
name: ML Pipeline              # اسم سير العمل

on:                            # أحداث التحفيز
  push:
    branches: [main]
  pull_request:
    branches: [main]
  workflow_dispatch:           # تحفيز يدوي

jobs:                          # مجموعة المهام
  train:                       # اسم المهمة
    runs-on: ubuntu-latest     # نوع المشغّل
    steps:                     # خطوات متتالية
      - uses: actions/checkout@v4
      - name: Train model
        run: python train.py

شرح المكونات الرئيسية

المكون الغرض حالة استخدام ML
Workflow خط أنابيب أتمتة كامل تدفق التدريب → النشر الكامل
Job وحدة عمل مستقلة التدريب، التحقق، النشر
Step مهمة واحدة داخل job تشغيل سكربت، رفع مخرج
Runner خادم ينفذ المهمة CPU للاختبارات، GPU للتدريب
Action حزمة خطوات قابلة لإعادة الاستخدام checkout, setup-python

محفزات سير العمل للتعلم الآلي

محفزات مختلفة تناسب سيناريوهات ML مختلفة:

on:
  # تغييرات الكود
  push:
    branches: [main, develop]
    paths:
      - 'src/**'
      - 'configs/**'
      - 'requirements.txt'

  # التحقق من pull request
  pull_request:
    branches: [main]

  # إعادة التدريب المجدولة
  schedule:
    - cron: '0 2 * * 0'  # كل أحد الساعة 2 صباحاً

  # تحفيز يدوي مع مدخلات
  workflow_dispatch:
    inputs:
      model_version:
        description: 'Model version to train'
        required: true
        default: 'v1.0'

إعداد Python للتعلم الآلي

jobs:
  train:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: '3.11'
          cache: 'pip'  # تخزين حزم pip مؤقتاً

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

      - name: Run training
        run: python train.py

متغيرات البيئة والأسرار

تخزين البيانات الحساسة بأمان:

jobs:
  train:
    runs-on: ubuntu-latest
    env:
      # تكوين غير حساس
      MODEL_NAME: fraud-detector
      EXPERIMENT_NAME: ci-training

    steps:
      - name: Train with secrets
        run: python train.py
        env:
          # بيانات حساسة من الأسرار
          MLFLOW_TRACKING_URI: ${{ secrets.MLFLOW_URI }}
          AWS_ACCESS_KEY_ID: ${{ secrets.AWS_KEY }}
          AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET }}

إعداد الأسرار

  1. اذهب إلى المستودع → Settings → Secrets and variables → Actions
  2. انقر "New repository secret"
  3. أضف أسراراً مثل MLFLOW_URI، AWS_KEY

تبعيات المهام

التحكم في ترتيب التنفيذ:

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - run: pytest tests/

  train:
    needs: test  # انتظر اكتمال الاختبار
    runs-on: ubuntu-latest
    steps:
      - run: python train.py

  validate:
    needs: train  # انتظر التدريب
    runs-on: ubuntu-latest
    steps:
      - run: python validate.py

  deploy:
    needs: [train, validate]  # انتظر كليهما
    runs-on: ubuntu-latest
    steps:
      - run: ./deploy.sh

التنفيذ الشرطي

تشغيل خطوات بناءً على شروط:

steps:
  - name: Deploy to production
    if: github.ref == 'refs/heads/main'
    run: ./deploy-prod.sh

  - name: Deploy to staging
    if: github.ref == 'refs/heads/develop'
    run: ./deploy-staging.sh

  - name: Run expensive tests
    if: github.event_name == 'push'
    run: pytest tests/integration/ --slow

  - name: Notify on failure
    if: failure()
    run: |
      curl -X POST $SLACK_WEBHOOK \
        -d '{"text": "Pipeline failed!"}'

رفع وتنزيل المخرجات

مشاركة الملفات بين المهام:

jobs:
  train:
    runs-on: ubuntu-latest
    steps:
      - name: Train model
        run: python train.py --output models/

      - name: Upload model artifact
        uses: actions/upload-artifact@v4
        with:
          name: trained-model
          path: models/
          retention-days: 30

  deploy:
    needs: train
    runs-on: ubuntu-latest
    steps:
      - name: Download model
        uses: actions/download-artifact@v4
        with:
          name: trained-model
          path: models/

      - name: Deploy model
        run: ./deploy.sh models/model.pkl

مخرجات سير العمل

تمرير البيانات بين المهام:

jobs:
  train:
    runs-on: ubuntu-latest
    outputs:
      accuracy: ${{ steps.train.outputs.accuracy }}
      model_path: ${{ steps.train.outputs.model_path }}

    steps:
      - name: Train and output metrics
        id: train
        run: |
          python train.py
          echo "accuracy=$(cat metrics.txt | grep accuracy)" >> $GITHUB_OUTPUT
          echo "model_path=models/model.pkl" >> $GITHUB_OUTPUT

  validate:
    needs: train
    runs-on: ubuntu-latest
    steps:
      - name: Check accuracy threshold
        run: |
          if (( $(echo "${{ needs.train.outputs.accuracy }} < 0.85" | bc -l) )); then
            echo "Accuracy below threshold!"
            exit 1
          fi

مثال سير عمل ML كامل

name: ML Pipeline

on:
  push:
    branches: [main]
  workflow_dispatch:

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-python@v5
        with:
          python-version: '3.11'
          cache: 'pip'
      - run: pip install -r requirements.txt
      - run: pytest tests/unit/

  train:
    needs: test
    runs-on: ubuntu-latest
    outputs:
      accuracy: ${{ steps.metrics.outputs.accuracy }}
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-python@v5
        with:
          python-version: '3.11'
          cache: 'pip'
      - run: pip install -r requirements.txt
      - run: python train.py
      - id: metrics
        run: echo "accuracy=$(cat metrics.json | jq .accuracy)" >> $GITHUB_OUTPUT
      - uses: actions/upload-artifact@v4
        with:
          name: model
          path: models/

  validate:
    needs: train
    runs-on: ubuntu-latest
    steps:
      - name: Check accuracy
        run: |
          echo "Model accuracy: ${{ needs.train.outputs.accuracy }}"

الرؤية الرئيسية: ابدأ بسير عمل بسيط، ثم أضف التعقيد. لا تحتاج كل ميزة من اليوم الأول.

التالي، سنستكشف خطوط أنابيب التدريب مع matrix builds وGPU runners. :::

اختبار

الوحدة 2: GitHub Actions لسير عمل التعلم الآلي

خذ الاختبار