CD-AUDITORIA-QA #14
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
# Nombre del pipeline | |
name: CD-AUDITORIA-QA | |
# Disparador manual del pipeline con entrada para el nombre del contenedor | |
on: | |
workflow_dispatch: | |
inputs: | |
app_container_name: | |
description: 'Nombre del contenedor' | |
required: true | |
default: 'auditoria-app-qa' | |
# Variables de entorno | |
env: | |
SSH_PRIVATE_KEY: ${{ secrets.PEM_AUDITORIA_QA }} | |
REMOTE_USER: ec2-user | |
REMOTE_HOST: ${{ secrets.IP_AUDITORIA_QA }} | |
APP_NAME: ${{ github.event.inputs.app_container_name }} | |
DOCKERHUB_TOKEN: ${{ secrets.DH_TOKEN_CLIENTES }} | |
DOCKERHUB_USERNAME: romicalani | |
jobs: | |
build: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v4 | |
- name: Iniciar sesion en Docker Hub | |
uses: docker/[email protected] | |
with: | |
username: ${{ env.DOCKERHUB_USERNAME }} | |
password: ${{ env.DOCKERHUB_TOKEN }} | |
# Construir la imagen Docker usando variables de entorno | |
- name: Generar imagen Docker | |
run: | | |
docker build . -t ${{env.DOCKERHUB_USERNAME }}/${{env.APP_NAME}}:latest | |
# Subir la imagen Docker a nuestra cuenta de Docker Hub | |
- name: Subir imagen Docker | |
run: | | |
docker push ${{env.DOCKERHUB_USERNAME }}/${{env.APP_NAME}}:latest | |
# Instalar Docker en la instancia ec2 en AWS | |
- name: Instalar 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 | |
sudo yum install docker -y | |
else | |
echo "docker is installed." | |
fi | |
if systemctl is-active --quiet docker; then | |
echo "Docker service is already running." | |
else | |
sudo systemctl start docker | |
fi | |
sudo usermod -aG docker ${{ env.REMOTE_USER }} | |
- name: Detener y eliminar si esta en ejecucion | |
uses: appleboy/ssh-action@master | |
with: | |
host: ${{ env.REMOTE_HOST }} | |
username: ${{ env.REMOTE_USER }} | |
key: ${{ env.SSH_PRIVATE_KEY }} | |
script: | | |
if sudo docker ps -a --format '{{.Names}}' | grep -q ${{env.APP_NAME}}; then | |
echo "Eliminando contenedor con nombre: ${{env.APP_NAME}}" | |
sudo docker stop $(sudo docker ps -q) || true | |
sudo docker rm -f ${{env.APP_NAME}} || true | |
echo "Contenedor eliminado exitosamente." | |
fi | |
- name: Descargar imagen docker | |
uses: appleboy/ssh-action@master | |
with: | |
host: ${{ env.REMOTE_HOST }} | |
username: ${{ env.REMOTE_USER }} | |
key: ${{ env.SSH_PRIVATE_KEY }} | |
script: | | |
if ! sudo docker image ${{ env.DOCKERHUB_USERNAME }}/${{env.APP_NAME}}:latest &> /dev/null; then | |
sudo docker pull ${{ env.DOCKERHUB_USERNAME }}/${{env.APP_NAME}}:latest | |
fi | |
sudo docker run -dp 3000:3000 --name ${{env.APP_NAME}} ${{ env.DOCKERHUB_USERNAME }}/${{env.APP_NAME}}:latest | |
- name: Verificar servicio en ejecucion | |
uses: appleboy/ssh-action@master | |
with: | |
host: ${{ env.REMOTE_HOST }} | |
username: ${{ env.REMOTE_USER }} | |
key: ${{ env.SSH_PRIVATE_KEY }} | |
script: | | |
if netstat -an | grep :3000; then | |
echo "${{env.APP_NAME}} is running" | |
else | |
echo "${{env.APP_NAME}} is not running" | |
fi |