Skip to content

Commit

Permalink
Refactor build-push-image action.yaml to conditionally build and push…
Browse files Browse the repository at this point in the history
… image
  • Loading branch information
adamlahbib committed Nov 16, 2024
1 parent 35990e4 commit fa4cd14
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 8 deletions.
9 changes: 9 additions & 0 deletions .github/actions/build-push-image/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ inputs:
stage:
description: 'Deployment stage'
required: true
buildable:
description: 'Whether the image should be built and pushed'
required: true
default: 'true'

outputs:
image:
Expand All @@ -31,29 +35,34 @@ runs:
using: 'composite'
steps:
- name: Set up Docker Buildx
if: ${{ inputs.buildable == 'true' }}
uses: docker/setup-buildx-action@v3
with:
version: latest

- name: Configure AWS Credentials
if: ${{ inputs.buildable == 'true' }}
uses: aws-actions/configure-aws-credentials@v4
with:
aws-access-key-id: ${{ inputs.aws-access-key-id }}
aws-secret-access-key: ${{ inputs.aws-secret-access-key }}
aws-region: ${{ inputs.aws-region }}

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

- name: Tag Image
if: ${{ inputs.buildable == 'true' }}
id: image-uri
shell: bash
run: |
tag=${{ steps.login-ecr.outputs.registry }}/${{ inputs.ecr-repository }}
echo "tag=$tag" >> $GITHUB_OUTPUT
- name: Build and Push Image
if: ${{ inputs.buildable == 'true' }}
uses: docker/build-push-action@v5
with:
push: true
Expand Down
37 changes: 29 additions & 8 deletions .github/workflows/sync-and-deploy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,15 @@ jobs:
echo "stage=dev" >> $GITHUB_ENV
fi
- name: Check for Changes
id: find_changes
run: |
if [ -z "$(git diff --name-only ${{ github.event.before }} ${{ github.sha }} -- app)" ]; then
echo "::set-output name=deploy::false"
else
echo "::set-output name=deploy::true"
fi
- name: Build, tag, and push image to Amazon ECR
id: build-push-image
uses: ./.github/actions/build-push-image
Expand All @@ -81,29 +90,41 @@ jobs:
ecr-repository: ${{ env.ECR_REPOSITORY }}
dockerfile: ./Dockerfile
stage: ${{ env.stage }}
buildable: ${{ steps.find_changes.outputs.deploy }}

- name: Update Kubeconfig
run: aws eks update-kubeconfig --name ${{ env.EKS_CLUSTER_NAME }} --region ${{ env.AWS_REGION }}
run: |
if [ "${{ steps.find_changes.outputs.deploy }}" != "false" ]; then
aws eks update-kubeconfig --name ${{ env.EKS_CLUSTER_NAME }} --region ${{ env.AWS_REGION }}
fi
- name: Deploy to EKS
working-directory: ./k8s
env:
IMAGE: ${{ steps.build-push-image.outputs.image }}
STAGE: ${{ env.stage }}
run: |
sed -i "s|{{IMAGE}}|${IMAGE}|g" ${STAGE}/deployment.yaml
kubectl apply -f ${STAGE}
if [ "${{ steps.find_changes.outputs.deploy }}" != "false" ]; then
sed -i "s|{{IMAGE}}|${IMAGE}|g" ${STAGE}/deployment.yaml
kubectl apply -f ${STAGE}
fi
- name: Verify Deployment
id: verify-deployment
env:
STAGE: ${{ env.stage }}
shell: bash
run: |
kubectl rollout status deployment/app -n ${STAGE} --timeout=5m
if [ $? -eq 0 ]; then
echo "status=Deployment successful!" >> $GITHUB_OUTPUT
if [ "${{ steps.find_changes.outputs.deploy }}" != "false" ]; then
kubectl rollout status deployment/app -n ${STAGE} --timeout=5m
if [ $? -eq 0 ]; then
echo "status=Deployment successful!" >> $GITHUB_OUTPUT
else
echo "status=Deployment failed!" >> $GITHUB_OUTPUT
exit 1
fi
else
echo "status=Deployment failed!" >> $GITHUB_OUTPUT
exit 1
echo "status=No changes to deploy" >> $GITHUB_OUTPUT
fi
notify-slack:
Expand Down

0 comments on commit fa4cd14

Please sign in to comment.