Skip to content

Commit a1cc56d

Browse files
committed
Adds CI/CD pipeline for automated deployment
Sets up a GitHub Actions workflow to build, push, and deploy the application to an EC2 instance. The workflow automates the deployment process on every push to the main branch. It includes steps for: - Checking out the code - Setting up Docker Buildx - Logging into Docker Hub - Building and pushing the Docker image - Deploying to EC2 using SSH, including stopping/removing the old container, pulling the latest image, and running the new container.
1 parent 96208d0 commit a1cc56d

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

.github/workflows/deploy.yml

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
name: Deploy to EC2
2+
3+
on:
4+
push:
5+
branches: [ "main" ]
6+
7+
jobs:
8+
build-and-deploy:
9+
runs-on: ubuntu-latest
10+
11+
steps:
12+
- name: Checkout code
13+
uses: actions/checkout@v4
14+
15+
- name: Set up Docker Buildx
16+
uses: docker/setup-buildx-action@v3
17+
18+
- name: Login to Docker Hub
19+
uses: docker/login-action@v3
20+
with:
21+
username: ${{ secrets.DOCKER_USERNAME }}
22+
password: ${{ secrets.DOCKER_PASSWORD }}
23+
24+
- name: Build and push Docker image
25+
uses: docker/build-push-action@v5
26+
with:
27+
context: .
28+
push: true
29+
tags: ${{ secrets.DOCKER_USERNAME }}/api-onibusbh:latest
30+
31+
- name: Deploy to EC2
32+
uses: appleboy/ssh-action@master
33+
with:
34+
host: ${{ secrets.EC2_HOST }}
35+
username: ${{ secrets.EC2_USERNAME }}
36+
key: ${{ secrets.EC2_SSH_KEY }}
37+
script: |
38+
docker stop api-onibusbh || true
39+
docker rm api-onibusbh || true
40+
41+
docker pull ${{ secrets.DOCKER_USERNAME }}/api-onibusbh:latest
42+
43+
docker run -d \
44+
--name api-onibusbh \
45+
-p 8080:8080 \
46+
-e SPRING_PROFILES_ACTIVE=prod \
47+
-e MONGO_HOST=${{ secrets.MONGO_HOST }} \
48+
-e MONGO_PORT=${{ secrets.MONGO_PORT }} \
49+
-e MONGO_DATABASE=${{ secrets.MONGO_DATABASE }} \
50+
-e MONGO_USERNAME=${{ secrets.MONGO_USERNAME }} \
51+
-e MONGO_PASSWORD=${{ secrets.MONGO_PASSWORD }} \
52+
${{ secrets.DOCKER_USERNAME }}/api-onibusbh:latest

0 commit comments

Comments
 (0)