Workflows e Eventos
Aula 1 de 5
O que é GitHub Actions?
GitHub Actions automatiza builds, testes e deploys diretamente do GitHub. Workflows são arquivos YAML em .github/workflows/.
# .github/workflows/ci.yml
name: CI Pipeline
on:
push:
branches: [main, develop]
pull_request:
branches: [main]
workflow_dispatch: # execução manual
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: echo "Hello, Actions!"
Estrutura do Workflow
name: Deploy
run-name: "Deploy ${{ github.ref_name }} by @${{ github.actor }}"
on:
push:
tags:
- 'v*'
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- run: npm ci && npm run lint
test:
needs: [lint]
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [18, 20, 22]
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
- run: npm ci && npm test
build:
needs: [test]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: npm ci && npm run build
- uses: actions/upload-artifact@v4
with:
name: build
path: dist/
deploy:
needs: [build]
runs-on: ubuntu-latest
environment: production
concurrency: production
steps:
- uses: actions/download-artifact@v4
with:
name: build
path: dist
- run: echo "Deploying..."
Eventos Comuns
on:
push: # push em qualquer branch
branches:
- main
- 'stable/*'
tags:
- 'v*'
paths:
- 'src/**'
- '!docs/**'
pull_request: # PR aberto/sincronizado
types: [opened, synchronize, reopened]
schedule: # cron
- cron: '0 2 * * 1' # toda segunda 2h
workflow_run: # após outro workflow
workflows: ["CI"]
types:
- completed
repository_dispatch: # API externa
types: [deploy-prod]
release:
types: [published]
Matrix Builds
jobs:
test:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
node: [18, 20, 22]
include:
- os: ubuntu-latest
node: 20
coverage: true
exclude:
- os: macos-latest
node: 18
fail-fast: false # continua se uma matrix falhar
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node }}
- run: npm ci && npm test
- if: matrix.coverage
run: npm run coverage
Contexts e Expressões
jobs:
context-demo:
runs-on: ubuntu-latest
steps:
- run: |
echo "Event: ${{ github.event_name }}"
echo "Branch: ${{ github.ref_name }}"
echo "SHA: ${{ github.sha }}"
echo "Actor: ${{ github.actor }}"
echo "Repo: ${{ github.repository }}"
shell: bash
- if: github.event_name == 'pull_request'
run: echo "PR #${{ github.event.number }}"
- if: startsWith(github.ref, 'refs/tags/v')
run: echo "Release ${{ github.ref_name }}"
- if: failure()
run: echo "Passo anterior falhou"
Workflows = eventos → jobs (paralelos) → steps (sequenciais). Matrix testa múltiplas configurações. Contexts (github., env., secrets.*) fornecem dados dinâmicos.