-
Notifications
You must be signed in to change notification settings - Fork 0
69 lines (55 loc) · 2.35 KB
/
ci.yaml
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
63
64
65
66
67
68
69
name: CI/CD Pipeline
on:
push:
branches:
- main
permissions:
contents: read
id-token: write
jobs:
build-and-push-ecr:
name: Build and Push to ECR
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
- 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 }}
- name: Build, tag, and push image to Amazon ECR
run: |
# Define the image name and tag
IMAGE_NAME=realestate-pricing-app
IMAGE_TAG=latest # or use $(git rev-parse --short HEAD) for commit hash
# Full image name including registry and repo
FULL_IMAGE_NAME=$IMAGE_NAME:$IMAGE_TAG
ECR_FULL_IMAGE_NAME=${{ secrets.AWS_ECR_LOGIN_URI }}/$IMAGE_NAME:$IMAGE_TAG
# Build the Docker image
docker build -t $FULL_IMAGE_NAME .
# Login to Amazon ECR
aws ecr get-login-password --region ${{ secrets.AWS_REGION }} | docker login --username AWS --password-stdin ${{ secrets.AWS_ECR_LOGIN_URI }}
# Tag the image to match the ECR repository
docker tag $FULL_IMAGE_NAME $ECR_FULL_IMAGE_NAME
# Push the image to ECR
docker push $ECR_FULL_IMAGE_NAME
deploy-to-ec2:
name: Deploy to EC2
needs: build-and-push-ecr
runs-on: [self-hosted, linux]
steps:
- name: Checkout repository
uses: actions/checkout@v2
- 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 }}
- name: Deploy to EC2
run: |
# Replace 'your-ec2-instance-ip' with your actual EC2 instance IP
# The following command assumes you have SSH set up to connect to your EC2 instance
ssh ec2-user@15.156.87.140 "docker pull ${{ secrets.AWS_ECR_LOGIN_URI }}/${{ secrets.AWS_ECR_REPOSITORY_NAME }}:latest && docker run -d -p 8501:8501 --restart=always ${{ secrets.AWS_ECR_LOGIN_URI }}/${{ secrets.AWS_ECR_REPOSITORY_NAME }}:latest"