22
33on :
44 push :
5- branches : ['**'] # Trigger on push to any branch for Docker builds
5+ branches : [master]
66 pull_request :
77 branches : [master]
88
@@ -173,9 +173,9 @@ jobs:
173173 docker-build-push :
174174 name : Build and Push Docker Image
175175 runs-on : ubuntu-latest
176- # Run on push events only (not pull_request )
177- if : github.event_name == 'push'
178- # For master branch , wait for CI checks to pass; for other branches, ci-success will pass immediately
176+ # Run on push to master (build+push) and on PRs (build only )
177+ if : github.event_name == 'push' || github.event_name == 'pull_request'
178+ # For master/PR , wait for CI checks to pass
179179 needs : [ci-success]
180180 permissions :
181181 id-token : write # Required for OIDC authentication
@@ -184,24 +184,36 @@ jobs:
184184 - name : Checkout code
185185 uses : actions/checkout@v4
186186
187+ - name : Determine push eligibility
188+ id : can-push
189+ run : |
190+ if [ "${{ github.event_name }}" == "push" ]; then
191+ echo "push=true" >> $GITHUB_OUTPUT
192+ elif [ "${{ github.event.pull_request.head.repo.full_name }}" == "${{ github.repository }}" ]; then
193+ echo "push=true" >> $GITHUB_OUTPUT
194+ else
195+ echo "push=false" >> $GITHUB_OUTPUT
196+ fi
197+
187198 - name : Determine Docker tag
188199 id : docker-tag
189200 run : |
190201 if [ "${{ github.ref }}" == "refs/heads/master" ]; then
191- echo "tag= prod" >> $GITHUB_OUTPUT
202+ echo "image=633607774026.dkr.ecr.us-east-2.amazonaws.com/back-end: prod" >> $GITHUB_OUTPUT
192203 echo "environment=Production" >> $GITHUB_OUTPUT
193204 else
194- echo "tag= staging" >> $GITHUB_OUTPUT
205+ echo "image=633607774026.dkr.ecr.us-east-2.amazonaws.com/back-end: staging" >> $GITHUB_OUTPUT
195206 echo "environment=Staging" >> $GITHUB_OUTPUT
196207 fi
197- echo "Building for ${{ steps.docker-tag.outputs.environment }} with tag : ${{ steps.docker-tag.outputs.tag }}"
208+ echo "Building for ${{ steps.docker-tag.outputs.environment }} with image : ${{ steps.docker-tag.outputs.image }}"
198209
199210 - name : Set up Docker Buildx
200211 uses : docker/setup-buildx-action@v3
201212 with :
202213 platforms : linux/arm64
203214
204215 - name : Configure AWS credentials
216+ if : steps.can-push.outputs.push == 'true'
205217 uses : aws-actions/configure-aws-credentials@v4
206218 with :
207219 role-to-assume : ${{ secrets.AWS_ROLE_ARN }}
@@ -210,6 +222,7 @@ jobs:
210222
211223 - name : Login to Amazon ECR
212224 id : login-ecr
225+ if : steps.can-push.outputs.push == 'true'
213226 uses : aws-actions/amazon-ecr-login@v2
214227
215228 - name : Build and push Docker image
@@ -218,17 +231,18 @@ jobs:
218231 context : .
219232 target : runtime
220233 platforms : linux/arm64
221- push : true
234+ push : ${{ steps.can-push.outputs.push == ' true' }}
222235 tags : |
223- 633607774026.dkr.ecr.us-east-2.amazonaws.com/back-end: ${{ steps.docker-tag.outputs.tag }}
236+ ${{ steps.docker-tag.outputs.image }}
224237 provenance : false
225238 cache-from : type=gha
226239 cache-to : type=gha,mode=max
227240
228241 - name : Output image URI
242+ if : steps.can-push.outputs.push == 'true'
229243 run : |
230244 echo "Successfully pushed ${{ steps.docker-tag.outputs.environment }} image:"
231- echo "633607774026.dkr.ecr.us-east-2.amazonaws.com/back-end: ${{ steps.docker-tag.outputs.tag }}"
245+ echo "${{ steps.docker-tag.outputs.image }}"
232246
233247 # Final status check for branch protection
234248 ci-success :
0 commit comments