1111 IMAGE_NAME : ${{ secrets.DOCKER_IMAGE_NAME || 'atom-dbro-backend' }}
1212
1313jobs :
14- build :
15- name : Build and Export Docker Image
14+ build-and-transfer :
15+ name : Build and Transfer Docker Image
1616 runs-on : ubuntu-latest
17+ if : github.ref == 'refs/heads/main'
1718
1819 steps :
1920 - name : Checkout code
2021 uses : actions/checkout@v4
2122
22- - name : Set up Docker Buildx
23- uses : docker/setup-buildx-action@v3
24-
23+ - name : Verify Docker access
24+ run : |
25+ echo "=== Verifying Docker access ==="
26+ docker ps 2>&1 | head -3 || {
27+ echo "❌ ERROR: Cannot access Docker daemon"
28+ exit 1
29+ }
30+ echo "✅ Docker access verified"
2531 - name : Build Docker image
26- uses : docker/build-push-action@v5
27- with :
28- context : .
29- file : ./Dockerfile
30- push : false
31- load : true
32- tags : ${{ env.IMAGE_NAME }}:latest
33- no-cache : true
34-
32+ run : |
33+ echo "📦 Building Docker image: ${{ env.IMAGE_NAME }}:latest"
34+ docker build --no-cache -t ${{ env.IMAGE_NAME }}:latest -f ./Dockerfile .
35+ echo "✅ Docker image built successfully"
3536 - name : Verify image was built
3637 run : |
3738 if ! docker images | grep -q "${{ env.IMAGE_NAME }}.*latest"; then
4041 fi
4142 echo "✅ Docker image built successfully: ${{ env.IMAGE_NAME }}:latest"
4243 docker images | grep "${{ env.IMAGE_NAME }}"
43-
4444 - name : Export Docker image to tar.gz
4545 run : |
4646 echo "📦 Exporting Docker image to tar.gz..."
5858 fi
5959
6060 echo "✅ Docker image exported successfully"
61-
62- - name : Upload Docker image artifact
63- uses : actions/upload-artifact@v4
64- with :
65- name : docker-image
66- path : image.tar.gz
67- retention-days : 1
68-
69- transfer :
70- name : Transfer Image to Server
71- needs : build
72- runs-on : ubuntu-latest
73- if : github.ref == 'refs/heads/main'
74-
75- steps :
76- - name : Download Docker image artifact
77- uses : actions/download-artifact@v4
78- with :
79- name : docker-image
80- path : ./
81-
8261 - name : Set up SSH
8362 uses : webfactory/ssh-agent@v0.9.0
8463 with :
8968 SSH_PORT="${DEPLOY_SSH_PORT:-22}"
9069 ssh -o StrictHostKeyChecking=no -p "$SSH_PORT" ${{ secrets.DEPLOY_USER }}@${{ secrets.DEPLOY_HOST }} \
9170 "echo 'SSH connection successful' && hostname"
71+ env :
72+ DEPLOY_SSH_PORT : ${{ secrets.DEPLOY_SSH_PORT || '22' }}
9273
9374 - name : Transfer Docker image to server
9475 run : |
@@ -108,9 +89,17 @@ jobs:
10889 env :
10990 DEPLOY_SSH_PORT : ${{ secrets.DEPLOY_SSH_PORT || '22' }}
11091
92+ - name : Cleanup
93+ if : always()
94+ run : |
95+ echo "🧹 Cleaning up temporary files and Docker resources..."
96+ rm -f image.tar.gz
97+ docker rmi ${{ env.IMAGE_NAME }}:latest 2>/dev/null || true
98+ docker builder prune -f || true
99+ echo "✅ Cleanup completed"
111100 deploy :
112101 name : Deploy Application
113- needs : transfer
102+ needs : build-and- transfer
114103 runs-on : ubuntu-latest
115104 if : github.ref == 'refs/heads/main'
116105
@@ -332,4 +321,4 @@ jobs:
332321 CONTAINER_NAME : ${{ secrets.DEPLOY_CONTAINER_NAME || 'atom-dbro-app' }}
333322 SERVICE_NAME : ${{ secrets.DEPLOY_SERVICE_NAME || 'app' }}
334323 IMAGE_NAME : ${{ env.IMAGE_NAME }}
335- COMPOSE_PROJECT_NAME : ${{ secrets.DEPLOY_COMPOSE_PROJECT_NAME || '' }}
324+ COMPOSE_PROJECT_NAME : ${{ secrets.DEPLOY_COMPOSE_PROJECT_NAME || '' }}
0 commit comments