Merge pull request #123 from Trycatch-tv/dev #52
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: Deploy to EC2 | |
on: | |
push: | |
branches: | |
- main | |
jobs: | |
build-and-push: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v3 | |
- name: Set up Docker Buildx (optional for caching) | |
uses: docker/setup-buildx-action@v1 | |
- name: Configure AWS credentials | |
uses: aws-actions/configure-aws-credentials@v4 | |
with: | |
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} | |
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | |
aws-region: ${{ secrets.AWS_REGION }} | |
- name: Log in to Amazon ECR | |
id: login-ecr | |
uses: aws-actions/amazon-ecr-login@v1 | |
with: | |
region: ${{ secrets.AWS_REGION }} | |
- name: Build Docker image | |
run: docker build -t glasscamp-backend:latest . | |
- name: Tag Docker image | |
run: docker tag glasscamp-backend:latest ${{ secrets.AWS_ACCOUNT_ID }}.dkr.ecr.${{ secrets.AWS_REGION }}.amazonaws.com/glasscamp-backend:latest | |
- name: Push Docker image to Amazon ECR | |
run: docker push ${{ secrets.AWS_ACCOUNT_ID }}.dkr.ecr.${{ secrets.AWS_REGION }}.amazonaws.com/glasscamp-backend:latest | |
deploy: | |
runs-on: ubuntu-latest | |
needs: build-and-push | |
steps: | |
- name: Install SSH client | |
run: sudo apt-get install -y sshpass | |
- name: SSH and deploy | |
env: | |
SSHPASS: ${{ secrets.EC2_KEY }} | |
run: | | |
sshpass -e ssh -o StrictHostKeyChecking=no ${{ secrets.EC2_USER }}@${{ secrets.EC2_HOST }} << 'EOF' | |
# Clonar el repositorio en una ruta específica | |
git clone https://github.com/Trycatch-tv/ValidadorBootcamp-backend /home/${{ secrets.EC2_USER }}/app | |
cd /home/${{ secrets.EC2_USER }}/app | |
export AWS_REGION=${{ secrets.AWS_REGION }} | |
export AWS_ACCOUNT_ID=${{ secrets.AWS_ACCOUNT_ID }} | |
# Iniciar sesión en Amazon ECR | |
aws ecr get-login-password --region $AWS_REGION | docker login --username AWS --password-stdin $AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com | |
# Crear un archivo .env con las variables de entorno necesarias | |
echo "DATABASE_HOST=${{secrets.DATABASE_NAME}}" > .env | |
echo "DATABASE_PORT=${{secrets.DATABASE_PORT}}" >> .env | |
echo "DATABASE_USER=${{secrets.DATABASE_USER}}" >> .env | |
echo "DATABASE_PASSWORD=${{secrets.DATABASE_PASSWORD}}" >> .env | |
echo "DATABASE_NAME=${{secrets.DATABASE_NAME}}" >> .env | |
echo "FILE_SERVICE_URL=${{secrets.FILE_SERVICE_URL}}" >> .env | |
echo "REVIEW_SERVICE_URL=${{secrets.REVIEW_SERVICE_URL}}" >> .env | |
echo "NODE_ENV=${{secrets.NODE_ENV}}" >> .env | |
echo "POSTGRES_USER=${{secrets.POSTGRES_USER}}" >> .env | |
echo "POSTGRES_PASSWORD=${{secrets.POSTGRES_PASSWORD}}" >> .env | |
echo "POSTGRES_DB=${{secrets.POSTGRES_DB}}" >> .env | |
echo "AWS_ACCOUNT_ID=${{secrets.AWS_ACCOUNT_ID}}" >> .env | |
echo "AWS_REGION=${{secrets.AWS_REGION}}" >> .env | |
# Hacer pull de los cambios en la imagen docker | |
docker pull ${{ secrets.AWS_ACCOUNT_ID }}.dkr.ecr.${{ secrets.AWS_REGION }}.amazonaws.com/glasscamp-backend:latest | |
# Actualizar la imagen del servicio backend y reiniciarlo sin afectar db | |
#docker-compose down | |
docker-compose up -d --no-deps --build | |
echo "Finalizado!" | |
EOF |