src: https://www.youtube.com/watch?v=1QLMqyFFEzU&list=PLVz2XdJiJQxwS0BZUHX34ocLTJtRGSQzN&index=2
- Create a spring project or any java project
- Create new repository spring-jenkins
- Create a new job
- Schedule job cron expression
- When we changed in code , commit and pushed build will automaticall triggered
- When build fail
- Email configuration
-
Create build pipeline
-
Create new job as DEV maven goal will be
install
-
Create new job UAT maven goal will be
test
-
Create new job PROD maven goal will be ```install``
-
Create build pipeline using build pipeline plugin please install plugin before create pipeline else option won't available
-
Configure each pipeling for after which job it has to run like DEV -> UAT -> PROD
-
Do some code change and commit
-
Create new job
-
In order to connect Jenkins with Docker we need to add some below plugins
-
We need to create docler file
-
Commit and push to remote repo
-
Start a build
-
We can create user and password environment variable
-
After Configuration build now
-
Another way we can commit
Jenkinsfile
in github
- We need to add plugin for kubernetes
- Create deployment.yaml file
- Commit this file to remote repo
- Build Now
FROM openjdk:8
EXPOSE 8080
ADD target/devops-integration.jar devops-integration.jar
ENTRYPOINT ["java","-jar","/devops-integration.jar"]
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}"
}
}
}
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.