Skip to content

Commit

Permalink
test deployment
Browse files Browse the repository at this point in the history
  • Loading branch information
folland87 committed Oct 22, 2024
1 parent 8081b47 commit 2dab081
Show file tree
Hide file tree
Showing 7 changed files with 223 additions and 81 deletions.
166 changes: 166 additions & 0 deletions .github/actions/k8s-deploy/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
name: "Kubernetes Deploy Action"
description: "Deploy an application to Kubernetes"
inputs:
app_port:
description: "The port on which the application runs"
required: false
default: "3000"
replicas:
description: "Number of replicas for the deployment"
required: false
default: "1"
cpu_request:
description: "CPU request for the container (e.g., 100m)"
required: false
default: "100m"
cpu_limit:
description: "CPU limit for the container (e.g., 200m)"
required: false
default: "200m"
memory_request:
description: "Memory request for the container (e.g., 128Mi)"
required: false
default: "128Mi"
memory_limit:
description: "Memory limit for the container (e.g., 256Mi)"
required: false
default: "256Mi"
kube_config:
description: "Base64 encoded kubeconfig file"
required: true
env_secrets:
description: "Base64 encoded .env file containing secrets"
required: false
default: ""

outputs:
deployment_url:
description: "The URL of the deployed application"
value: ${{ steps.set-output.outputs.deployment_url }}

runs:
using: "composite"
steps:
- name: Check out repository
uses: actions/checkout@v2

- name: Set up kubectl
uses: azure/setup-kubectl@v1

- name: Configure kubectl
shell: bash
run: |
echo "${{ inputs.kube_config }}" | base64 -d > kubeconfig.yaml
export KUBECONFIG=./kubeconfig.yaml
- name: Install Kustomize
shell: bash
run: |
curl -s "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh" | bash
sudo mv kustomize /usr/local/bin/
- name: Set environment variables
shell: bash
run: |
echo "BRANCH_NAME=$(echo ${GITHUB_REF#refs/heads/})" >> $GITHUB_ENV
echo "IMAGE_TAG=$(echo $GITHUB_SHA | cut -c1-7)" >> $GITHUB_ENV
echo "REPO_NAME=$(echo ${GITHUB_REPOSITORY#*/} | tr '[:upper:]' '[:lower:]')" >> $GITHUB_ENV
echo "DEPLOYMENT_URL=$(echo ${GITHUB_REPOSITORY#*/} | tr '[:upper:]' '[:lower:]')-${GITHUB_REF#refs/heads/}.staging.dataesr.ovh" >> $GITHUB_ENV
- name: Build and push Docker image
shell: bash
run: |
docker build -t ghcr.io/${{ github.repository_owner }}/${{ env.REPO_NAME }}:${{ env.IMAGE_TAG }} .
docker push ghcr.io/${{ github.repository_owner }}/${{ env.REPO_NAME }}:${{ env.IMAGE_TAG }}
- name: Create Kustomize files
shell: bash
run: |
mkdir -p kustomize/base
cat <<EOF > kustomize/base/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: ${{ env.REPO_NAME }}
spec:
replicas: ${{ inputs.replicas }}
selector:
matchLabels:
app: ${{ env.REPO_NAME }}
template:
metadata:
labels:
app: ${{ env.REPO_NAME }}
spec:
containers:
- name: app
image: ghcr.io/${{ github.repository_owner }}/${{ env.REPO_NAME }}:${{ env.IMAGE_TAG }}
ports:
- containerPort: ${{ inputs.app_port }}
resources:
requests:
cpu: ${{ inputs.cpu_request }}
memory: ${{ inputs.memory_request }}
limits:
cpu: ${{ inputs.cpu_limit }}
memory: ${{ inputs.memory_limit }}
${{ inputs.env_secrets != '' && 'envFrom:' || '' }}
${{ inputs.env_secrets != '' && '- secretRef:' || '' }}
${{ inputs.env_secrets != '' && ' name: ${{ env.REPO_NAME }}-secrets' || '' }}
EOF
cat <<EOF > kustomize/base/service.yaml
apiVersion: v1
kind: Service
metadata:
name: ${{ env.REPO_NAME }}-svc
spec:
selector:
app: ${{ env.REPO_NAME }}
ports:
- port: 80
targetPort: ${{ inputs.app_port }}
EOF
cat <<EOF > kustomize/base/ingress.yaml
apiVersion: traefik.io/v1alpha1
kind: IngressRoute
metadata:
name: ${{ env.REPO_NAME }}
spec:
entryPoints:
- websecure
routes:
- match: Host(${{ env.DEPLOYMENT_URL }})
kind: Rule
services:
- name: ${{ env.REPO_NAME }}-svc
port: 80
tls: {}
EOF
cat <<EOF > kustomize/base/kustomization.yaml
resources:
- deployment.yaml
- service.yaml
- ingress.yaml
${{ inputs.env_secrets != '' && '- secrets.yaml' || '' }}
EOF
- name: Create Kubernetes Secret
if: inputs.env_secrets != ''
shell: bash
run: |
echo "${{ inputs.env_secrets }}" | base64 -d > .env
kubectl create secret generic ${{ env.REPO_NAME }}-secrets --from-env-file=.env --dry-run=client -o yaml > kustomize/base/secrets.yaml
rm .env
- name: Deploy to Kubernetes
shell: bash
run: |
kustomize build kustomize/base | kubectl apply -f -
- name: Set output
id: set-output
shell: bash
run: echo "deployment_url=${{ env.DEPLOYMENT_URL }}" >> $GITHUB_OUTPUT
27 changes: 27 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Deploy to Kubernetes

