diff --git a/.github/workflows/cicd-deploy-to-poc.yml b/.github/workflows/cicd-deploy-to-poc.yml new file mode 100644 index 0000000..251088b --- /dev/null +++ b/.github/workflows/cicd-deploy-to-poc.yml @@ -0,0 +1,76 @@ +name: "♻️ Deploy to PoC Namespace" + +on: + workflow_dispatch: + pull_request: + branches: [main] + push: + branches: [main] +env: + KUBE_CLUSTER: ${{ secrets.KUBE_CLUSTER }} + KUBE_NAMESPACE: ${{ secrets.POC_KUBE_NAMESPACE }} + KUBE_CERT: ${{ secrets.POC_KUBE_CERT }} + KUBE_TOKEN: ${{ secrets.POC_KUBE_TOKEN }} + + IMAGE_TAG: ${{ github.sha }} + ECR_REGISTRY: ${{ vars.DEVELOPMENT_ECR_REGISTRY }} + ECR_REPOSITORY: ${{ vars.DEVELOPMENT_ECR_REPOSITORY }} + + POSTGRES_USER: ${{secrets.POC_POSTGRES_USER}} + POSTGRES_PASSWORD: ${{secrets.POC_POSTGRES_PASSWORD}} + POSTGRES_DB: ${{secrets.POC_POSTGRES_DB}} + POSTGRES_HOST: ${{secrets.POC_POSTGRES_HOST}} + POSTGRES_PORT: ${{secrets.POC_POSTGRES_PORT}} + +jobs: + build-push: + runs-on: ubuntu-latest + permissions: + id-token: write + contents: read + steps: + - uses: actions/checkout@v4 + + - uses: aws-actions/configure-aws-credentials@v4 + with: + role-to-assume: ${{ secrets.DEVELOPMENT_ECR_ROLE_TO_ASSUME }} + aws-region: ${{ vars.DEVELOPMENT_ECR_REGION }} + + - uses: aws-actions/amazon-ecr-login@v2 + id: login-ecr + - run: | + docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG . + docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG + + deploy-to-dev: + needs: build-push + runs-on: ubuntu-latest + container: alpine/k8s:1.23.17 + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Authenticate to the cluster + run: | + echo "${KUBE_CERT}" > ca.crt + kubectl config set-cluster ${KUBE_CLUSTER} --certificate-authority=./ca.crt --server=https://${KUBE_CLUSTER} + kubectl config set-credentials deploy-user --token=${KUBE_TOKEN} + kubectl config set-context ${KUBE_CLUSTER} --cluster=${KUBE_CLUSTER} --user=deploy-user --namespace=${KUBE_NAMESPACE} + kubectl config get-contexts + kubectl config use-context ${KUBE_CLUSTER} + + - name: Deploy helm chart to PoC + run: | + helm upgrade kpi-dashboard \ + helm/kpi-dashboard \ + --install \ + --force \ + --wait \ + --timeout 10m \ + --namespace ${KUBE_NAMESPACE} \ + --values=helm/kpi-dashboard/values-poc.yaml \ + --set app.deployment.env.POSTGRES_USER=${POSTGRES_USER} \ + --set app.deployment.env.POSTGRES_PASSWORD=${POSTGRES_PASSWORD} \ + --set app.deployment.env.POSTGRES_DB=${POSTGRES_DB} \ + --set app.deployment.env.POSTGRES_HOST=${POSTGRES_HOST} \ + --set app.deployment.env.POSTGRES_PORT=${POSTGRES_PORT} \ diff --git a/helm/kpi-dashboard/.helmignore b/helm/kpi-dashboard/.helmignore new file mode 100644 index 0000000..0e8a0eb --- /dev/null +++ b/helm/kpi-dashboard/.helmignore @@ -0,0 +1,23 @@ +# Patterns to ignore when building packages. +# This supports shell glob matching, relative path matching, and +# negation (prefixed with !). Only one pattern per line. +.DS_Store +# Common VCS dirs +.git/ +.gitignore +.bzr/ +.bzrignore +.hg/ +.hgignore +.svn/ +# Common backup files +*.swp +*.bak +*.tmp +*.orig +*~ +# Various IDEs +.project +.idea/ +*.tmproj +.vscode/ diff --git a/helm/kpi-dashboard/Chart.yaml b/helm/kpi-dashboard/Chart.yaml new file mode 100644 index 0000000..118fe91 --- /dev/null +++ b/helm/kpi-dashboard/Chart.yaml @@ -0,0 +1,24 @@ +apiVersion: v2 +name: kpi-dashboard +description: A Helm chart for Kubernetes + +# A chart can be either an 'application' or a 'library' chart. +# +# Application charts are a collection of templates that can be packaged into versioned archives +# to be deployed. +# +# Library charts provide useful utilities or functions for the chart developer. They're included as +# a dependency of application charts to inject those utilities and functions into the rendering +# pipeline. Library charts do not define any templates and therefore cannot be deployed. +type: application + +# This is the chart version. This version number should be incremented each time you make changes +# to the chart and its templates, including the app version. +# Versions are expected to follow Semantic Versioning (https://semver.org/) +version: 0.1.0 + +# This is the version number of the application being deployed. This version number should be +# incremented each time you make changes to the application. Versions are not expected to +# follow Semantic Versioning. They should reflect the version the application is using. +# It is recommended to use it with quotes. +appVersion: "1.16.0" diff --git a/helm/kpi-dashboard/templates/_helpers.tpl b/helm/kpi-dashboard/templates/_helpers.tpl new file mode 100644 index 0000000..e303543 --- /dev/null +++ b/helm/kpi-dashboard/templates/_helpers.tpl @@ -0,0 +1,51 @@ +{{/* +Expand the name of the chart. +*/}} +{{- define "kpi-dashboard.name" -}} +{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }} +{{- end }} + +{{/* +Create a default fully qualified app name. +We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec). +If release name contains chart name it will be used as a full name. +*/}} +{{- define "kpi-dashboard.fullname" -}} +{{- if .Values.fullnameOverride }} +{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }} +{{- else }} +{{- $name := default .Chart.Name .Values.nameOverride }} +{{- if contains $name .Release.Name }} +{{- .Release.Name | trunc 63 | trimSuffix "-" }} +{{- else }} +{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" }} +{{- end }} +{{- end }} +{{- end }} + +{{/* +Create chart name and version as used by the chart label. +*/}} +{{- define "kpi-dashboard.chart" -}} +{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }} +{{- end }} + +{{/* +Common labels +*/}} +{{- define "kpi-dashboard.labels" -}} +helm.sh/chart: {{ include "kpi-dashboard.chart" . }} +{{ include "kpi-dashboard.selectorLabels" . }} +{{- if .Chart.AppVersion }} +app.kubernetes.io/version: {{ .Chart.AppVersion | quote }} +{{- end }} +app.kubernetes.io/managed-by: {{ .Release.Service }} +{{- end }} + +{{/* +Selector labels +*/}} +{{- define "kpi-dashboard.selectorLabels" -}} +app.kubernetes.io/name: {{ include "kpi-dashboard.name" . }} +app.kubernetes.io/instance: {{ .Release.Name }} +{{- end }} diff --git a/helm/kpi-dashboard/templates/deployment.yaml b/helm/kpi-dashboard/templates/deployment.yaml new file mode 100644 index 0000000..538f592 --- /dev/null +++ b/helm/kpi-dashboard/templates/deployment.yaml @@ -0,0 +1,35 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: { { include "kpi-dashboard.fullname" . } } + labels: { { - include "kpi-dashboard.labels" . | nindent 4 } } +spec: + replicas: { { .Values.app.deployment.replicaCount } } + selector: + matchLabels: { { - include "kpi-dashboard.selectorLabels" . | nindent 6 } } + template: + metadata: + labels: { { - include "kpi-dashboard.selectorLabels" . | nindent 8 } } + spec: + # serviceAccountName created by the Cloud Platform environment + serviceAccountName: cd-serviceaccount + containers: + - name: { { .Chart.Name } } + image: "{{ .Values.app.deployment.image.repository }}:{{ .Values.app.deployment.image.tag | default .Chart.AppVersion }}" + imagePullPolicy: IfNotPresent + env: + - name: POSTGRES_USER + value: { { .Values.app.deployment.env.POSTGRES_USER | quote } } + - name: POSTGRES_PASSWORD + value: + { { .Values.app.deployment.env.POSTGRES_PASSWORD | quote } } + - name: POSTGRES_DB + value: { { .Values.app.deployment.env.POSTGRES_DB | quote } } + - name: POSTGRES_HOST + value: { { .Values.app.deployment.env.POSTGRES_HOST | quote } } + - name: POSTGRES_PORT + value: { { .Values.app.deployment.env.POSTGRES_PORT | quote } } + + ports: + - name: http + containerPort: 80 diff --git a/helm/kpi-dashboard/templates/ingress.yaml b/helm/kpi-dashboard/templates/ingress.yaml new file mode 100644 index 0000000..d58b444 --- /dev/null +++ b/helm/kpi-dashboard/templates/ingress.yaml @@ -0,0 +1,29 @@ +{{- $fullName := include "kpi-dashboard.fullname" . -}} +--- +apiVersion: networking.k8s.io/v1 +kind: Ingress +metadata: + name: {{ $fullName }} + labels: + {{- include "kpi-dashboard.labels" . | nindent 4 }} + annotations: + external-dns.alpha.kubernetes.io/set-identifier: {{ $fullName }}-{{ $fullName }}-green + external-dns.alpha.kubernetes.io/aws-weight: "100" + cloud-platform.justice.gov.uk/ignore-external-dns-weight: "true" +spec: + ingressClassName: "default" + tls: + - hosts: + - {{ .Values.app.ingress.host }} + rules: + - host: {{ .Values.app.ingress.host }} + http: + paths: + - path: / + pathType: ImplementationSpecific + backend: + service: + name: {{ $fullName }} + port: + number: 80 + diff --git a/helm/kpi-dashboard/templates/service.yaml b/helm/kpi-dashboard/templates/service.yaml new file mode 100644 index 0000000..bc4d023 --- /dev/null +++ b/helm/kpi-dashboard/templates/service.yaml @@ -0,0 +1,11 @@ +apiVersion: v1 +kind: Service +metadata: + name: { { include "kpi-dashboard.fullname" . } } + labels: { { - include "kpi-dashboard.labels" . | nindent 4 } } +spec: + ports: + - port: 80 + targetPort: 4567 + name: https + selector: { { - include "kpi-dashboard.selectorLabels" . | nindent 4 } } diff --git a/helm/kpi-dashboard/values-poc.yaml b/helm/kpi-dashboard/values-poc.yaml new file mode 100644 index 0000000..56ee4f3 --- /dev/null +++ b/helm/kpi-dashboard/values-poc.yaml @@ -0,0 +1,8 @@ +app: + ingress: + host: "kpi-dashboard-poc.cloud-platform.service.justice.gov.uk" + + deployment: + replicaCount: 1 + env: + LOGGING_LEVEL: INFO