Skip to content

Proper gitops workflow #1635

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 28 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 108 additions & 0 deletions .github/workflows/deploy-to-aks.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
name: Build Docker Image, Update GitOps, and Push to ACR

on:
push:
branches:
- main
- helm-release

jobs:
build:
runs-on: ubuntu-latest

steps:
# Checkout the code from the current repository
- name: Checkout code
uses: actions/checkout@v2

# Extract and increment chart version
- name: Update Chart version
id: version
run: |
# Extract current version
CURRENT_VERSION=$(grep 'version:' ./k8s/charts/json-server/Chart.yaml | sed 's/version: //')

# Increment patch version (1.0.0 -> 1.0.1)
NEW_VERSION=$(echo $CURRENT_VERSION | awk -F. '{$NF = $NF + 1;} 1' OFS=.)

# Update Chart.yaml
sed -i "s/version: $CURRENT_VERSION/version: $NEW_VERSION/g" ./k8s/charts/json-server/Chart.yaml

echo "Updated chart version from $CURRENT_VERSION to $NEW_VERSION"
echo "NEW_VERSION=$NEW_VERSION" >> $GITHUB_ENV
echo "CURRENT_VERSION=$CURRENT_VERSION" >> $GITHUB_ENV

# Login to Azure using the service principal credentials
- name: Azure Login
uses: azure/login@v1
with:
creds: ${{ secrets.AZURE_CREDENTIALS }}

# Build Docker image with semantic version
- name: Build Docker Image
run: |
docker build -t json-server:$NEW_VERSION .

# Tag image with version
- name: Tag Docker image
run: |
docker tag json-server:$NEW_VERSION ${{ secrets.AZURE_CONTAINER_REGISTRY }}.azurecr.io/json-server:$NEW_VERSION

# Log in to Azure Container Registry (ACR)
- name: Azure Container Registry login
run: |
az acr login --name ${{ secrets.AZURE_CONTAINER_REGISTRY }}

# Push Docker image to ACR
- name: Push Docker image to ACR
run: |
docker push ${{ secrets.AZURE_CONTAINER_REGISTRY }}.azurecr.io/json-server:$NEW_VERSION

# Package helm chart
- name: Package Helm chart
run: |
helm package ./k8s/charts/json-server -d ./helm-chart

# Push Helm chart to helm repository
- name: Push Helm chart to repository
run: |
helm push ./helm-chart/json-server-$NEW_VERSION.tgz oci://${{ secrets.AZURE_CONTAINER_REGISTRY }}.azurecr.io/helm/

# Clone the GitOps repository
- name: Clone GitOps repository
uses: actions/checkout@v4
with:
repository: fabremartin/gitops
token: ${{ secrets.GITOPS_PAT }}
path: gitops-repo

# Update values.yaml in the GitOps repo with new image tag
- name: Update GitOps & HelmRelease with new image tag and chart version
run: |
sed -i "s|tag: .*|tag: $NEW_VERSION|g" gitops-repo/services/json-server/HelmRelease.yaml
sed -i "s|version: .*|version: $NEW_VERSION|g" gitops-repo/services/json-server/HelmRelease.yaml

# Check the content after updating
- name: Check content after update
run: |
echo "Updated HelmRelease.yaml:"
cat gitops-repo/services/json-server/HelmRelease.yaml

# Commit and push updated GitOps manifest
- name: Commit and push updated GitOps manifest
run: |
cd gitops-repo
git config --global user.name "fabremartin"
git config --global user.email "[email protected]"
git add services/json-server/HelmRelease.yaml
git commit -m "Update json-server to version $NEW_VERSION"
git push origin main

# Commit the updated Chart.yaml back to the source repo
# - name: Commit updated Chart.yaml
# run: |
# git config --global user.name "fabremartin"
# git config --global user.email "[email protected]"
# git add ./k8s/charts/json-server/Chart.yaml
# git commit -m "Bump chart version to $NEW_VERSION [skip ci]"
# git push origin main
31 changes: 0 additions & 31 deletions .github/workflows/node.js.yml

This file was deleted.

38 changes: 0 additions & 38 deletions .github/workflows/npm-publish.yml

