Push images til ECR

Reference

name: "Build and push images 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:     "MyLambda/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, og generated Deployment-ID.
  # Syntax: Multiline-string "NAME=VALUE"
  image-tags: |                               
    latest
    my_other_tag
    my_last_tag

Examples

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: Continous Deployment

on:
  push:
    branches: [master]

jobs:
  build_push_image_to_ecr:
    name: Build and push images to ECR
    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 defaults the build context to the root directory (./) and assumes that the Dockerfile resides in same directory (i.e. ./Dockerfile).

In some projects this will not be the correct assumption.

Case 1: The default case

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 effectively executes the following command:

docker build .

Case 2: Build context is not the root directory

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 effectively executes the following command:

docker build Lambda.CreateBooking/

Case 3: Build context and Dockerfile reside in different directories

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 effectively executes the following command:

docker build --file Lambda.CreateBooking/Dockerfile .