Skip to content

Commit ab89c89

Browse files
authored
Add Helm Chart (#7)
* Add Helm Chart * Update chart
1 parent 80121e0 commit ab89c89

File tree

12 files changed

+388
-2
lines changed

12 files changed

+388
-2
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
name: Build and Release Auth Chart
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
workflow_dispatch:
8+
9+
env:
10+
IMAGE_NAME: auth
11+
REGISTRY: ghcr.io
12+
OCI_REPO: ghcr.io/abstractize
13+
PROJECT_NAME: todo
14+
15+
jobs:
16+
docker-build:
17+
runs-on: ubuntu-latest
18+
permissions:
19+
packages: write
20+
21+
steps:
22+
- name: Checkout code
23+
uses: actions/checkout@v4
24+
25+
- name: Set lowercase env vars
26+
id: set_env
27+
run: |
28+
USERNAME=$(echo '${{ github.repository_owner }}' | tr '[:upper:]' '[:lower:]')
29+
REPO_NAME=$(echo "${{ github.repository }}" | cut -d/ -f2 | tr '[:upper:]' '[:lower:]')
30+
echo "USERNAME=$USERNAME" >> $GITHUB_ENV
31+
echo "REPO_NAME=$REPO_NAME" >> $GITHUB_ENV
32+
echo "USERNAME=$USERNAME" >> $GITHUB_OUTPUT
33+
echo "REPO_NAME=$REPO_NAME" >> $GITHUB_OUTPUT
34+
35+
- name: Set up Docker Buildx
36+
uses: docker/setup-buildx-action@v3
37+
38+
- name: Log in to GitHub Container Registry
39+
uses: docker/login-action@v3
40+
with:
41+
registry: ${{ env.REGISTRY }}
42+
username: ${{ github.actor }}
43+
password: ${{ secrets.GITHUB_TOKEN }}
44+
45+
- name: Build and push Docker image
46+
uses: docker/build-push-action@v6
47+
with:
48+
file: ./Dockerfile
49+
push: true
50+
build-args: |
51+
GITHUB_USERNAME=${{ github.repository_owner }}
52+
secrets: |
53+
GITHUB_TOKEN=${{ secrets.GITHUB_TOKEN }}
54+
tags: |
55+
${{ env.OCI_REPO }}/docker-images/${{ env.PROJECT_NAME }}/${{ env.IMAGE_NAME }}:latest
56+
${{ env.OCI_REPO }}/docker-images/${{ env.PROJECT_NAME }}/${{ env.IMAGE_NAME }}:${{ github.sha }}
57+
58+
helm-release:
59+
runs-on: ubuntu-latest
60+
needs: docker-build
61+
permissions:
62+
contents: read
63+
packages: write
64+
65+
steps:
66+
- name: Checkout code
67+
uses: actions/checkout@v4
68+
69+
- name: Set up Helm
70+
uses: azure/setup-helm@v4
71+
with:
72+
version: v3.14.0
73+
74+
- name: Update Chart.yaml version and appVersion
75+
run: |
76+
sudo apt-get update && sudo apt-get install -y yq
77+
VERSION="0.1.${GITHUB_RUN_NUMBER}"
78+
yq -y -i ".appVersion = \"${GITHUB_SHA}\" | .version = \"$VERSION\"" .helm/Chart.yaml
79+
80+
- name: Lint Helm chart
81+
run: helm lint .helm
82+
83+
- name: Package Helm chart
84+
run: helm package .helm --destination .helm-dist
85+
86+
- name: Push Helm chart to GHCR (OCI)
87+
run: |
88+
echo "${{ secrets.GITHUB_TOKEN }}" | helm registry login ghcr.io --username ${{ github.actor }} --password-stdin
89+
CHART_FILE=$(ls .helm-dist/*.tgz)
90+
helm push $CHART_FILE oci://${{ env.OCI_REPO }}/helm-charts/${{ env.PROJECT_NAME }}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
name: PR Validation
2+
3+
on:
4+
pull_request:
5+
branches:
6+
- main
7+
8+
jobs:
9+
helm-lint:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- name: Checkout code
13+
uses: actions/checkout@v4
14+
15+
- name: Set up Helm
16+
uses: azure/setup-helm@v4
17+
with:
18+
version: v3.14.0
19+
20+
- name: Helm lint
21+
run: helm lint .helm
22+
23+
docker-build-test:
24+
permissions:
25+
contents: read
26+
packages: write
27+
28+
runs-on: ubuntu-latest
29+
needs: helm-lint
30+
steps:
31+
- name: Checkout code
32+
uses: actions/checkout@v4
33+
34+
- name: Set up Docker Buildx
35+
uses: docker/setup-buildx-action@v3
36+
37+
- name: Build Docker image with secret
38+
uses: docker/build-push-action@v6
39+
with:
40+
context: .
41+
file: Dockerfile
42+
push: false
43+
load: true
44+
build-args: |
45+
GITHUB_USERNAME=${{ github.repository_owner }}
46+
secrets: |
47+
GITHUB_TOKEN=${{ secrets.GITHUB_TOKEN }}
48+
tags: test-image:pr-${{ github.event.pull_request.number }}
49+
50+
- name: Run container test
51+
run: docker run --rm --entrypoint sleep test-image:pr-${{ github.event.pull_request.number }} 5

.helm/.helmignore

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Patterns to ignore when building packages.
2+
# This supports shell glob matching, relative path matching, and
3+
# negation (prefixed with !). Only one pattern per line.
4+
.DS_Store
5+
# Common VCS dirs
6+
.git/
7+
.gitignore
8+
.bzr/
9+
.bzrignore
10+
.hg/
11+
.hgignore
12+
.svn/
13+
# Common backup files
14+
*.swp
15+
*.bak
16+
*.tmp
17+
*.orig
18+
*~
19+
# Various IDEs
20+
.project
21+
.idea/
22+
*.tmproj
23+
.vscode/

.helm/Chart.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
apiVersion: v2
2+
name: auth-service
3+
description: Helm chart for the Auth Service of the TODO app
4+
type: application
5+
version: 0.1.0
6+
appVersion: "latest"
7+
icon: https://raw.githubusercontent.com/Abstractize/todo.auth/main/.helm/icons/auth.svg

.helm/icons/auth.svg

Lines changed: 7 additions & 0 deletions
Loading

.helm/templates/_helpers.tpl

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
{{/*
2+
Expand the name of the chart.
3+
*/}}
4+
{{- define "auth-service.name" -}}
5+
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }}
6+
{{- end }}
7+
8+
{{/*
9+
Create a default fully qualified app name.
10+
We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
11+
If release name contains chart name it will be used as a full name.
12+
*/}}
13+
{{- define "auth-service.fullname" -}}
14+
{{- if .Values.fullnameOverride }}
15+
{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }}
16+
{{- else }}
17+
{{- $name := default .Chart.Name .Values.nameOverride }}
18+
{{- if contains $name .Release.Name }}
19+
{{- .Release.Name | trunc 63 | trimSuffix "-" }}
20+
{{- else }}
21+
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" }}
22+
{{- end }}
23+
{{- end }}
24+
{{- end }}
25+
26+
{{/*
27+
Create chart name and version as used by the chart label.
28+
*/}}
29+
{{- define "auth-service.chart" -}}
30+
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }}
31+
{{- end }}
32+
33+
{{/*
34+
Common labels
35+
*/}}
36+
{{- define "auth-service.labels" -}}
37+
helm.sh/chart: {{ include "auth-service.chart" . }}
38+
{{ include "auth-service.selectorLabels" . }}
39+
{{- if .Chart.AppVersion }}
40+
app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
41+
{{- end }}
42+
app.kubernetes.io/managed-by: {{ .Release.Service }}
43+
{{- end }}
44+
45+
{{/*
46+
Selector labels
47+
*/}}
48+
{{- define "auth-service.selectorLabels" -}}
49+
app.kubernetes.io/name: {{ include "auth-service.name" . }}
50+
app.kubernetes.io/instance: {{ .Release.Name }}
51+
{{- end }}
52+
53+
{{/*
54+
Create the name of the service account to use
55+
*/}}
56+
{{- define "auth-service.serviceAccountName" -}}
57+
{{- if .Values.serviceAccount.create }}
58+
{{- default (include "auth-service.fullname" .) .Values.serviceAccount.name }}
59+
{{- else }}
60+
{{- default "default" .Values.serviceAccount.name }}
61+
{{- end }}
62+
{{- end }}

