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.
More on Write-a-pipeline tasks
Write a GitHub Actions workflow that runs tests on every PR and push to main.BeginnerWrite a matrix job that tests on Python 3.10, 3.11 and 3.12 on Linux and Windows.BeginnerWrite a declarative Jenkinsfile with build, test, Docker push and a manual approval before prod.AdvancedWrite a GitHub Actions deploy job that requires approval for production using environments.BeginnerWrite a reusable workflow and call it from another repo.IntermediateWrite a workflow that only runs the backend job when files under `backend/` change.Intermediate