Skip to content

Ci Ventas Dev.yml

Ci Ventas Dev.yml #11

Workflow file for this run

on:
push:
branches:
- maicol_ventas
jobs:
deploy:
runs-on: ubuntu-latest
env:
EC2_HOST: ${{ secrets.IP_ventas_DEV }}
EC2_USERNAME: ${{ secrets.USERS_COBRANZAS }}
EC2_SSH_KEY: ${{ secrets.PEM_VENTAS_DEV }}
GH_TOKEN: ${{ secrets.TOKEN_COBRANZAS }}
steps:
- name: Checkout repo
uses: actions/checkout@v2
# Paso para comprobar si el directorio existe y crearlo si es necesario
- name: Check if directory exists
uses: appleboy/ssh-action@master
with:
host: ${{ env.EC2_HOST }}
username: ${{ env.EC2_USERNAME }}
key: ${{ env.EC2_SSH_KEY }}
script: |
if [ -d "/opt/ms-ventas-dev-app" ]; then
echo "Directory already exists."
else
echo "Directory does not exist. Creating directory..."
sudo mkdir -p /opt/ms-ventas-dev-app
fi
# Paso para instalar Node.js y npm en un directorio específico
- name: Install Node.js and npm
uses: appleboy/ssh-action@master
with:
host: ${{ env.EC2_HOST }}
username: ${{ env.EC2_USERNAME }}
key: ${{ env.EC2_SSH_KEY }}
script: |
sudo apt update
sudo apt install -y nodejs npm
export PATH="$HOME/.npm/bin:$PATH"
mkdir -p /opt/node_modules # Directorio donde se instalarán los paquetes
npm config set prefix /opt/node_modules # Configurar npm para que instale paquetes en el directorio personal
# Paso para instalar PM2 en un directorio específico
- name: Install PM2
uses: appleboy/ssh-action@master
with:
host: ${{ env.EC2_HOST }}
username: ${{ env.EC2_USERNAME }}
key: ${{ env.EC2_SSH_KEY }}
script: |
sudo npm install pm2 --prefix /opt/node_modules -g
# Resto de los pasos para el despliegue
- name: Install Git
uses: appleboy/ssh-action@master
with:
host: ${{ env.EC2_HOST }}
username: ${{ env.EC2_USERNAME }}
key: ${{ env.EC2_SSH_KEY }}
script: |
sudo yum update -y && sudo yum install -y git
- name: Clone repository
uses: appleboy/ssh-action@master
with:
host: ${{ env.EC2_HOST }}
username: ${{ env.EC2_USERNAME }}
key: ${{ env.EC2_SSH_KEY }}
script: |
cd /opt
sudo rm -rf /opt/ms-ventas-dev-app # Eliminar el directorio existente
sudo git clone -b maicol_ventas https://${{ secrets.TOKEN_COBRANZAS }}@github.com/alkemyTech/UMSA-DevOps-T3 /opt/ms-ventas-dev-app
- name: Install npm if not installed
uses: appleboy/ssh-action@master
with:
host: ${{ env.EC2_HOST }}
username: ${{ env.EC2_USERNAME }}
key: ${{ env.EC2_SSH_KEY }}
script: |
if ! command -v npm &> /dev/null; then
sudo yum install -y npm
fi
- name: Install npm packages
uses: appleboy/ssh-action@master
with:
host: ${{ env.EC2_HOST }}
username: ${{ env.EC2_USERNAME }}
key: ${{ env.EC2_SSH_KEY }}
script: |
cd /opt/ms-ventas-dev-app
sudo npm install express pm2
- name: Restart or Start app.js
uses: appleboy/ssh-action@master
with:
host: ${{ env.EC2_HOST }}
username: ${{ env.EC2_USERNAME }}
key: ${{ env.EC2_SSH_KEY }}
script: |
cd /opt/ms-ventas-dev-app
sudo ls -l # Comprobar la existencia del archivo
sudo pm2 status # Verificar el estado de PM2
sudo pm2 restart app.js || sudo pm2 start -f app.js
- name: Check service status
uses: appleboy/ssh-action@master
with:
host: ${{ env.EC2_HOST }}
username: ${{ env.EC2_USERNAME }}
key: ${{ env.EC2_SSH_KEY }}
script: |
sudo pm2 status