From 8ff3ef35944f98a1740482c6320c8be943771ada Mon Sep 17 00:00:00 2001
From: Thomas Schneider <thomas@fsmpi.rwth-aachen.de>
Date: Tue, 28 Jun 2022 16:36:58 +0200
Subject: [PATCH] Add Containerfile and CI config to build and upload container

---
 .containerignore | 28 ++++++++++++++++++++++++++++
 .dockerignore    |  1 +
 .gitlab-ci.yml   | 16 ++++++++++++++++
 Containerfile    | 21 +++++++++++++++++++++
 4 files changed, 66 insertions(+)
 create mode 100644 .containerignore
 create mode 120000 .dockerignore
 create mode 100644 Containerfile

diff --git a/.containerignore b/.containerignore
new file mode 100644
index 0000000..8aad965
--- /dev/null
+++ b/.containerignore
@@ -0,0 +1,28 @@
+cert-manager-webhook-rwth
+*.config
+
+# If you prefer the allow list template instead of the deny list, see community template:
+# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
+#
+# Binaries for programs and plugins
+*.exe
+*.exe~
+*.dll
+*.so
+*.dylib
+
+# Test binary, built with `go test -c`
+*.test
+
+# Output of the go coverage tool, specifically when used with LiteIDE
+*.out
+
+# Dependency directories (remove the comment below to include it)
+# vendor/
+
+# Go workspace file
+go.work
+
+.git
+.gitlab-ci.yml
+.gitignore
diff --git a/.dockerignore b/.dockerignore
new file mode 120000
index 0000000..092a75d
--- /dev/null
+++ b/.dockerignore
@@ -0,0 +1 @@
+.containerignore
\ No newline at end of file
diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index c10ee44..093c6f7 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -28,3 +28,19 @@ lint:
     reports:
       junit: golangci-output.xml
     expire_in: 7 days
+
+build-image:
+  image:
+    name: gcr.io/kaniko-project/executor:debug
+    entrypoint: [""]
+  stage: deploy
+  before_script:
+    - echo "{\"auths\":{\"$CI_REGISTRY\":{\"username\":\"$CI_REGISTRY_USER\",\"password\":\"$CI_REGISTRY_PASSWORD\"}}}" > /kaniko/.docker/config.json
+    - /kaniko/executor version
+  script:
+    - >-
+      /kaniko/executor
+      --context $CI_PROJECT_DIR
+      --dockerfile Containerfile
+      --destination $CI_REGISTRY_IMAGE:$CI_COMMIT_SHORT_SHA
+      --destination $CI_REGISTRY_IMAGE:latest
diff --git a/Containerfile b/Containerfile
new file mode 100644
index 0000000..d822377
--- /dev/null
+++ b/Containerfile
@@ -0,0 +1,21 @@
+FROM docker.io/library/golang:1-alpine AS build
+
+ARG GOARCH="amd64"
+ARG GOARM=""
+
+WORKDIR /workspace
+
+ENV GOPATH="/workspace/.go"
+
+COPY . .
+
+RUN go mod download
+
+RUN CGO_ENABLED=0 GOARCH=$GOARCH GOARM=$GOARM go build -v -o webhook -ldflags '-w -s -extldflags "-static"' .
+
+FROM scratch
+
+COPY --from=build /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt
+COPY --from=build /workspace/webhook /webhook
+
+ENTRYPOINT ["/webhook"]
-- 
GitLab