Skip to content

Commit ce0092e

Browse files
Skip conditionals
- Update build workflow to skip build if the image tag already exists
1 parent 6308332 commit ce0092e

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

.github/workflows/build-and-push-image.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,38 @@ on:
55
workflow_call:
66

77
jobs:
8+
# job to check if image tag is already present in ECR
9+
check-image-tag:
10+
runs-on: ubuntu-latest
11+
permissions:
12+
id-token: write
13+
steps:
14+
- name: Checkout code
15+
uses: actions/checkout@v4
16+
- name: Configure AWS Credentials
17+
uses: aws-actions/configure-aws-credentials@v4
18+
with:
19+
role-to-assume: arn:aws:iam::820242920762:role/GitHubActionsRole
20+
aws-region: eu-west-2
21+
- name: Login to ECR
22+
id: login-ecr
23+
uses: aws-actions/amazon-ecr-login@v2
24+
- name: Check if image tag exists
25+
id: check-if-image-exists
26+
run: |
27+
DOCKER_IMAGE="${{ steps.login-ecr.outputs.registry }}/mavis/webapp:${{ github.sha }}"
28+
aws ecr describe-images --repository-name mavis/webapp --image-ids imageTag="${{ github.sha }}" --region eu-west-2
29+
if [ $? -eq 0 ]; then
30+
echo "Image exists skipping build and push"
31+
echo "IMAGE_EXISTS=True" >> $GITHUB_OUTPUT
32+
else
33+
echo "Image does not exist"
34+
echo "IMAGE_EXISTS=False" >> $GITHUB_OUTPUT
35+
fi
36+
outputs:
37+
IMAGE_EXISTS: ${{ steps.check-if-image-exists.outputs.IMAGE_EXISTS }}
838
build:
39+
if: jobs.check-image-tag.outputs.IMAGE_EXISTS == 'False'
940
runs-on: ubuntu-latest
1041
steps:
1142
- name: Checkout code

.github/workflows/deploy-mavis.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,8 @@ on:
2424

2525
jobs:
2626
build-and-push-image:
27-
if: github.event_name == 'workflow_dispatch'
2827
uses: ./.github/workflows/build-and-push-image.yml
2928
deploy-infrastructure:
30-
if: github.event_name == 'workflow_call'
3129
needs: build-and-push-image
3230
uses: ./.github/workflows/deploy-infrastructure.yml
3331
with:

0 commit comments

Comments
 (0)