Skip to content

Push images til ECR

Example

name: Deploy ECS Fargate to TEST

on:
  push:
    branches: [master]
  workflow_dispatch:

jobs:
  build_push_image_to_shared:
    name: Build image and publish to ECR
    runs-on: ubuntu-latest
    permissions:
      id-token: write
      contents: read
    steps:
      - name: "Build Kattehotell image and push to ECR"
        uses: BYM-IKT/github-actions/build-and-push-image-to-ecr@master
        with:
          aws-account-id:      "<<AWS-ACCOUNT-ID>>"   # Mandatory
          ecr-name:            "kattehotell-service"  # Mandatory
          docker-context-path: "KattehotellService"   # Optional: Defaults to "."
          dockerfile-path:     "Kattehotell.service/Dockerfile"  # Optional: Defaults to "./Dockerfile"
          # Optional: Defaults to no extra build-arguments
          # Syntax: Multiline-string "NAME=VALUE"
          docker-build-args: |                        
            NUGET_AUTH_TOKEN=${{ secrets.PACKAGES_GITHUB_ACCESS_TOKEN }}
            KATTEHOTELL_ID=123

          # Optional: Defaults to 3 tags: 'latest', current commit SHA, and a generated deployment ID.
          # Syntax: Multiline-string "NAME=VALUE"
          image-tags: |                               
            latest
            my_other_tag
            my_last_tag
Replace <<AWS_ACCOUNT_ID>> with the AWS Account ID where the ECR repository is hosted.

Note

The workflow requires the following GitHub Actions permissions:

permissions:
  id-token: write
  contents: read

The repository must also be configured to assume an AWS IAM role with permissions to push images to ECR.

Other Scenarios

Pushing Multiple Images

You have the following project at hand:

Example project
.
├── Lambda.CreateBooking/
│   ├── ...
│   └── Dockerfile
└── Lambda.DeleteBooking/
    ├── ...
    └── Dockerfile

You can build both projects and push the images by adding the following workflow-file:

.github/workflows/cd.yml
name: Continuous Deployment

on:
  push:
    branches: [master]

jobs:
  build_push_image_to_lambda:
    name: Build and push images to lambda
    runs-on: ubuntu-latest
    permissions:
      id-token: write
      contents: read
    steps:

    - name: "Lambda.CreateBooking"
      uses: BYM-IKT/github-actions/build-and-push-image-to-ecr@master
      with:
        aws-account-id:      "<<AWS-ACCOUNT-ID>>"
        ecr-name:            "kattehotell-create-booking"   
        docker-context-path: "Lambda.CreateBooking"

    - name: "Lambda.DeleteBooking"
      uses: BYM-IKT/github-actions/build-and-push-image-to-ecr@master
      with:
        aws-account-id:      "<<AWS-ACCOUNT-ID>>"
        ecr-name:            "kattehotell-delete-booking"      
        docker-context-path: "Lambda.DeleteBooking"

Changing build context and target Dockerfile

The build-and-push-image-to-ecr action uses the repository root (./) as the default Docker build context and assumes the Dockerfile is located at ./Dockerfile.

In some projects this is not the correct assumption.

Case 1: Default Configuration

Example project
.   # <-- Build Context
├── ...
├── Program.cs
└── Dockerfile  # <-- Target this Dockerfile

Via GitHub Actions:

1
2
3
4
5
- name: "Lambda.CreateBooking"
  uses: BYM-IKT/github-actions/build-and-push-image-to-ecr@master
  with:
    aws-account-id:      "<<AWS-ACCOUNT-ID>>"
    ecr-name:            "kattehotell-create-booking"   

Which executes the following command:

docker build .

Case 2: Custom Build Context

Example project
.
├── Lambda.CreateBooking/
│   ├── ...         # <-- Build Context
│   └── Dockerfile  # <-- Target this Dockerfile
└── Lambda.DeleteBooking/
    ├── ...
    └── Dockerfile

Via GitHub Actions:

1
2
3
4
5
6
- name: "Lambda.CreateBooking"
  uses: BYM-IKT/github-actions/build-and-push-image-to-ecr@master
  with:
    aws-account-id:      "<<AWS-ACCOUNT-ID>>"
    ecr-name:            "kattehotell-create-booking"   
    docker-context-path: "Lambda.CreateBooking"

Which executes the following command:

docker build Lambda.CreateBooking/

Case 3: Separate Build Context and Dockerfile Location

Example project
.   # <-- Build Context

├── Lambda.CreateBooking/
│   ├── ...
│   └── Dockerfile  # <-- Target this Dockerfile
└── Lambda.DeleteBooking/
    ├── ...
    └── Dockerfile

Via GitHub Actions:

1
2
3
4
5
6
7
- name: "Lambda.CreateBooking"
  uses: BYM-IKT/github-actions/build-and-push-image-to-ecr@master
  with:
    aws-account-id:      "<<AWS-ACCOUNT-ID>>"
    ecr-name:            "kattehotell-create-booking"   
    docker-context-path: "."
    dockerfile-path:     "Lambda.CreateBooking/Dockerfile"

Which executes the following command:

docker build --file Lambda.CreateBooking/Dockerfile .