Repository navigation
62 lines (53 loc) · 2.02 KB
/
Copy pathdeploy.yml
File metadata and controls
62 lines (53 loc) · 2.02 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
58
59
60
61
62
name: Deploy CodeX Backend
on:
push:
branches:
- main
paths:
- 'Backend/**'
- '.github/workflows/deploy.yml'
workflow_dispatch:
permissions:
contents: read
jobs:
build-and-deploy:
name: Build Docker Image & Deploy via AWS SSM
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Log in to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Build and Push Docker Image
uses: docker/build-push-action@v5
with:
context: ./Backend
file: ./Backend/Dockerfile
push: true
tags: |
${{ secrets.DOCKERHUB_USERNAME }}/codex-backend:latest
${{ secrets.DOCKERHUB_USERNAME }}/codex-backend:${{ github.sha }}
- 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 || 'ap-south-1' }}
- name: Execute Deployment on EC2 via AWS SSM
run: |
aws ssm send-command \
--instance-ids "${{ secrets.EC2_INSTANCE_ID }}" \
--document-name "AWS-RunShellScript" \
--comment "Pull latest container image and deploy via Docker Compose with automatic service restart" \
--parameters 'commands=[
"cd /home/ubuntu/codex/backend",
"echo \"${{ secrets.DOCKERHUB_TOKEN }}\" | sudo docker login -u \"${{ secrets.DOCKERHUB_USERNAME }}\" --password-stdin",
"sudo docker pull ${{ secrets.DOCKERHUB_USERNAME }}/codex-backend:latest",
"sudo docker compose up -d --force-recreate --remove-orphans",
"sudo docker image prune -f"
]'