forked from codebasics/python_projects
-
Notifications
You must be signed in to change notification settings - Fork 0
57 lines (48 loc) · 1.85 KB
/
Copy pathdocker-deploy.yml
File metadata and controls
57 lines (48 loc) · 1.85 KB
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
name: Build and Deploy Python App
on:
push:
branches:
- main # Adjust if using a different branch
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Debug Secrets (Check if they are available)
run: echo "Secrets are set"
- name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Build and push Docker image
run: |
docker build -t ${{ secrets.DOCKER_USERNAME }}/python-snake-game:test .
docker push ${{ secrets.DOCKER_USERNAME }}/python-snake-game:test
deploy:
needs: build
runs-on: ubuntu-latest
steps:
- name: Set up SSH Key
run: |
echo "${{ secrets.SSH_PRIVATE_KEY }}" > private_key
chmod 600 private_key
- name: Install Docker & Docker Compose on Remote Server
run: |
ssh -o StrictHostKeyChecking=no -i private_key ubuntu@${{ secrets.SERVER_IP }} << 'EOF'
sudo apt update
sudo apt install -y docker.io docker-compose
sudo systemctl enable --now docker
EOF
- name: Deploy using Docker Compose
run: |
ssh -o StrictHostKeyChecking=no -i private_key ubuntu@${{ secrets.SERVER_IP }} << 'EOF'
cd /home/ubuntu/python_projects # Navigate to the correct directory
docker login -u "${{ secrets.DOCKER_USERNAME }}" -p "${{ secrets.DOCKER_PASSWORD }}"
docker pull ${{ secrets.DOCKER_USERNAME }}/python-snake-game:test
docker-compose down # Stop any running containers
docker-compose up -d --force-recreate # Start fresh
EOF