75 lines
		
	
	
		
			3.0 KiB
		
	
	
	
		
			YAML
		
	
	
	
	
	
			
		
		
	
	
			75 lines
		
	
	
		
			3.0 KiB
		
	
	
	
		
			YAML
		
	
	
	
	
	
| name: Build and Deploy
 | |
| 
 | |
| on:
 | |
|   push:
 | |
|     branches:
 | |
|       - main
 | |
| 
 | |
| jobs:
 | |
|   build-and-push:
 | |
|     runs-on: ubuntu-latest
 | |
| 
 | |
|     steps:
 | |
|       - name: Checkout code
 | |
|         uses: actions/checkout@v3
 | |
| 
 | |
|       - name: Create Kaniko config file
 | |
|         run: |
 | |
|           echo '{"auths":{"${{ secrets.REGISTRY_URL }}":{"username":"${{ secrets.REGISTRY_USER }}","password":"${{ secrets.ACTIONS_PAT }}"}}}' > /kaniko/config.json
 | |
|         # Este comando crea el fichero de credenciales que Kaniko necesita.
 | |
|         # Lo creamos en un directorio que montaremos en el siguiente paso.
 | |
| 
 | |
|       - name: Build and Push Backend
 | |
|         run: |
 | |
|           docker run --rm \
 | |
|             -v ${{ gitea.workspace }}:/workspace \
 | |
|             -v /kaniko/config.json:/kaniko/.docker/config.json \
 | |
|             gcr.io/kaniko-project/executor:v1.9.0 \
 | |
|             --context=/workspace \
 | |
|             --dockerfile=/workspace/Backend/GestionIntegral.Api/Dockerfile \
 | |
|             --destination=${{ secrets.REGISTRY_URL }}/${{ gitea.actor }}/${{ toLower(gitea.repository_name) }}-backend:latest \
 | |
|             --destination=${{ secrets.REGISTRY_URL }}/${{ gitea.actor }}/${{ toLower(gitea.repository_name) }}-backend:${{ gitea.sha_short }} \
 | |
|             --insecure
 | |
| 
 | |
|       - name: Build and Push Frontend
 | |
|         run: |
 | |
|           docker run --rm \
 | |
|             -v ${{ gitea.workspace }}:/workspace \
 | |
|             -v /kaniko/config.json:/kaniko/.docker/config.json \
 | |
|             gcr.io/kaniko-project/executor:v1.9.0 \
 | |
|             --context=/workspace \
 | |
|             --dockerfile=/workspace/Frontend/Dockerfile \
 | |
|             --destination=${{ secrets.REGISTRY_URL }}/${{ gitea.actor }}/${{ toLower(gitea.repository_name) }}-frontend:latest \
 | |
|             --destination=${{ secrets.REGISTRY_URL }}/${{ gitea.actor }}/${{ toLower(gitea.repository_name) }}-frontend:${{ gitea.sha_short }} \
 | |
|             --insecure
 | |
| 
 | |
|   deploy:
 | |
|     runs-on: ubuntu-latest
 | |
|     needs: build-and-push
 | |
|     
 | |
|     steps:
 | |
|       - name: Deploy to Production
 | |
|         run: |
 | |
|           # Este paso no necesita cambios, pero para mantenerlo simple,
 | |
|           # lo ejecutaremos directamente sin la acción de checkout,
 | |
|           # ya que no necesita los ficheros del repositorio.
 | |
|           apk add --no-cache openssh-client
 | |
|           mkdir -p ~/.ssh
 | |
|           echo "${{ secrets.PROD_SERVER_SSH_KEY }}" > ~/.ssh/id_rsa
 | |
|           chmod 600 ~/.ssh/id_rsa
 | |
|           ssh-keyscan -H ${{ secrets.PROD_SERVER_HOST }} >> ~/.ssh/known_hosts
 | |
|           
 | |
|           ssh ${{ secrets.PROD_SERVER_USER }}@${{ secrets.PROD_SERVER_HOST }} << 'EOF'
 | |
|             echo "--- CONECTADO AL SERVIDOR DE PRODUCCIÓN ---"
 | |
|             cd /opt/gestion-integral
 | |
|             
 | |
|             export DB_SA_PASSWORD="${{ secrets.DB_SA_PASSWORD_SECRET }}"
 | |
|             export JWT_KEY="${{ secrets.JWT_KEY_SECRET }}"
 | |
|             
 | |
|             docker login ${{ secrets.REGISTRY_URL }} -u ${{ secrets.REGISTRY_USER }} -p ${{ secrets.ACTIONS_PAT }}
 | |
|             docker compose pull
 | |
|             docker compose up -d
 | |
|             docker image prune -af
 | |
|             
 | |
|             echo "--- DESPLIEGUE COMPLETADO ---"
 | |
|           EOF |