From 929b32362088851b465df1d90512c63a81dc1867 Mon Sep 17 00:00:00 2001
From: BlueWhaleKo <koko8624@gmail.com>
Date: Tue, 22 Feb 2022 16:18:58 +0900
Subject: [PATCH] feature: support linux/arm64 architecture

---
 Makefile                  |  2 ++
 scripts/build-and-push.sh | 60 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 62 insertions(+)
 create mode 100644 Makefile
 create mode 100755 scripts/build-and-push.sh

diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..dbd6ee7
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,2 @@
+build-and-push:
+	./scripts/build-and-push.sh ghcr.io/rtcamp/action-slack-notify:v2.2.0
\ No newline at end of file
diff --git a/scripts/build-and-push.sh b/scripts/build-and-push.sh
new file mode 100755
index 0000000..5e86d80
--- /dev/null
+++ b/scripts/build-and-push.sh
@@ -0,0 +1,60 @@
+#!/bin/bash
+
+IMAGE=$1
+BUILDX_INSTANCE="action-slack-notify-builder"
+PLATFORMS="linux/arm64,linux/amd64"
+
+function print_usage {
+  echo "$0 <Image>"
+}
+
+function validate_command_line_args {
+  if [[ -z "$IMAGE" ]]; then 
+    echo "Image is not supplied"
+    print_usage
+    exit 1
+  fi
+
+  echo "=================================="
+  echo "Arguments"
+  echo "Image: $IMAGE"
+  echo "=================================="
+}
+
+function validate_docker_installed {
+  if ! [[ $(command -v docker) ]]; then 
+    echo "Docker is not installed. Install docker first"
+    exit 1
+  fi
+}
+
+function setup_buildx_platforms {
+  echo "Install buildx platforms"
+  docker run --privileged --rm tonistiigi/binfmt --install all > /dev/null
+}
+
+function setup_buildx_instance {
+  echo "Check buildx instance '$BUILDX_INSTANCE' is set"
+
+  if [[ $(docker buildx ls | grep $BUILDX_INSTANCE | grep docker-container | wc -l ) -eq 0 ]]; then
+    echo "Create buildx instance '$BUILDX_INSTANCE'"
+    docker buildx create --name=$BUILDX_INSTANCE --driver=docker-container
+    echo "Successfully created and converted to buildx instance '$BUILDX_INSTANCE'"
+  else
+    echo "Buildx instance $BUILDX_INSTANCE is ready. Skip..."
+  fi
+}
+
+function build_and_push_images {
+  echo "Build and push docker images"
+  docker buildx build --builder=$BUILDX_INSTANCE --platform=$PLATFORMS --push -t $IMAGE .
+}
+
+
+validate_command_line_args
+validate_docker_installed
+
+setup_buildx_platforms
+setup_buildx_instance
+
+build_and_push_images
\ No newline at end of file