ECR uri change #6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 repository | |
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" |