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: | |
deploy: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v3 | |
- name: Login to Docker Hub | |
uses: docker/login-action@v2 | |
with: | |
username: ${{ secrets.DOCKER_USERNAME }} | |
password: ${{ secrets.DOCKER_PASSWORD }} | |
- name: Build and push the image | |
run: | | |
docker build -t my-python-app . | |
docker tag my-python-app ${{ secrets.DOCKER_USERNAME }}/my-python-app | |
docker push ${{ secrets.DOCKER_USERNAME }}/my-python-app | |
- name: SSH Remote Commands | |
uses: appleboy/ssh-action@v1.0.3 | |
with: | |
host: ${{ secrets.EC2_HOST }} | |
username: ${{ secrets.EC2_USERNAME }} | |
key: ${{ secrets.EC2_KEY }} | |
port: 22 | |
script: | | |
# Install Docker if not already installed | |
sudo apt update && sudo apt install -y docker.io | |
sudo systemctl start docker | |
sudo systemctl enable docker | |
# Your deployment commands here | |
docker pull ${{ secrets.DOCKER_USERNAME }}/my-python-app | |
docker stop my-python-app || true | |
docker rm my-python-app || true | |
docker run -d -p 8000:8000 ${{ secrets.DOCKER_USERNAME }}/my-python-app |