From b12a62afd46756266f08b1a6fac05634342c2807 Mon Sep 17 00:00:00 2001
From: Erik Schierboom <erik_schierboom@hotmail.com>
Date: Tue, 6 Apr 2021 16:28:21 +0200
Subject: [PATCH] [CI] Add workflow to push Docker image to Dockerhub and ECR
 (#9)

---
 .github/workflows/deploy.yml    | 59 ---------------------------------
 .github/workflows/docker.yml    | 54 ++++++++++++++++++++++++++++++
 .github/workflows/dockerhub.yml | 25 --------------
 3 files changed, 54 insertions(+), 84 deletions(-)
 delete mode 100644 .github/workflows/deploy.yml
 create mode 100644 .github/workflows/docker.yml
 delete mode 100644 .github/workflows/dockerhub.yml

diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml
deleted file mode 100644
index a80a9bf..0000000
--- a/.github/workflows/deploy.yml
+++ /dev/null
@@ -1,59 +0,0 @@
-name: Deploy to Amazon ECR
-
-env:
-  aws_region: eu-west-2
-  ecr_repository: ${{ github.event.repository.name }}
-  dockerfile: "Dockerfile"
-
-on:
-  push:
-    branches: [master]
-
-jobs:
-  deploy:
-    name: Upload image to ECR
-    runs-on: ubuntu-latest
-    steps:
-      ###############
-      ###############
-      # Setup Steps #
-      ###############
-      ###############
-      - name: Checkout
-        uses: actions/checkout@v2
-
-      - uses: satackey/action-docker-layer-caching@v0.0.5
-        continue-on-error: true
-        with:
-          concurrency: 30
-
-      - name: Configure AWS credentials
-        uses: aws-actions/configure-aws-credentials@v1
-        with:
-          aws-access-key-id: ${{ secrets.AWS_ECR_ACCESS_KEY_ID }}
-          aws-secret-access-key: ${{ secrets.AWS_ECR_SECRET_ACCESS_KEY }}
-          aws-region: ${{ env.aws_region }}
-
-      - name: Login to Amazon ECR
-        id: login_to_ecr
-        uses: aws-actions/amazon-ecr-login@v1
-
-      ###################
-      ###################
-      # Build the image #
-      ###################
-      ###################
-      - name: Build and Push Docker image
-        env:
-          ECR_REGISTRY: ${{ steps.login_to_ecr.outputs.registry }}
-          ECR_REPOSITORY: ${{ env.ecr_repository }}
-          IMAGE_TAG: "${{ github.sha }}"
-          DOCKERFILE: ${{ env.dockerfile }}
-        run: |
-          # Build a docker container and push it to ECR
-          docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG -f $DOCKERFILE .
-          docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
-          
-          # Retag this as the production tag to deploy it
-          docker tag $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG $ECR_REGISTRY/$ECR_REPOSITORY:production
-          docker push $ECR_REGISTRY/$ECR_REPOSITORY:production
diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml
new file mode 100644
index 0000000..3558005
--- /dev/null
+++ b/.github/workflows/docker.yml
@@ -0,0 +1,54 @@
+name: Push Docker images to DockerHub and ECR
+
+on:
+  push:
+    branches: [main, master]
+
+jobs:
+  multiple-registries:
+    runs-on: ubuntu-latest
+
+    env:
+      ECR_REGISTRY: ${{ secrets.AWS_ACCOUNT_ID }}.dkr.ecr.${{ secrets.AWS_REGION }}.amazonaws.com
+
+    steps:
+      - name: Checkout code
+        uses: actions/checkout@5a4ac9002d0be2fb38bd78e4b4dbde5606d7042f # 2.3.4
+
+      - name: Set up Docker Buildx
+        uses: docker/setup-buildx-action@154c24e1f33dbb5865a021c99f1318cfebf27b32 # 1.1.1
+
+      - name: Cache Docker layers
+        uses: actions/cache@0781355a23dac32fd3bac414512f4b903437991a # 2.1.3
+        with:
+          path: /tmp/.buildx-cache
+          key: ${{ runner.os }}-buildx-${{ github.sha }}
+          restore-keys: |
+            ${{ runner.os }}-buildx-
+
+      - name: Login to DockerHub
+        uses: docker/login-action@f3364599c6aa293cdc2b8391b1b56d0c30e45c8a # 1.8.0
+        with:
+          username: ${{ secrets.DOCKERHUB_USERNAME }}
+          password: ${{ secrets.DOCKERHUB_PASSWORD }}
+
+      - name: Login to ECR
+        uses: docker/login-action@f3364599c6aa293cdc2b8391b1b56d0c30e45c8a # 1.8.0
+        with:
+          registry: ${{ env.ECR_REGISTRY }}
+          username: ${{ secrets.AWS_ECR_ACCESS_KEY_ID }}
+          password: ${{ secrets.AWS_ECR_SECRET_ACCESS_KEY }}
+
+      - name: Build and push Docker image
+        uses: docker/build-push-action@0db984c1826869dcd0740ff26ff75ff543238fd9 # 2.2.2
+        with:
+          context: .
+          file: ./Dockerfile
+          push: true
+          tags: |
+            ${{ github.event.repository.full_name }}:latest
+            ${{ github.event.repository.full_name }}:${{ github.sha }}
+            ${{ env.ECR_REGISTRY }}/${{ github.event.repository.name }}:production
+            ${{ env.ECR_REGISTRY }}/${{ github.event.repository.name }}:${{ github.sha }}
+          cache-from: type=local,src=/tmp/.buildx-cache
+          cache-to: type=local,dest=/tmp/.buildx-cache
diff --git a/.github/workflows/dockerhub.yml b/.github/workflows/dockerhub.yml
deleted file mode 100644
index 6d78d03..0000000
--- a/.github/workflows/dockerhub.yml
+++ /dev/null
@@ -1,25 +0,0 @@
-name: Push to DockerHub
-
-on:
-  push:
-    branches: [master]
-
-jobs:
-  dockerhub:
-    runs-on: ubuntu-latest
-
-    steps:
-      - name: Checkout code
-        uses: actions/checkout@v2
-
-      - uses: satackey/action-docker-layer-caching@v0.0.5
-
-      - name: Build and push Docker images
-        uses: docker/build-push-action@v1.1.0
-        with:
-          username: ${{ secrets.DOCKERHUB_USERNAME }}
-          password: ${{ secrets.DOCKERHUB_PASSWORD }}
-          repository: ${{ github.event.repository.full_name }}
-          tag_with_sha: true
-          tags: latest
-          dockerfile: Dockerfile