Pipeline Juanma (WiP) #33
Workflow file for this run
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: AI4Devs-pipeline (backend) | ||
on: | ||
pull_request: | ||
types: [opened, synchronize, reopened] | ||
paths: | ||
- 'backend/**' | ||
- '.github/workflows/**' | ||
permissions: | ||
contents: read | ||
checks: write | ||
jobs: | ||
backend-ci: | ||
name: Backend CI | ||
runs-on: ubuntu-latest | ||
concurrency: | ||
group: ${{ github.workflow }}-${{ github.ref }} | ||
cancel-in-progress: true | ||
steps: | ||
- name: Checkout repository | ||
uses: actions/checkout@v4 | ||
with: | ||
fetch-depth: 1 | ||
persist-credentials: false | ||
- name: Setup Node.js | ||
uses: actions/setup-node@v4 | ||
with: | ||
node-version: '20' | ||
cache: 'npm' | ||
cache-dependency-path: './backend/package-lock.json' | ||
- name: Install dependencies | ||
working-directory: ./backend | ||
run: | | ||
npm ci | ||
env: | ||
npm_config_loglevel: error | ||
- name: Run tests | ||
working-directory: ./backend | ||
run: npm test | ||
env: | ||
NODE_ENV: test | ||
- name: Build backend | ||
working-directory: ./backend | ||
run: npm run build | ||
env: | ||
NODE_ENV: production | ||
- name: Upload build artifacts | ||
uses: actions/upload-artifact@v4 | ||
with: | ||
name: backend-build | ||
path: backend/dist/ | ||
retention-days: 7 | ||
deploy-to-ec2: | ||
name: Deploy to EC2 | ||
needs: backend-ci | ||
runs-on: ubuntu-latest | ||
# if: github.base_ref == 'develop' || github.base_ref == 'staging' | ||
environment: | ||
name: ${{ github.base_ref }} | ||
url: ${{ vars.EC2_APPLICATION_URL }} | ||
steps: | ||
- name: Download build artifact | ||
uses: actions/download-artifact@v4 | ||
with: | ||
name: backend-build | ||
path: dist | ||
- name: Configure SSH | ||
run: | | ||
mkdir -p ~/.ssh | ||
echo "${{ secrets.EC2_SSH_KEY }}" > ~/.ssh/deploy_key | ||
chmod 600 ~/.ssh/deploy_key | ||
# echo "${{ vars.EC2_SSH_KNOWN_HOSTS }}" >> ~/.ssh/known_hosts | ||
- name: Deploy to EC2 | ||
env: | ||
EC2_USER: ${{ vars.EC2_USER }} | ||
EC2_HOST: ${{ vars.EC2_HOST }} | ||
run: | | ||
# Crear archivo de variables de entorno si es necesario | ||
# echo "NODE_ENV=${{ github.base_ref }}" > .env | ||
# echo "PORT=${{ vars.APP_PORT }}" >> .env | ||
# Crea un directorio temporal con timestamp | ||
TIMESTAMP=$(date +'%Y%m%d_%H%M%S') | ||
DEPLOY_DIR="backend_$TIMESTAMP" | ||
# Transfiere los archivos | ||
ssh -i ~/.ssh/deploy_key $EC2_USER@$EC2_HOST "mkdir -p ~/$DEPLOY_DIR" | ||
scp -i ~/.ssh/deploy_key -r dist/* .env $EC2_USER@$EC2_HOST:~/$DEPLOY_DIR/ | ||
# Ejecuta el script de despliegue | ||
# ssh -i ~/.ssh/deploy_key $EC2_USER@$EC2_HOST << 'ENDSSH' | ||
# # Variables del despliegue actual | ||
# DEPLOY_DIR=$DEPLOY_DIR | ||
# APP_DIR=/opt/app/backend | ||
# | ||
# # Detener la aplicación actual | ||
# pm2 stop backend || true | ||
# | ||
# # Backup del despliegue anterior | ||
# if [ -d "$APP_DIR" ]; then | ||
# mv $APP_DIR "${APP_DIR}_backup_$(date +'%Y%m%d_%H%M%S')" | ||
# fi | ||
# | ||
# # Mover nuevo código a producción | ||
# mkdir -p $APP_DIR | ||
# mv ~/$DEPLOY_DIR/* $APP_DIR/ | ||
# | ||
# # Iniciar la aplicación | ||
# cd $APP_DIR | ||
# pm2 start ecosystem.config.js --env ${{ github.base_ref }} | ||
# | ||
# # Limpieza: eliminar directorio temporal y backups antiguos | ||
# rm -rf ~/$DEPLOY_DIR | ||
# find /opt/app -name "backend_backup_*" -type d -mtime +7 -exec rm -rf {} \; | ||
# ENDSSH | ||
# Verifica el despliegue | ||
# - name: Verify deployment | ||
# env: | ||
# HEALTH_CHECK_URL: ${{ vars.EC2_HEALTH_CHECK_URL }} | ||
# run: | | ||
# attempts=0 | ||
# max_attempts=30 | ||
# until curl -s -f $HEALTH_CHECK_URL > /dev/null || [ $attempts -eq $max_attempts ] | ||
# do | ||
# attempts=$((attempts+1)) | ||
# echo "Waiting for application to be ready... ($attempts/$max_attempts)" | ||
# sleep 10 | ||
# done | ||
# | ||
# if [ $attempts -eq $max_attempts ]; then | ||
# echo "❌ Deployment verification failed" | ||
# exit 1 | ||
# fi | ||
# | ||
# echo "✅ Deployment verified successfully" | ||
# Limpia las credenciales SSH | ||
- name: Cleanup SSH credentials | ||
if: always() | ||
run: rm -f ~/.ssh/deploy_key |