Skip to content

Commit f105589

Browse files
committed
Refactor CI/CD workflow to update AWS actions to v2, improve container management, and enhance code organization with clear section headers
1 parent 793527d commit f105589

1 file changed

Lines changed: 32 additions & 26 deletions

File tree

.github/workflows/cicd.yaml

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ permissions:
1212
contents: read
1313

1414
jobs:
15+
# -----------------------------
16+
# Continuous Integration
17+
# -----------------------------
1518
integration:
1619
name: Continuous Integration
1720
runs-on: ubuntu-latest
@@ -25,6 +28,9 @@ jobs:
2528
- name: Run unit tests
2629
run: echo "Running unit tests"
2730

31+
# -----------------------------
32+
# Build & Push Docker Image
33+
# -----------------------------
2834
build-and-push-ecr-image:
2935
name: Continuous Delivery
3036
needs: integration
@@ -37,65 +43,65 @@ jobs:
3743
run: |
3844
sudo apt-get update
3945
sudo apt-get install -y jq unzip
46+
4047
- name: Configure AWS credentials
41-
uses: aws-actions/configure-aws-credentials@v1
48+
uses: aws-actions/configure-aws-credentials@v2
4249
with:
4350
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
4451
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
4552
aws-region: ${{ secrets.AWS_REGION }}
4653

4754
- name: Login to Amazon ECR
4855
id: login-ecr
49-
uses: aws-actions/amazon-ecr-login@v1
56+
uses: aws-actions/amazon-ecr-login@v2
5057

5158
- name: Build, tag, and push image to Amazon ECR
52-
id: build-image
5359
env:
5460
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
5561
ECR_REPOSITORY: ${{ secrets.ECR_REPOSITORY_NAME }}
5662
IMAGE_TAG: latest
5763
run: |
58-
# Build a docker container and
59-
# push it to ECR so that it can
60-
# be deployed to ECS.
6164
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG .
6265
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
63-
echo "::set-output name=image::$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG"
6466
65-
Continuous-Deployment:
67+
# -----------------------------
68+
# Continuous Deployment (EC2)
69+
# -----------------------------
70+
continuous-deployment:
71+
name: Continuous Deployment
6672
needs: build-and-push-ecr-image
6773
runs-on: self-hosted
6874
steps:
69-
- name: Checkout
75+
- name: Checkout Code
7076
uses: actions/checkout@v3
7177

7278
- name: Configure AWS credentials
73-
uses: aws-actions/configure-aws-credentials@v1
79+
uses: aws-actions/configure-aws-credentials@v2
7480
with:
7581
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
7682
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
7783
aws-region: ${{ secrets.AWS_REGION }}
7884

7985
- name: Login to Amazon ECR
80-
id: login-ecr
81-
uses: aws-actions/amazon-ecr-login@v1
86+
uses: aws-actions/amazon-ecr-login@v2
8287

83-
- name: Pull latest images
88+
- name: Pull latest image
8489
run: |
85-
docker pull ${{secrets.AWS_ECR_LOGIN_URI}}/${{ secrets.ECR_REPOSITORY_NAME }}:latest
90+
docker pull ${{ secrets.AWS_ECR_LOGIN_URI }}/${{ secrets.ECR_REPOSITORY_NAME }}:latest
8691
87-
- name: Stop and remove container if exists
92+
- name: Stop and remove existing container (safe)
8893
run: |
89-
if [ "$(docker ps -aq -f name=cnnclassifier)" ]; then
90-
docker stop cnnclassifier
91-
docker rm -fv cnnclassifier
92-
else
93-
echo "No existing container found"
94-
fi
95-
96-
- name: Run Docker Image to serve users
94+
docker stop cnnclassifier || true
95+
docker rm cnnclassifier || true
96+
97+
- name: Run Docker container
9798
run: |
98-
docker run -d -p 8080:8080 --name=cnnclassifier -e 'AWS_ACCESS_KEY_ID=${{ secrets.AWS_ACCESS_KEY_ID }}' -e 'AWS_SECRET_ACCESS_KEY=${{ secrets.AWS_SECRET_ACCESS_KEY }}' -e 'AWS_REGION=${{ secrets.AWS_REGION }}' ${{secrets.AWS_ECR_LOGIN_URI}}/${{ secrets.ECR_REPOSITORY_NAME }}:latest
99-
- name: Clean previous images and containers
99+
docker run -d \
100+
--restart unless-stopped \
101+
-p 80:8080 \
102+
--name cnnclassifier \
103+
${{ secrets.AWS_ECR_LOGIN_URI }}/${{ secrets.ECR_REPOSITORY_NAME }}:latest
104+
105+
- name: Cleanup unused images (safe)
100106
run: |
101-
docker system prune -f
107+
docker image prune -f

0 commit comments

Comments
 (0)