Skip to content

Latest commit

 

History

History
253 lines (230 loc) · 13.4 KB

Java_Techi_Jenkins.md

File metadata and controls

253 lines (230 loc) · 13.4 KB

Java Techi Jenkins

src: https://www.youtube.com/watch?v=1QLMqyFFEzU&list=PLVz2XdJiJQxwS0BZUHX34ocLTJtRGSQzN&index=2

image image image image image image image

Install Jenkins

image image image image image

  • Create a spring project or any java project image
  • Create new repository spring-jenkins
  • image
  • Create a new job
  • image
  • image
  • image
  • Schedule job cron expression
  • image
  • image
  • image
  • image
  • When we changed in code , commit and pushed build will automaticall triggered
  • image
  • When build fail
  • image
  • image
  • Email configuration
  • image
  • image

Continuous Integration Pipeline

  • image

  • image

  • image

  • image

  • image

  • image

  • Create build pipeline

  • Create new job as DEV maven goal will be install

  • image

  • Create new job UAT maven goal will be test

  • image

  • Create new job PROD maven goal will be ```install``

  • image

  • Create build pipeline using build pipeline plugin please install plugin before create pipeline else option won't available

  • image

  • image

  • image

  • image

  • Configure each pipeling for after which job it has to run like DEV -> UAT -> PROD

  • image

  • image

  • Do some code change and commit

  • image

  • image

  • Building Docker Images using Jenkins step by step image

  • Create new job

  • image

  • image

  • image

  • image

  • In order to connect Jenkins with Docker we need to add some below plugins

  • image

  • image

  • We need to create docler file

  • image

  • Commit and push to remote repo

  • image

  • image

  • Start a build

  • image


Build Docker image using Jenkins pipeline and puch to Docker hub

  • image
  • Create Pipeline
  • image
  • image
  • image
  • image
  • image
  • image
  • image
  • Docker file
  • image
  • Commit the docker file

image

  • We can create user and password environment variable

  • image

  • image

  • image

  • image

  • After Configuration build now

  • image

  • Another way we can commit Jenkinsfile in github

  • image


CI CD Pipeline Using Jenkins | Deploy Docker Image to Kubernetes using Jenkins

  • image
  • We need to add plugin for kubernetes
  • image
  • Create deployment.yaml file
  • image
  • image
  • Commit this file to remote repo
  • image
  • image
  • image
  • Build Now
  • image

Docker file

FROM openjdk:8
EXPOSE 8080
ADD target/devops-integration.jar devops-integration.jar
ENTRYPOINT ["java","-jar","/devops-integration.jar"]

Jenkinsfile

pipeline {
    agent any
    tools{
        maven 'maven_3_5_0'
    }
    stages{
        stage('Build Maven'){
            steps{
                checkout([$class: 'GitSCM', branches: [[name: '*/main']], extensions: [], userRemoteConfigs: [[url: 'https://github.com/Java-Techie-jt/devops-automation']]])
                sh 'mvn clean install'
            }
        }
        stage('Build docker image'){
            steps{
                script{
                    sh 'docker build -t javatechie/devops-integration .'
                }
            }
        }
        stage('Push image to Hub'){
            steps{
                script{
                   withCredentials([string(credentialsId: 'dockerhub-pwd', variable: 'dockerhubpwd')]) {
                   sh 'docker login -u javatechie -p ${dockerhubpwd}'

}
                   sh 'docker push javatechie/devops-integration'
                }
            }
        }
        stage('Deploy to k8s'){
            steps{
                script{
                    kubernetesDeploy (configs: 'deploymentservice.yaml',kubeconfigId: 'k8sconfigpwd')
                }
            }
        }
    }
}
node{
   stage('SCM Checkout'){
       git credentialsId: 'git-creds', url: 'https://github.com/javahometech/my-app'
   }
   stage('Mvn Package'){
     def mvnHome = tool name: 'maven-3', type: 'maven'
     def mvnCMD = "${mvnHome}/bin/mvn"
     sh "${mvnCMD} clean package"
   }
   stage('Build Docker Image'){
     sh 'docker build -t kammana/my-app:2.0.0 .'
   }
   stage('Push Docker Image'){
     withCredentials([string(credentialsId: 'docker-pwd', variable: 'dockerHubPwd')]) {
        sh "docker login -u kammana -p ${dockerHubPwd}"
     }
     sh 'docker push kammana/my-app:2.0.0'
   }
   stage('Run Container on Dev Server'){
     def dockerRun = 'docker run -p 8080:8080 -d --name my-app kammana/my-app:2.0.0'
     sshagent(['dev-server']) {
       sh "ssh -o StrictHostKeyChecking=no ec2-user@172.31.18.198 ${dockerRun}"
     }
   }
}

Deployment.yaml

apiVersion: apps/v1
kind: Deployment # Kubernetes resource kind we are creating
metadata:
  name: spring-boot-k8s-deployment
spec:
  selector:
    matchLabels:
      app: spring-boot-k8s
  replicas: 2 # Number of replicas that will be created for this deployment
  template:
    metadata:
      labels:
        app: spring-boot-k8s
    spec:
      containers:
        - name: spring-boot-k8s
          image: javatechie/devops-integration # Image that will be used to containers in the cluster
          imagePullPolicy: IfNotPresent
          ports:
            - containerPort: 8080 # The port that the container is running on in the cluster


---

apiVersion: v1 # Kubernetes API version
kind: Service # Kubernetes resource kind we are creating
metadata: # Metadata of the resource kind we are creating
  name: springboot-k8ssvc
spec:
  selector:
    app: spring-boot-k8s
  ports:
    - protocol: "TCP"
      port: 8080 # The port that the service is running on in the cluster
      targetPort: 8080 # The port exposed by the service
  type: NodePort # type of the service.