-
Notifications
You must be signed in to change notification settings - Fork 0
136 lines (127 loc) · 4.77 KB
/
pagos-qa.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
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
# Obtener el ID del contenedor en ejecución
CONTAINER_ID=$(docker ps -qf "name=pagosimagen")
# Si hay un contenedor en ejecución, detenerlo
if [ ! -z "$CONTAINER_ID" ]; then
docker stop $CONTAINER_ID
docker rm $CONTAINER_ID
sleep 5
echo "Contenedor existente detenido."
fi
# Eliminar imágenes antiguas si existen
if docker images -q pagosimagen; then
docker rmi -f pagosimagen
fi
# Construir la imagen
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"
docker stop pagosimagen || true
docker rm pagosimagen || true
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