From cae977d89267b4394efd65257d765c6725839c90 Mon Sep 17 00:00:00 2001
From: Konrad Botor <kbotor@syncad.com>
Date: Tue, 2 Apr 2024 10:17:45 +0200
Subject: [PATCH] Added psql image

---
 .gitlab-ci.yml  | 45 +++++++++++++++++++++++++++++++++++++++++++++
 Dockerfile.psql | 15 +++++++++++++++
 docker-bake.hcl | 13 +++++++++++++
 3 files changed, 73 insertions(+)
 create mode 100644 Dockerfile.psql

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 20d46c7..85f3653 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -9,6 +9,7 @@ stages:
   - validation
   - pre-build
   - build
+  - test
   - example-build
   - example-test
   - example-cleanup
@@ -187,6 +188,50 @@ build_emsdk_image:
           - scripts/bash/emscripten/**/*
         compare_to: 'refs/heads/develop'
 
+build_psql_image:
+  extends: .build_docker_image
+  stage: build
+  variables:
+    BUILD_TARGET: "psql"
+  needs:
+    - build_docker_dind_image
+  rules:
+    - if: $CI_COMMIT_BRANCH == "main" # for branch main run if there were changes on the branch
+      exists:
+        - Dockerfile.psql
+      changes:
+        paths:
+          - Dockerfile.psql
+    - if: $CI_COMMIT_BRANCH !~ /^main$/ # for other branches compare to develop and run if there are diffrences
+      exists:
+        - Dockerfile.psql
+      changes:
+        paths:
+          - Dockerfile.psql
+        compare_to: 'refs/heads/develop'
+
+psql_image_test:
+  stage: test
+  image: registry.gitlab.syncad.com/hive/common-ci-configuration/psql:14-1
+  services:
+    - postgres:14
+  variables:
+    POSTGRES_DB: haf
+    POSTGRES_USER: haf_admin
+    POSTGRES_PASSWORD: password
+    POSTGRES_HOST_AUTH_METHOD: trust
+    PGHOSTADDR: postgres
+    PGPORT: 5432
+    PGDATABASE: $POSTGRES_DB
+    PGPASSWORD: $POSTGRES_PASSWORD
+  script:
+    - |
+      set -e
+
+      psql --list
+      psql --command="\set"
+  tags:
+    - public-runner-docker    
 
 example_docker_image_builder_job:
   extends: .docker_image_builder_job_template
diff --git a/Dockerfile.psql b/Dockerfile.psql
new file mode 100644
index 0000000..184d935
--- /dev/null
+++ b/Dockerfile.psql
@@ -0,0 +1,15 @@
+# syntax=docker/dockerfile:1.5
+ARG PAAS_PSQL_VERSION=11251948d5dd4867552f9b9836a9e02110304df5
+FROM ghcr.io/alphagov/paas/psql:${PAAS_PSQL_VERSION} AS psql
+
+RUN <<EOF
+    set -e
+    apk add --no-cache sudo git bash
+    adduser -s /bin/bash -G users -D "haf_admin"
+    echo "haf_admin ALL=(ALL:ALL) NOPASSWD:ALL" >> /etc/sudoers
+EOF
+
+USER haf_admin
+WORKDIR /home/haf_admin
+
+ENTRYPOINT [ "psql" ]
\ No newline at end of file
diff --git a/docker-bake.hcl b/docker-bake.hcl
index e99b3e6..4304ca8 100644
--- a/docker-bake.hcl
+++ b/docker-bake.hcl
@@ -3,6 +3,12 @@ variable "CI_COMMIT_SHA" {}
 variable "EMSCRIPTEN_VERSION" {
   default = "3.1.56"
 }
+variable "PSQL_IMAGE_VERSION" {
+  default = "14-1" # After updating tag here, remeber to also update it in job 'psql_image_test'
+}
+variable "PAAS_PSQL_VERSION" {
+  default = "11251948d5dd4867552f9b9836a9e02110304df5"
+}
 variable "BOOST_VERSION_TAG" {
   default = null
 }
@@ -85,4 +91,11 @@ target "emsdk" {
     BOOST_VERSION_TAG = "${BOOST_VERSION_TAG}",
     OPENSSL_VERSION_TAG = "${OPENSSL_VERSION_TAG}"
   }
+}
+
+target "psql" {
+  dockerfile = "Dockerfile.psql"
+  tags = generate-tags("psql", "${PSQL_IMAGE_VERSION}")
+  cache-from = generate-cache-from("psql", "${PSQL_IMAGE_VERSION}")
+  cache-to = generate-cache-to("psql", "${PSQL_IMAGE_VERSION}")
 }
\ No newline at end of file
-- 
GitLab