github-actions
Automate CI/CD with GitHub Actions: workflows, jobs, matrices, caching, artifacts, and reusable workflows. Use for build, test, and deploy pipelines.
GitHub Actions
Automating build, test, and deploy pipelines with GitHub Actions.
Quick Start
# Add .github/workflows/ci.yml and push
# Workflow runs on push/PR automaticallyWhen to Use
- Running tests and linting on every PR
- Building and publishing artifacts
- Deploying to cloud, registries, and Pages
- Scheduled jobs and dependency updates
Best Practices
Workflow Design
- Split CI/CD into separate workflows or jobs
- Use job `needs` for ordering; `matrix` for version combos
- Cache dependencies (`actions/cache`) for speed
- Keep secrets in repo/org secrets; never in YAML
Security
- Pin action versions (`@v4`) or commit SHAs
- Use `permissions:` minimal scope per job
- Sanitize untrusted inputs (PRs from forks)
- Avoid `pull_request_target` unless you handle secrets carefully
Efficiency
- Trigger only on relevant paths (`on: push: paths:`)
- Use concurrency to cancel stale runs
- Reuse workflows via `workflow_call`
- Upload artifacts only when needed
Dependencies
# None required — GitHub-hosted runners have common tools.
# Pin versions:
# actions/checkout@v4, actions/setup-python@v5, actions/cache@v4Examples
name: CI
on:
push:
branches: [main]
pull_request:
permissions:
contents: read
jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.11", "3.12"]
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
cache: pip
- run: pip install -e ".[dev]"
- run: ruff check .
- run: pytest tests/ -q# Deploy on tag with permissions
name: Deploy
on:
push:
tags: ["v*"]
permissions:
contents: write
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: echo "deploying ${{ github.ref_name }}"
env:
TOKEN: ${{ secrets.DEPLOY_TOKEN }}# Reusable workflow
name: Quality
on:
workflow_call:
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: npm ci && npm run lint# Scheduled + caching
name: Nightly
on:
schedule:
- cron: "0 2 * * *"
jobs:
nightly:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/cache@v4
with:
path: ~/.cache/pip
key: pip-${{ hashFiles('requirements.txt') }}
- run: pip install -r requirements.txtStep-by-Step
1. Define the trigger (`on`) and job breakdown.
2. Checkout code and set up the language runtime.
3. Cache dependencies for speed.
4. Run lint, typecheck, and tests in parallel jobs where possible.
5. Add a build/publish step gated on success.
6. Add deployment job with `needs` and environment protection.
7. Set `permissions` minimal; store secrets in repo settings.
8. Review Actions logs; add status badges to the README.
Validation
1. Workflow runs green on push and PR
2. Matrix covers the supported versions
3. Artifacts/secrets used correctly (no leaks in logs)
4. Caching measurably speeds up runs
5. Cancelled stale runs via concurrency
Troubleshooting
- "Permission denied" on push/deploy: check `permissions` and token scope.
- Secrets missing: define them in repo/org settings.
- Flaky runs: pin action versions and runner images.
GitHub Actions (ГитХаб Экшенс)
Автоматизация build, test и deploy пайплайнов через GitHub Actions.
Быстрый старт
# Добавьте .github/workflows/ci.yml и запушьте
# Workflow запускается автоматически при push/PRКогда использовать
- Запуск тестов и линтеров на каждом PR
- Сборка и публикация артефактов
- Деплой в облако, реестры и Pages
- Scheduled-задачи и обновление зависимостей
Лучшие практики
Дизайн workflow
- Разделяйте CI/CD на отдельные workflow или jobs
- Для порядка используйте `needs`; для комбо версий — `matrix`
- Кэшируйте зависимости (`actions/cache`) для скорости
- Секреты храните в repo/org secrets; никогда не в YAML
Безопасность
- Пиньте версии действий (`@v4`) или SHA коммитов
- Используйте `permissions:` минимальный скоуп на job
- Санизируйте недоверенный ввод (PR из форков)
- Избегайте `pull_request_target`, если не работаете с секретами аккуратно
Эффективность
- Триггерите только по релевантным путям (`on: push: paths:`)
- Используйте concurrency для отмены устаревших запусков
- Переиспользуйте workflow через `workflow_call`
- Загружайте артефакты только когда нужно
Зависимости
# Ничего не нужно — на GitHub-hosted раннерах есть общие инструменты.
# Пиньте версии:
# actions/checkout@v4, actions/setup-python@v5, actions/cache@v4Примеры
name: CI
on:
push:
branches: [main]
pull_request:
permissions:
contents: read
jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.11", "3.12"]
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
cache: pip
- run: pip install -e ".[dev]"
- run: ruff check .
- run: pytest tests/ -q# Деплой по тегу с permissions
name: Deploy
on:
push:
tags: ["v*"]
permissions:
contents: write
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: echo "deploying ${{ github.ref_name }}"
env:
TOKEN: ${{ secrets.DEPLOY_TOKEN }}# Переиспользуемый workflow
name: Quality
on:
workflow_call:
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: npm ci && npm run lint# Scheduled + кэш
name: Nightly
on:
schedule:
- cron: "0 2 * * *"
jobs:
nightly:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/cache@v4
with:
path: ~/.cache/pip
key: pip-${{ hashFiles('requirements.txt') }}
- run: pip install -r requirements.txtПошаговое руководство
1. Определите триггер (`on`) и разбивку на jobs.
2. Сделайте checkout кода и настройте рантайм языка.
3. Кэшируйте зависимости для скорости.
4. Запускайте lint, typecheck и тесты параллельно, где возможно.
5. Добавьте шаг сборки/публикации после успеха.
6. Добавьте job деплоя с `needs` и защитой environment.
7. Выставьте `permissions` минимально; секреты — в настройках репо.
8. Смотрите логи Actions; добавляйте status badges в README.
Валидация
1. Workflow зелёный на push и PR
2. Matrix покрывает поддерживаемые версии
3. Артефакты/секреты используются корректно (без утечек в логи)
4. Кэш измеримо ускоряет запуски
5. Устаревшие запуски отменяются через concurrency
Устранение неполадок
- "Permission denied" на push/deploy: проверьте `permissions` и скоуп токена.
- Секреты не найдены: определите их в настройках repo/org.
- Flaky запуски: пиньте версии действий и образы раннеров.