Skip to content

Commit

Permalink
CodeDeploy with CICD
Browse files Browse the repository at this point in the history
  • Loading branch information
2003HARSH committed Nov 21, 2024
1 parent 88d556b commit 33be129
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 25 deletions.
55 changes: 32 additions & 23 deletions .github/workflows/cicd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -63,33 +63,42 @@ jobs:
- name: Tag Docker Image
run: |
docker tag text-classification:latest 843369994444.dkr.ecr.eu-north-1.amazonaws.com/text-classification:latest
docker tag text-classification:latest 843369994444.dkr.ecr.eu-north-1.amazonaws.com/text-classification:v3
- name: Push Docker Image to AWS ECR
run: |
docker push 843369994444.dkr.ecr.eu-north-1.amazonaws.com/text-classification:latest
docker push 843369994444.dkr.ecr.eu-north-1.amazonaws.com/text-classification:v3
Deploy-to-EC2:
runs-on: ubuntu-latest
needs: test-and-package
# Deploy:
# runs-on: ubuntu-latest
# needs: test-and-package

steps:
# steps:

- name: Deploy to EC2
uses: appleboy/ssh-action@v0.1.5
with:
host: ${{ secrets.EC2_HOST }}
username: ${{ secrets.EC2_USER }}
key: ${{ secrets.EC2_SSH_KEY }}
script: |
aws configure set aws_access_key_id ${{secrets.AWS_ACCESS_KEY_ID }}
aws configure set aws_secret_access_key ${{secrets.AWS_SECRET_ACCESS_KEY }}
aws configure set default.region eu-north-1
aws ecr get-login-password --region eu-north-1 | docker login --username AWS --password-stdin 843369994444.dkr.ecr.eu-north-1.amazonaws.com
docker pull 843369994444.dkr.ecr.eu-north-1.amazonaws.com/text-classification:latest
docker stop text-classification || true
docker rm text-classification || true
docker run -d -p 80:5000 -e DAGSHUB_PAT=${{ secrets.DAGSHUB_PAT }} --name text-classification 843369994444.dkr.ecr.eu-north-1.amazonaws.com/text-classification:latest

# Zip the required files
- name: Zip files for deployment
run: |
zip -r deployment.zip appspec.yml deploy/scripts/install_dependencies.sh deploy/scripts/start_docker.sh
# Upload the ZIP file to S3
- name: Upload ZIP to S3
if: success()
run: |
aws s3 cp deployment.zip s3://text-classification-appspec-bucket/deployment.zip
# Deploy to AWS CodeDeploy using the uploaded ZIP file
- name: Deploy to AWS CodeDeploy
if: success()
run: |
aws configure set aws_access_key_id ${{ secrets.AWS_ACCESS_KEY_ID }}
aws configure set aws_secret_access_key ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws deploy create-deployment \
--application-name MyDockerApp \
--deployment-config-name CodeDeployDefault.OneAtATime \
--deployment-group-name MyDeploymentGroup \
--s3-location bucket=text-classification-appspec-bucket,key=deployment.zip,bundleType=zip \
--file-exists-behavior OVERWRITE \
--region eu-north-1
4 changes: 2 additions & 2 deletions deploy/scripts/start_docker.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ aws ecr get-login-password --region eu-north-1 | docker login --username AWS --p
# Pull the latest image
docker pull 843369994444.dkr.ecr.eu-north-1.amazonaws.com/text-classification:v3

# Check if the container 'campusx-app' is running
# Check if the container 'text-classification' is running
if [ "$(docker ps -q -f name=text-classification)" ]; then
# Stop the running container
docker stop text-classification
fi

# Check if the container 'campusx-app' exists (stopped or running)
# Check if the container 'text-classification' exists (stopped or running)
if [ "$(docker ps -aq -f name=text-classification)" ]; then
# Remove the container if it exists
docker rm text-classification
Expand Down

0 comments on commit 33be129

Please sign in to comment.