Skip to content

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

7 Commits

Folders and files

Repository files navigation

CI/CD: Deploy application to k8s in Windows OS with Jenkins Declarative CI/CD

  1. Add k8s required plugins 2

  2. After adding the plugin in your Jenkins script, click on Generate pipeline syntax.

  3. Next, from the dropdown, select kubeconfig 3

  4. Next, in the credential section, click on add -> Jenkins 4

  5. Next, select secret file from the dropdown and provide the path of your kube config. Usually, you will find your kubeconfig file from the below path 1 Once you execute the select command, then it will appear content like below 9 Just upload this file in the credential section like this 5

  6. Next, once you complete this, it will generate syntax for you 6, copy this and use it in your script.

  7. Use the below script for Windows:

    pipeline {
        agent any
     
        tools {
            // Define Maven tool
            maven "maven"
        }
    
        environment {
            // Define environment variables
            APP_NAME = "user-service"
            RELEASE_NO = "1.0.0"
            DOCKER_USER = "javatechie4u"
            IMAGE_NAME = "${DOCKER_USER}/${APP_NAME}"
            IMAGE_TAG = "${RELEASE_NO}-${BUILD_NUMBER}"
        }
    
        stages {
            stage("SCM checkout") {
                steps {
                    // Checkout code from Git
                    checkout([$class: 'GitSCM', branches: [[name: '*/main']], userRemoteConfigs: [[url: 'https://github.com/javatechie-devops/user-service.git']]])
                }
            }
    
            stage("Build Process") {
                steps {
                    // Build the project using Maven
                    bat 'mvn clean install'
                }
            }
    
            stage("Build Image") {
                steps {
                    // Build Docker image
                    bat "docker build -t ${IMAGE_NAME}:${IMAGE_TAG} ."
                }
            }
    
            stage("Deploy Image to Hub") {
                steps {
                    // Login to Docker Hub
                     withCredentials([string(credentialsId: 'dpwd', variable: 'dpwd')]) {
                        bat "docker login -u ${DOCKER_USER} -p ${dpwd}"
                        bat "docker push ${IMAGE_NAME}:${IMAGE_TAG}"
                    }
                }
            }
    
            stage("Deploy to Kubernetes") {
                steps {
                    script {
                        kubeconfig(credentialsId: 'kubeconfig', serverUrl: '') {
                            bat """powershell -Command "(Get-Content k8s-app.yaml) -replace 'image: .*', 'image: ${IMAGE_NAME}:${IMAGE_TAG}' | Set-Content k8s-app.yaml" """
                            bat 'kubectl apply -f k8s-app.yaml'
                        }
                    }
                }
            }
    
            stage("Verify deployment") {
                steps {
                    script {
                        kubeconfig(credentialsId: 'kubeconfig', serverUrl: '') {
                            // Check deployed pods
                            bat 'kubectl get pods -n user-app'
                        }
                    }
                }
            }
        }
    }
  8. Build your job then you could see successful results in the pipeline 8

  9. validate k8s componenet 7

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages