Interview Prep/Real-World Interview/Write-a-pipeline tasks

Write a workflow that builds a Docker image and pushes it to ECR using OIDC (no access keys).

IntermediateHands-onWrite-a-pipeline tasks
Answer
name: build-push
on:
  push:
    branches: [main]

permissions:
  id-token: write
  contents: read

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: aws-actions/configure-aws-credentials@v4
        with:
          role-to-assume: arn:aws:iam::123456789012:role/gha-ecr-push
          aws-region: ap-south-1
      - id: ecr
        uses: aws-actions/amazon-ecr-login@v2
      - uses: docker/setup-buildx-action@v3
      - uses: docker/build-push-action@v6
        with:
          push: true
          tags: ${{ steps.ecr.outputs.registry }}/myapp:${{ github.sha }}
          cache-from: type=gha
          cache-to: type=gha,mode=max

Key trick → id-token: write lets the job request an OIDC token that AWS trades for short-lived credentials. The IAM role's trust policy must restrict the sub claim to this repo and branch.

All Real-World Interview questions Roadmap: Interview readiness

More on Write-a-pipeline tasks