pagos-qa #24
This file contains 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: pagos-qa | |
on: | |
workflow_dispatch: | |
#inputs: | |
# environment: | |
# description: 'Deployment environment' | |
# required: true | |
# default: 'QA' | |
# options: | |
# - 'QA' | |
# - 'DEV' | |
env: | |
SSH_PRIVATE_KEY: ${{ secrets.PEM_ORG_PAGOS_DEV }} | |
REMOTE_USER: ec2-user | |
REMOTE_HOST: ${{ secrets.IP_PAGOS_QA }} | |
APP_NAME: app.js | |
jobs: | |
pagos-qa: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v2 | |
- name: Crear directorio de trabajo en la máquina EC2 | |
uses: appleboy/ssh-action@master | |
with: | |
host: ${{ env.REMOTE_HOST }} | |
username: ${{ env.REMOTE_USER }} | |
key: ${{ env.SSH_PRIVATE_KEY }} | |
script: | | |
sudo rm -rf /opt/ms-pagos-qa-app || true | |
sudo mkdir -p /opt/ms-pagos-qa-app | |
sudo usermod -aG docker $USER | |
- name: Clonar repositorio de GitHub | |
uses: appleboy/ssh-action@master | |
with: | |
host: ${{ env.REMOTE_HOST }} | |
username: ${{ env.REMOTE_USER }} | |
key: ${{ env.SSH_PRIVATE_KEY }} | |
script: | | |
cd /opt/ms-pagos-qa-app | |
sudo git clone -b franco-pagos-qa https://${{ secrets.TOKEN_PAGOS }}@github.com/alkemyTech/UMSA-DevOps-T3 . | |
# Instalar Docker | |
- name: Install Docker | |
uses: appleboy/ssh-action@master | |
with: | |
host: ${{ env.REMOTE_HOST }} | |
username: ${{ env.REMOTE_USER }} | |
key: ${{ env.SSH_PRIVATE_KEY }} | |
script: | | |
if ! command -v docker &> /dev/null | |
then | |
echo "Docker is not installed. Installing Docker..." | |
sudo yum update -y | |
sudo yum install -y docker | |
else | |
echo "Docker is already installed." | |
fi | |
if ! systemctl is-active --quiet docker | |
then | |
echo "Docker is not running. Starting Docker..." | |
sudo systemctl start docker | |
else | |
echo "Docker is already running." | |
fi | |
# Construir la imagen Docker | |
- name: Build Docker image | |
uses: appleboy/ssh-action@master | |
with: | |
host: ${{ env.REMOTE_HOST }} | |
username: ${{ env.REMOTE_USER }} | |
key: ${{ env.SSH_PRIVATE_KEY }} | |
script: | | |
cd /opt/ms-pagos-qa-app | |
if docker images -q pagosimagen; then | |
docker rmi -f pagosimagen | |
fi | |
docker build -t pagosimagen . | |
# Eliminar imágenes sin etiqueta | |
docker rmi $(docker images -f "dangling=true" -q) || true | |
# Autenticación en DockerHub | |
- name: Login to DockerHub | |
uses: docker/login-action@v2 | |
with: | |
username: ${{ secrets.dockerUserpago }} | |
password: ${{ secrets.DOCKERPAGO_TOKEN }} | |
# Subir imagen a DockerHub | |
#- name: Push image to DockerHub | |
# run: docker push "${{secrets.dockerUserpago}}"/pagosimagen:latest | |
- name: Build and push Docker image | |
uses: docker/build-push-action@v5 | |
with: | |
context: . | |
file: ./Dockerfile | |
push: true | |
tags: ${{ secrets.dockerUserpago }}/pagosimagen:latest | |
username: ${{ secrets.dockerUserpago }} | |
password: ${{ secrets.DOCKERPAGO_TOKEN }} | |
# Despliegue al entorno de QA (manual) | |
- name: Desplegar al entorno de QA | |
#if: github.event.inputs.environment == 'QA' | |
uses: appleboy/ssh-action@master | |
with: | |
host: ${{ env.REMOTE_HOST }} | |
username: ${{ env.REMOTE_USER }} | |
key: ${{ env.SSH_PRIVATE_KEY }} | |
script: | | |
echo "Presiona un botón para desplegar en QA" | |
# Obtener el ID del contenedor existente si está en ejecución | |
CONTAINER_ID=$(docker ps -qf "name=pagosimagen") | |
if [ ! -z "$CONTAINER_ID" ]; then | |
# Si hay un contenedor en ejecución, detenerlo | |
docker stop $CONTAINER_ID | |
docker rm $CONTAINER_ID | |
sleep 5 | |
echo "Contenedor existente detenido." | |
fi | |
# Ejecutar el contenedor Docker después de asegurarse de que la imagen esté disponible | |
docker run -d -p 3000:3000 --name pagosimagen pagosimagen | |
# Verificar si el contenedor está en ejecución | |
if docker ps | grep -q "pagosimagen"; then | |
echo "El contenedor está en ejecución." | |
else | |
echo "¡Error! El contenedor no se inició correctamente." | |
fi |