on:
push:
branches:
- "main"

jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Deploy to Kubernetes
uses: ./.github/actions/k8s-deploy
with:
app_port: 3000
replicas: 2
cpu_request: 100m
cpu_limit: 200m
memory_request: 128Mi
memory_limit: 256Mi
kube_config: ${{ secrets.KUBECONFIG }}
docker_username: ${{ secrets.DOCKER_USERNAME }}
docker_password: ${{ secrets.DOCKER_PASSWORD }}
env_secrets: ${{ secrets.MAIN_ENV_SECRETS }}
- name: Get deployment URL
run: echo "Deployed to ${{ steps.deploy.outputs.deployment_url }}"
80 changes: 0 additions & 80 deletions .github/workflows/deployment.yml

This file was deleted.

4 changes: 4 additions & 0 deletions client/src/components/header.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { Link } from "react-router-dom";
import useVersion from "@/hooks/useVersion";


const Header: React.FC = () => {
const { data } = useVersion();
return (
<header role="banner" className="fr-header">
<div className="fr-header__body">
Expand All @@ -24,6 +27,7 @@ const Header: React.FC = () => {
<a href="/" title="Accueil - [À MODIFIER - Nom du site / service] - Nom de l’entité (ministère, secrétariat d‘état, gouvernement)">
<p className="fr-header__service-title">
Nom du site / service
{data?.version && <span className="fr-badge fr-badge--green-emeraude">{data?.version}</span>}
</p>
</a>
<p className="fr-header__service-tagline">baseline - précisions sur l‘organisation</p>
Expand Down
12 changes: 12 additions & 0 deletions client/src/hooks/useVersion.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import api from '@/api-client';
import { useQuery } from '@tanstack/react-query';

export default function useVersion() {
return useQuery({
queryKey: ['version'],
queryFn: async () => {
const { data } = await api.version.get();
return data;
}
})
}
14 changes: 13 additions & 1 deletion server/src/routes/healthy.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Elysia from 'elysia';

import { HealthySchema, PingPongSchema } from '~/schemas/healthy';
import { HealthySchema, PingPongSchema, VersionSchema } from '~/schemas/healthy';


export const healthyRoutes = new Elysia()
Expand All @@ -21,3 +21,15 @@ export const healthyRoutes = new Elysia()
summary: 'Check if the server is healthy',
}
)
.get(
'/version',
() => {
return { version: Bun.env.VERSION ?? '0.0.0' }
},
{
reponse: {
200: VersionSchema
},
summary: 'Returns the version of the application',
}
)
1 change: 1 addition & 0 deletions server/src/schemas/healthy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export const HealthySchema = t.Object({
healthy: t.Boolean()
});
export const PingPongSchema = t.String();
export const VersionSchema = t.String();

0 comments on commit 2dab081

Please sign in to comment.