86 lines
3.0 KiB
YAML
86 lines
3.0 KiB
YAML
# Ejemplo de pipeline: build -> push al registry de Gitea -> deploy por SSH.
|
|
#
|
|
# Copia este archivo a la raíz de TU repo como .gitea/workflows/ci.yml
|
|
# (Gitea 1.24+ también acepta .github/workflows/).
|
|
#
|
|
# Secretos a definir en el repo (Settings -> Actions -> Secrets):
|
|
# REGISTRY_TOKEN token de acceso de un usuario con permiso de packages
|
|
# DEPLOY_SSH_KEY clave privada ed25519 para el servidor destino
|
|
# DEPLOY_HOST usuario@servidor (ej. deploy@10.0.0.5)
|
|
#
|
|
# Notas:
|
|
# - runs-on: linux -> label del runner definido en RUNNER_LABELS
|
|
# - ${{ gitea.* }} -> contexto nativo de Gitea (github.* también funciona)
|
|
# - gitea.server_url trae esquema (https://...), así que el registry se
|
|
# deriva en un step quitando el esquema (docker login no lo acepta).
|
|
# -----------------------------------------------------------------------------
|
|
name: ci
|
|
|
|
on:
|
|
push:
|
|
branches: [main]
|
|
tags: ["v*"]
|
|
pull_request:
|
|
|
|
jobs:
|
|
build:
|
|
runs-on: linux
|
|
steps:
|
|
- uses: actions/checkout@v4
|
|
with:
|
|
fetch-depth: 0
|
|
|
|
- name: Resolver registry e imagen
|
|
run: |
|
|
REG="${{ gitea.server_url }}"
|
|
REG="${REG#*://}"
|
|
REG="${REG%/}"
|
|
echo "REGISTRY=${REG}" >> "$GITHUB_ENV"
|
|
echo "IMAGE=${REG}/${{ gitea.repository }}" >> "$GITHUB_ENV"
|
|
echo "registry=${REG}"
|
|
echo "repo=${{ gitea.repository }} ref=${{ gitea.ref }} sha=${{ gitea.sha }}"
|
|
|
|
- name: Entorno
|
|
run: |
|
|
docker version --format 'daemon {{.Server.Version}}'
|
|
docker buildx version || true
|
|
|
|
- name: Login al registry de Gitea
|
|
run: |
|
|
echo "${{ secrets.REGISTRY_TOKEN }}" \
|
|
| docker login "$REGISTRY" -u "${{ gitea.actor }}" --password-stdin
|
|
|
|
- name: Build y push
|
|
run: |
|
|
SHORT="$(echo "${{ gitea.sha }}" | cut -c1-7)"
|
|
docker build \
|
|
--label "org.opencontainers.image.revision=${{ gitea.sha }}" \
|
|
--label "org.opencontainers.image.source=${{ gitea.server_url }}/${{ gitea.repository }}" \
|
|
-t "${IMAGE}:${SHORT}" \
|
|
-t "${IMAGE}:latest" .
|
|
docker push "${IMAGE}:${SHORT}"
|
|
docker push "${IMAGE}:latest"
|
|
|
|
- name: Trivy (opcional)
|
|
continue-on-error: true
|
|
run: |
|
|
docker run --rm -v /var/run/docker.sock:/var/run/docker.sock \
|
|
aquasec/trivy:latest image --exit-code 0 --quiet "${IMAGE}:latest"
|
|
|
|
deploy:
|
|
needs: build
|
|
# Actívalo creando la variable de repo DEPLOY_ENABLED=true
|
|
if: vars.DEPLOY_ENABLED == 'true' && gitea.ref == 'refs/heads/main'
|
|
runs-on: linux
|
|
steps:
|
|
- name: Deploy por SSH
|
|
env:
|
|
SSH_KEY: ${{ secrets.DEPLOY_SSH_KEY }}
|
|
TARGET: ${{ secrets.DEPLOY_HOST }}
|
|
run: |
|
|
install -d -m 700 ~/.ssh
|
|
printf '%s\n' "$SSH_KEY" > ~/.ssh/id_ed25519
|
|
chmod 600 ~/.ssh/id_ed25519
|
|
ssh -o StrictHostKeyChecking=accept-new "$TARGET" \
|
|
'cd /srv/app && docker compose pull && docker compose up -d'
|