This file was deleted.

20 changes: 20 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Use official Node.js image as a base image
FROM node:16

# Set the working directory in the container
WORKDIR /usr/src/app

# Copy the package.json and package-lock.json
COPY package*.json ./

# Install dependencies (if needed, for any other dependencies)
RUN npm install

# Copy the rest of the application files
COPY . .

# Expose port 3000 to the outside world
EXPOSE 3000

# Command to run the JSON server with npx
CMD ["npx", "json-server", "db.json", "--host", "0.0.0.0", "--port", "3000"]
15 changes: 15 additions & 0 deletions db.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"posts": [
{ "id": "1", "title": "a title", "views": 100 },
{ "id": "2", "title": "another title", "views": 200 },
{ "id": "4", "title": "fresh install here we go", "views": 10 }
],
"comments": [
{ "id": "1", "text": "a comment about post 1", "postId": "1" },
{ "id": "2", "text": "another comment about post 1", "postId": "1" },
{ "id": "3", "text": "hello everyone", "postId": "1" }
],
"profile": {
"name": "typicode"
}
}
23 changes: 23 additions & 0 deletions k8s/charts/json-server/.helmignore
Original file line number Diff line number Diff line change
@@ -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/
24 changes: 24 additions & 0 deletions k8s/charts/json-server/Chart.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
apiVersion: v2
name: json-server
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.1

# 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"
43 changes: 43 additions & 0 deletions k8s/charts/json-server/templates/.ingress.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{{- if .Values.ingress.enabled -}}
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: {{ include "json-server.fullname" . }}
labels:
{{- include "json-server.labels" . | nindent 4 }}
{{- with .Values.ingress.annotations }}
annotations:
{{- toYaml . | nindent 4 }}
{{- end }}
spec:
{{- with .Values.ingress.className }}
ingressClassName: {{ . }}
{{- end }}
{{- if .Values.ingress.tls }}
tls:
{{- range .Values.ingress.tls }}
- hosts:
{{- range .hosts }}
- {{ . | quote }}
{{- end }}
secretName: {{ .secretName }}
{{- end }}
{{- end }}
rules:
{{- range .Values.ingress.hosts }}
- host: {{ .host | quote }}
http:
paths:
{{- range .paths }}
- path: {{ .path }}
{{- with .pathType }}
pathType: {{ . }}
{{- end }}
backend:
service:
name: {{ include "json-server.fullname" $ }}
port:
number: {{ $.Values.service.port }}
{{- end }}
{{- end }}
{{- end }}
13 changes: 13 additions & 0 deletions k8s/charts/json-server/templates/.serviceaccount.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{{- if .Values.serviceAccount.create -}}
apiVersion: v1
kind: ServiceAccount
metadata:
name: {{ include "json-server.serviceAccountName" . }}
labels:
{{- include "json-server.labels" . | nindent 4 }}
{{- with .Values.serviceAccount.annotations }}
annotations:
{{- toYaml . | nindent 4 }}
{{- end }}
automountServiceAccountToken: {{ .Values.serviceAccount.automount }}
{{- end }}
62 changes: 62 additions & 0 deletions k8s/charts/json-server/templates/_helpers.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{{/*
Expand the name of the chart.
*/}}
{{- define "json-server.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 "json-server.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 "json-server.chart" -}}
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }}
{{- end }}

{{/*
Common labels
*/}}
{{- define "json-server.labels" -}}
helm.sh/chart: {{ include "json-server.chart" . }}
{{ include "json-server.selectorLabels" . }}
{{- if .Chart.AppVersion }}
app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
{{- end }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
{{- end }}

{{/*
Selector labels
*/}}
{{- define "json-server.selectorLabels" -}}
app.kubernetes.io/name: {{ include "json-server.name" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
{{- end }}

{{/*
Create the name of the service account to use
*/}}
{{- define "json-server.serviceAccountName" -}}
{{- if .Values.serviceAccount.create }}
{{- default (include "json-server.fullname" .) .Values.serviceAccount.name }}
{{- else }}
{{- default "default" .Values.serviceAccount.name }}
{{- end }}
{{- end }}
Loading