running in the correct port the api #3
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: Build and Push Docker Image | |
on: | |
push: | |
branches: | |
- main | |
env: | |
DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }} | |
DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }} | |
EC2_HOST: ${{ secrets.EC2_HOST }} | |
EC2_KEY: ${{ secrets.EC2_KEY }} | |
jobs: | |
deploy-backend: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v2 | |
- name: Debug environment variables | |
run: | | |
echo "DOCKER_USERNAME=${DOCKER_USERNAME}" | |
echo "DOCKER_PASSWORD=${DOCKER_PASSWORD}" | |
echo "EC2_HOST=${EC2_HOST}" | |
echo "EC2_KEY=${EC2_KEY}" | |
- name: Decode and save EC2 private key | |
run: | | |
echo "${{ secrets.EC2_KEY }}" | base64 --decode > ec2-private-key.pem | |
chmod 600 ec2-private-key.pem | |
- name: SSH into EC2 instance and deploy | |
run: | | |
ssh -o StrictHostKeyChecking=no -i ./ec2-private-key.pem ec2-user@${EC2_HOST} << 'EOF' | |
cd /home/ec2-user/fastapi-todos | |
# Pull latest code | |
git pull origin main | |
# Build Docker image | |
sudo docker build -t fastapi-to-dos . | |
# Stop previous Docker container (if running) | |
sudo docker stop fastapi-to-dos || true | |
# Remove previous Docker container (if exists) | |
sudo docker rm fastapi-to-dos || true | |
# Run new Docker container | |
docker run -p 8000:8000 fastapi-to-dos --restart unless-stopped fastapi-to-dos | |
EOF |