← All examples
Pipelines dockerawsecr

Docker Build & Push

Build a Docker image and push it to a registry, gated by a precondition check for AWS credentials.

Replaces: a hand-rolled release.sh script

Builds and tags a Docker image, then authenticates and pushes to ECR. The preconditions block fails fast with a clear message if AWS credentials aren’t configured, instead of letting docker push fail deep into the run with a cryptic auth error. Mark this high risk once it’s pushing to a shared registry other services pull from — an agent calling it would need explicit confirmation.

version: v1

tasks:
  docker-build:
    description: "Build the Docker image"
    cmds:
      - docker build -t $IMAGE_NAME:$IMAGE_TAG .
      - docker tag $IMAGE_NAME:$IMAGE_TAG $IMAGE_NAME:latest
    env:
      IMAGE_NAME:
        type: string
        default: "myapp"
      IMAGE_TAG:
        type: string
        default: "latest"

  ecr-push:
    description: "Push the image to AWS ECR"
    agent:
      risk_level: high
      requires_approval: true
      confirmation: "This pushes to the shared ECR registry. Continue?"
    cmds:
      - aws ecr get-login-password --region $AWS_REGION | docker login --username AWS --password-stdin $AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com
      - docker tag $IMAGE_NAME:$IMAGE_TAG $AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com/$ECR_REPO:$IMAGE_TAG
      - docker push $AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com/$ECR_REPO:$IMAGE_TAG
    env:
      AWS_REGION:
        type: string
        default: "us-east-1"
      AWS_ACCOUNT_ID:
        type: string
        required: true
      ECR_REPO:
        type: string
        required: true
      IMAGE_NAME:
        type: string
        default: "myapp"
      IMAGE_TAG:
        type: string
        default: "latest"
    preconditions:
      - cmds:
          - aws sts get-caller-identity
        on_failure:
          continue: false
          cmds:
            - echo "AWS credentials not configured. Run 'aws configure' or set AWS_PROFILE."

jobs:
  release:
    description: "Build and push the release image"
    tasks:
      - docker-build
      - ecr-push

Run it:

meriadoc job release --env ECR_REPO=myapp --env AWS_ACCOUNT_ID=123456789012