Skip to content

Commit 76781c1

Browse files
committed
cicd docker builds new everytime
1 parent e8296db commit 76781c1

1 file changed

Lines changed: 27 additions & 14 deletions

File tree

.github/workflows/cicd.yaml

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,71 @@
11
name: Deploy Application Docker Image to EC2 instance
2-
#lets do it after adding secrets - lets do it
2+
33
on:
44
push:
55
branches: [main]
66

7-
87
jobs:
98
Continuous-Integration:
109
runs-on: ubuntu-latest
1110

1211
steps:
13-
- name: Checkout
14-
uses: actions/checkout@v2
12+
- name: Checkout source code
13+
uses: actions/checkout@v3
1514

1615
- name: Configure AWS credentials
17-
uses: aws-actions/configure-aws-credentials@v1
16+
uses: aws-actions/configure-aws-credentials@v2
1817
with:
1918
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
2019
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
2120
aws-region: ${{ secrets.AWS_DEFAULT_REGION }}
2221

2322
- name: Login to Amazon ECR
2423
id: login-ecr
25-
uses: aws-actions/amazon-ecr-login@v1
24+
uses: aws-actions/amazon-ecr-login@v2
2625

2726
- name: Build, tag, and push image to Amazon ECR
28-
id: build-image
2927
env:
3028
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
3129
ECR_REPOSITORY: ${{ secrets.ECR_REPO }}
3230
IMAGE_TAG: latest
3331
run: |
34-
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG .
32+
docker build --no-cache -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG .
3533
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
36-
echo "::set-output name=image::$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG"
3734
3835
Continuous-Deployment:
3936
needs: Continuous-Integration
4037
runs-on: self-hosted
38+
4139
steps:
42-
- name: Checkout
40+
- name: Checkout source code
4341
uses: actions/checkout@v3
4442

4543
- name: Configure AWS credentials
46-
uses: aws-actions/configure-aws-credentials@v1
44+
uses: aws-actions/configure-aws-credentials@v2
4745
with:
4846
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
4947
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
5048
aws-region: ${{ secrets.AWS_DEFAULT_REGION }}
5149

5250
- name: Login to Amazon ECR
5351
id: login-ecr
54-
uses: aws-actions/amazon-ecr-login@v1
52+
uses: aws-actions/amazon-ecr-login@v2
5553

56-
- name: Run Docker Image to serve users
54+
- name: Deploy latest Docker image
5755
run: |
58-
docker run -d -e AWS_ACCESS_KEY_ID="${{ secrets.AWS_ACCESS_KEY_ID }}" -e AWS_SECRET_ACCESS_KEY="${{ secrets.AWS_SECRET_ACCESS_KEY }}" -e AWS_DEFAULT_REGION="${{ secrets.AWS_DEFAULT_REGION }}" -e PINECONE_API_KEY="${{ secrets.PINECONE_API_KEY }}" -e GROQ_API_KEY="${{ secrets.GROQ_API_KEY }}" -p 8080:8080 "${{ steps.login-ecr.outputs.registry }}"/"${{ secrets.ECR_REPO }}":latest
56+
echo "Stopping old containers..."
57+
docker stop $(docker ps -aq) || true
58+
docker rm $(docker ps -aq) || true
59+
60+
echo "Pulling latest image..."
61+
docker pull ${{ steps.login-ecr.outputs.registry }}/${{ secrets.ECR_REPO }}:latest
62+
63+
echo "Running new container..."
64+
docker run -d --restart always \
65+
-p 8080:8080 \
66+
-e AWS_ACCESS_KEY_ID="${{ secrets.AWS_ACCESS_KEY_ID }}" \
67+
-e AWS_SECRET_ACCESS_KEY="${{ secrets.AWS_SECRET_ACCESS_KEY }}" \
68+
-e AWS_DEFAULT_REGION="${{ secrets.AWS_DEFAULT_REGION }}" \
69+
-e PINECONE_API_KEY="${{ secrets.PINECONE_API_KEY }}" \
70+
-e GROQ_API_KEY="${{ secrets.GROQ_API_KEY }}" \
71+
${{ steps.login-ecr.outputs.registry }}/${{ secrets.ECR_REPO }}:latest

0 commit comments

Comments
 (0)