Skip to content

Commit fa4cd14

Browse files
committed
Refactor build-push-image action.yaml to conditionally build and push image
1 parent 35990e4 commit fa4cd14

File tree

2 files changed

+38
-8
lines changed

2 files changed

+38
-8
lines changed

.github/actions/build-push-image/action.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ inputs:
2121
stage:
2222
description: 'Deployment stage'
2323
required: true
24+
buildable:
25+
description: 'Whether the image should be built and pushed'
26+
required: true
27+
default: 'true'
2428

2529
outputs:
2630
image:
@@ -31,29 +35,34 @@ runs:
3135
using: 'composite'
3236
steps:
3337
- name: Set up Docker Buildx
38+
if: ${{ inputs.buildable == 'true' }}
3439
uses: docker/setup-buildx-action@v3
3540
with:
3641
version: latest
3742

3843
- name: Configure AWS Credentials
44+
if: ${{ inputs.buildable == 'true' }}
3945
uses: aws-actions/configure-aws-credentials@v4
4046
with:
4147
aws-access-key-id: ${{ inputs.aws-access-key-id }}
4248
aws-secret-access-key: ${{ inputs.aws-secret-access-key }}
4349
aws-region: ${{ inputs.aws-region }}
4450

4551
- name: Login to Amazon ECR
52+
if: ${{ inputs.buildable == 'true' }}
4653
id: login-ecr
4754
uses: aws-actions/amazon-ecr-login@v2
4855

4956
- name: Tag Image
57+
if: ${{ inputs.buildable == 'true' }}
5058
id: image-uri
5159
shell: bash
5260
run: |
5361
tag=${{ steps.login-ecr.outputs.registry }}/${{ inputs.ecr-repository }}
5462
echo "tag=$tag" >> $GITHUB_OUTPUT
5563
5664
- name: Build and Push Image
65+
if: ${{ inputs.buildable == 'true' }}
5766
uses: docker/build-push-action@v5
5867
with:
5968
push: true

.github/workflows/sync-and-deploy.yaml

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,15 @@ jobs:
7171
echo "stage=dev" >> $GITHUB_ENV
7272
fi
7373
74+
- name: Check for Changes
75+
id: find_changes
76+
run: |
77+
if [ -z "$(git diff --name-only ${{ github.event.before }} ${{ github.sha }} -- app)" ]; then
78+
echo "::set-output name=deploy::false"
79+
else
80+
echo "::set-output name=deploy::true"
81+
fi
82+
7483
- name: Build, tag, and push image to Amazon ECR
7584
id: build-push-image
7685
uses: ./.github/actions/build-push-image
@@ -81,29 +90,41 @@ jobs:
8190
ecr-repository: ${{ env.ECR_REPOSITORY }}
8291
dockerfile: ./Dockerfile
8392
stage: ${{ env.stage }}
93+
buildable: ${{ steps.find_changes.outputs.deploy }}
8494

8595
- name: Update Kubeconfig
86-
run: aws eks update-kubeconfig --name ${{ env.EKS_CLUSTER_NAME }} --region ${{ env.AWS_REGION }}
96+
run: |
97+
if [ "${{ steps.find_changes.outputs.deploy }}" != "false" ]; then
98+
aws eks update-kubeconfig --name ${{ env.EKS_CLUSTER_NAME }} --region ${{ env.AWS_REGION }}
99+
fi
87100
88101
- name: Deploy to EKS
89102
working-directory: ./k8s
90103
env:
91104
IMAGE: ${{ steps.build-push-image.outputs.image }}
92105
STAGE: ${{ env.stage }}
93106
run: |
94-
sed -i "s|{{IMAGE}}|${IMAGE}|g" ${STAGE}/deployment.yaml
95-
kubectl apply -f ${STAGE}
107+
if [ "${{ steps.find_changes.outputs.deploy }}" != "false" ]; then
108+
sed -i "s|{{IMAGE}}|${IMAGE}|g" ${STAGE}/deployment.yaml
109+
kubectl apply -f ${STAGE}
110+
fi
96111
97112
- name: Verify Deployment
98113
id: verify-deployment
114+
env:
115+
STAGE: ${{ env.stage }}
99116
shell: bash
100117
run: |
101-
kubectl rollout status deployment/app -n ${STAGE} --timeout=5m
102-
if [ $? -eq 0 ]; then
103-
echo "status=Deployment successful!" >> $GITHUB_OUTPUT
118+
if [ "${{ steps.find_changes.outputs.deploy }}" != "false" ]; then
119+
kubectl rollout status deployment/app -n ${STAGE} --timeout=5m
120+
if [ $? -eq 0 ]; then
121+
echo "status=Deployment successful!" >> $GITHUB_OUTPUT
122+
else
123+
echo "status=Deployment failed!" >> $GITHUB_OUTPUT
124+
exit 1
125+
fi
104126
else
105-
echo "status=Deployment failed!" >> $GITHUB_OUTPUT
106-
exit 1
127+
echo "status=No changes to deploy" >> $GITHUB_OUTPUT
107128
fi
108129
109130
notify-slack:

0 commit comments

Comments
 (0)