.helm/templates/deployment.yaml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
apiVersion: apps/v1
2+
kind: Deployment
3+
metadata:
4+
name: {{ include "auth-service.fullname" . }}
5+
labels:
6+
app: {{ include "auth-service.name" . }}
7+
chart: {{ include "auth-service.chart" . }}
8+
spec:
9+
replicas: {{ .Values.replicaCount }}
10+
selector:
11+
matchLabels:
12+
app: {{ include "auth-service.name" . }}
13+
template:
14+
metadata:
15+
labels:
16+
app: {{ include "auth-service.name" . }}
17+
spec:
18+
containers:
19+
- name: {{ include "auth-service.name" . }}
20+
image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
21+
imagePullPolicy: {{ .Values.image.pullPolicy }}
22+
ports:
23+
- containerPort: {{ .Values.service.port }}
24+
envFrom:
25+
- configMapRef:
26+
name: {{ .Values.configMapName }}
27+
- secretRef:
28+
name: {{ .Values.secretName }}
29+
resources:
30+
{{- toYaml .Values.resources | nindent 12 }}

.helm/templates/hpa.yaml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{{- if .Values.hpa.enabled }}
2+
apiVersion: autoscaling/v2
3+
kind: HorizontalPodAutoscaler
4+
metadata:
5+
name: {{ include "auth-service.fullname" . }}
6+
spec:
7+
scaleTargetRef:
8+
apiVersion: apps/v1
9+
kind: Deployment
10+
name: {{ include "auth-service.fullname" . }}
11+
minReplicas: {{ .Values.hpa.minReplicas }}
12+
maxReplicas: {{ .Values.hpa.maxReplicas }}
13+
metrics:
14+
- type: Resource
15+
resource:
16+
name: cpu
17+
target:
18+
type: Utilization
19+
averageUtilization: {{ .Values.hpa.targetCPUUtilizationPercentage }}
20+
{{- end }}

.helm/templates/service.yaml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
apiVersion: v1
2+
kind: Service
3+
metadata:
4+
name: {{ include "auth-service.fullname" . }}
5+
labels:
6+
app: {{ include "auth-service.name" . }}
7+
spec:
8+
type: {{ .Values.service.type }}
9+
ports:
10+
- port: {{ .Values.service.port }}
11+
targetPort: {{ .Values.service.port }}
12+
protocol: TCP
13+
name: http
14+
selector:
15+
app: {{ include "auth-service.name" . }}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
apiVersion: v1
2+
kind: Pod
3+
metadata:
4+
name: "{{ include "auth-service.fullname" . }}-test-connection"
5+
labels:
6+
app: {{ include "auth-service.name" . }}
7+
annotations:
8+
"helm.sh/hook": test
9+
"helm.sh/hook-delete-policy": before-hook-creation,hook-succeeded
10+
spec:
11+
containers:
12+
- name: wget
13+
image: busybox:1.36
14+
command: ['wget']
15+
args: ['-qO-', 'http://{{ include "auth-service.fullname" . }}:{{ .Values.service.port }}']
16+
restartPolicy: Never

0 commit comments

Comments
 (0)