From 4901890d0ec512078fecf5551e52707b83bac221 Mon Sep 17 00:00:00 2001 From: Marcin-Ramotowski Date: Thu, 12 Jun 2025 19:02:29 +0000 Subject: [PATCH] Created pipeline for Jenkins in Kubernetes cluster --- .jenkins/Jenkinsfile | 90 +++++++++++++++++++++++++++++++++++++++ .jenkins/podTemplate.yaml | 30 +++++++++++++ Jenkinsfile | 34 --------------- 3 files changed, 120 insertions(+), 34 deletions(-) create mode 100644 .jenkins/Jenkinsfile create mode 100644 .jenkins/podTemplate.yaml delete mode 100644 Jenkinsfile diff --git a/.jenkins/Jenkinsfile b/.jenkins/Jenkinsfile new file mode 100644 index 0000000..c595e9b --- /dev/null +++ b/.jenkins/Jenkinsfile @@ -0,0 +1,90 @@ +pipeline { + agent { + kubernetes { + defaultContainer 'kubectl' + yamlFile '.jenkins/podTemplate.yaml' + } + } + environment { + RESOURCE_GROUP = 'tst-aks-rg' + CLUSTER_NAME = 'build' + DEPLOY_FILES = 'namespace.yaml secret-store.yaml deploy.yaml ingress.yaml' + NAMESPACE = 'user-microservice' + DEPLOYMENT = 'api' + } + stages { + stage('Checkout') { + steps { + container('kubectl') { + checkout scm + } + } + } + stage('Login to Azure & Get Kubeconfig') { + steps { + container('kubectl') { + sh ''' + az login --identity + az aks get-credentials --resource-group $RESOURCE_GROUP --name $CLUSTER_NAME --overwrite-existing + ''' + } + container('kubelogin') { + sh ''' + kubelogin convert-kubeconfig -l azurecli + ''' + } + } + } + stage('Apply Kubernetes Resources') { + steps { + container('kubectl') { + script { + def files = DEPLOY_FILES.tokenize() + for (file in files) { + sh "kubectl apply -f ${file}" + } + } + } + } + } + + stage('Verify Deployment') { + steps { + container('kubectl') { + script { + // Waiting until all pods reach "ready" status + sh ''' + echo "Waiting for deployment rollout..." + kubectl rollout status deployment/$DEPLOYMENT -n $NAMESPACE --timeout=60s + ''' + } + } + } + } + + stage('Health Check') { + steps { + container('kubectl') { + script { + // Check if app is healthy + def ingressUrl = "https://user-microservice.marcin00.pl/health" + sh """ + echo "Checking app health ${ingressUrl}..." + for i in {1..30}; do + if curl -sf $ingressUrl; then + echo "Health check OK" + exit 0 + else + echo "Health check failed. Retry \$i..." + sleep 5 + fi + done + echo "Health check failed." + exit 1 + """ + } + } + } + } + } +} diff --git a/.jenkins/podTemplate.yaml b/.jenkins/podTemplate.yaml new file mode 100644 index 0000000..37bb1f0 --- /dev/null +++ b/.jenkins/podTemplate.yaml @@ -0,0 +1,30 @@ +apiVersion: v1 +kind: Pod +metadata: + labels: + some-label: jenkins-agent +spec: + containers: + - name: kubectl + image: mcr.microsoft.com/azure-cli:latest + command: + - cat + tty: true + volumeMounts: + - name: azure-config + mountPath: /root/.azure + + - name: kubelogin + image: ghcr.io/int128/kubelogin:latest + command: + - cat + tty: true + + volumes: + - name: azure-config + emptyDir: {} + + nodeSelector: + kubernetes.io/os: linux + + restartPolicy: Never diff --git a/Jenkinsfile b/Jenkinsfile deleted file mode 100644 index 366cc23..0000000 --- a/Jenkinsfile +++ /dev/null @@ -1,34 +0,0 @@ -pipeline { - agent any - stages { - stage('Checkout') { - steps { - checkout scm - } - } - stage('Get credentials to Cluster') { - steps { - sh ''' - az login --identity - az aks get-credentials --resource-group tst-aks-rg --name edu - kubelogin convert-kubeconfig -l azurecli - ''' - } - } - stage('Apply to Cluster') { - steps { - sh ''' - kubectl apply -f namespace.yaml - kubectl apply -f secret-store.yaml - kubectl apply -f deploy.yaml - kubectl apply -f ingress.yaml - ''' - } - } - } - post { - cleanup { - sh 'rm -f ~/.kube/config || true' - } - } -}