diff --git a/.github/actions/build-push-image/action.yaml b/.github/actions/build-push-image/action.yaml index c1a6054..65bfed9 100644 --- a/.github/actions/build-push-image/action.yaml +++ b/.github/actions/build-push-image/action.yaml @@ -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: @@ -31,11 +35,13 @@ 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 }} @@ -43,10 +49,12 @@ runs: 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: | @@ -54,6 +62,7 @@ runs: echo "tag=$tag" >> $GITHUB_OUTPUT - name: Build and Push Image + if: ${{ inputs.buildable == 'true' }} uses: docker/build-push-action@v5 with: push: true diff --git a/.github/workflows/sync-and-deploy.yaml b/.github/workflows/sync-and-deploy.yaml index afd0b38..ad624a7 100644 --- a/.github/workflows/sync-and-deploy.yaml +++ b/.github/workflows/sync-and-deploy.yaml @@ -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 @@ -81,9 +90,13 @@ 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 @@ -91,19 +104,27 @@ jobs: 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: