Skip to content

Latest commit

 

History

History
217 lines (171 loc) · 4.51 KB

git-cicd.md

File metadata and controls

217 lines (171 loc) · 4.51 KB

Git & CI/CD — Cheatsheet

Modelo mental (3 áreas)

Working Directory  →  Staging Area  →  Repository (commits)
     (editás)           (git add)         (git commit)
  • git status = el "pwd" de Git (antes y después de cada acción)
  • git diff = working vs last commit
  • git diff --staged = staging vs last commit (lo que irá al commit)

Git fundamentals

git init
git status
git diff
git diff --staged
git add <archivo>              # selectivo (commits atómicos)
git add -A                     # todo (cuidado con secretos)
git commit -m "fix: msg claro"
git log --oneline --graph

.gitignore (ops)

.env
*.pem
*.key
*.log
logs/
build/
target/
node_modules/
.DS_Store

Nunca git add -A sin mirar git status.

Branches

git branch
git checkout -b feature/x      # crear + cambiar
git switch feature/x           # moderno
git checkout main
git branch -d feature/x        # solo si mergeada
git branch -D feature/x        # forzado

Estrategia típica: main estable + feature/* + hotfix desde tag/release.

Merge

git checkout main
git merge feature/x
Tipo Cuándo
Fast-forward main no avanzó → mueve el puntero
3-way ambas branches avanzaron → merge commit
Conflicto misma línea en ambos lados → resolver a mano

Resolver conflicto (paso a paso)

  1. git merge feature/x → conflicto
  2. Abrí el archivo: buscá <<<<<<< ======= >>>>>>>
  3. Dejá la versión correcta
  4. git add <archivo>
  5. git commit (cierra el merge)

Rebase vs merge

Merge Rebase
Historia Preserva ramas (merge commit) Lineal (reescribe commits)
Cuándo Integrar features a main Limpiar branch local antes de PR
Cuidado Más ruidoso Nunca rebasees commits ya pusheados compartidos
git checkout feature/x
git rebase main                # solo en branches tuyas

Cherry-pick (hotfixes)

git cherry-pick <sha>          # aplica UN commit a la branch actual

Útil: llevar un fix a main sin mergear toda la feature.

Tags (SemVer)

git tag -a v1.0.0 -m "Release 1.0.0"
git tag
git show v1.0.0
git diff v1.0.0 v1.1.0
git push origin v1.0.0
git push --tags
  • PATCH v1.0.1 — bugfix
  • MINOR v1.1.0 — feature compatible
  • MAJOR v2.0.0 — breaking change

Remote & GitHub CLI

git remote -v
git remote add origin <url>
git push -u origin main
git pull origin main

gh auth status
gh repo create
gh pr create --title "..." --body "..."
gh pr list
gh run list
gh run view <id> --log

GitHub Actions — anatomía

name: CI
on:
  push:
    branches: [main]
  pull_request:
    branches: [main]
  # releases:
  # push:
  #   tags: ['v*']

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - run: ./mvnw test          # o npm test
  build:
    needs: test                   # fail-fast: solo si test OK
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - run: docker build -t app:${{ github.sha }} .

Archivos en: .github/workflows/*.yml

Secrets

  • Settings → Secrets and variables → Actions
  • Uso: ${{ secrets.MY_TOKEN }}
  • Nunca hardcodear passwords en YAML ni en Dockerfile
  • Evitar echo de secrets en logs

Matrix builds

strategy:
  matrix:
    java: [17, 21]
steps:
  - uses: actions/setup-java@v4
    with:
      java-version: ${{ matrix.java }}

Corre el mismo job N veces en paralelo (compatibilidad).

Tagging de imágenes (CI/CD)

Tag Uso
:sha-abc1234 Trazabilidad exacta
:v1.2.0 Release SemVer
:latest Solo demos (no prod)
- uses: docker/metadata-action@v5
- uses: docker/build-push-action@v6

Pipeline típico

lint → test → build image → push registry → deploy staging → smoke → deploy prod (approval)

Buenas prácticas:

  • Tests antes del build (fail-fast)
  • Tag con SHA + SemVer
  • Environment + required reviewers para prod
  • Continuous Delivery (click) vs Continuous Deployment (auto)

Troubleshooting de pipelines

Síntoma Qué mirar
Workflow no corre path .github/workflows/, trigger on:, branch/tag
Job skipped needs: falló upstream
Auth Docker/GHCR secrets, docker/login-action
Tests OK local, fallan en CI versiones matrix, env vars faltantes
Deploy no triggerea ¿pusheaste tag? ¿environment protegido?
gh run list
gh run view --log-failed