Petclinic-22-06
Petclinic-22-06
Petclinic-22-06
Create an IAM role with administrator access and add it to your Jenkins machine, in case when you use
slave attach it to slave machine
INSTALLATIONS
awscli =>
=========
eksctl
=======
eksctl version
kubectl
=======
eksctl create cluster --name cloud-eks --region ap-south-1 --nodegroup-name my-nodes --node-type
t3.small --managed --nodes 1
aws eks update-kubeconfig --name cloud-eks --region ap-south-1
USE JAVA 11
pipeline {
agent {
label "Agent1"
}
tools {
jdk "java17"
maven "M3"
}
environment {
APP_NAME = "petclinic"
RELEASE = "1.0.0"
DOCKER_USER = "sevenajay"
DOCKER_PASS = "dockerhub"
IMAGE_NAME = "${DOCKER_USER}" + "/" + "${APP_NAME}"
IMAGE_TAG = "${RELEASE}-${BUILD_NUMBER}"
DEPLOYMENT_FILE = "kubernetes/petclinic.yaml"
}
stages {
stage("clean workspace"){
steps {
cleanWs()
}
}
stage("checkout scm"){
steps {
checkout scmGit(branches: [[name: '*/master']],
extensions: [], userRemoteConfigs: [[url:
'https://github.com/Aj7Ay/amazon-eks-jenkins-terraform-aj7.git']])
}
}
stage("build stage"){
steps {
sh "mvn clean package"
}
}
stage("test code"){
steps {
sh "mvn test"
}
}
stage("sonar checks"){
steps {
script {
withSonarQubeEnv(credentialsId: 'sonar') {
sh "mvn sonar:sonar"
}
}
}
}
stage("quality gate"){
steps {
script {
waitForQualityGate abortPipeline: false, credentialsId:
'sonar'
}
}
}
stage ("docker build") {
steps {
script {
docker.withRegistry('',DOCKER_PASS) {
// Remove old images from docker repository
sh "docker images --format '{{.Repository}}:
{{.Tag}}' | grep ${IMAGE_NAME} | grep -v ${RELEASE}-${BUILD_NUMBER} |
grep -v latest | xargs -I {} docker rmi {} || true"
docker_image = docker.build "${IMAGE_NAME}"
}
}
}
}
stage ("Trivy image scan") {
steps {
script {
sh "trivy image ${docker_image.id} > trivy.txt"
}
}
}
stage ("PUSH docker image") {
steps {
script {
docker.withRegistry('',DOCKER_PASS) {
docker_image.push("${IMAGE_TAG}")
docker_image.push('latest')
}
}
}
}
stage ("deployment to k8s") {
steps {
script {
withKubeConfig(caCertificate: '', clusterName: '',
contextName: '', credentialsId: 'k8s', namespace: '',
restrictKubeConfigAccess: false, serverUrl: '') {
//UPDATE THE IMAGE NAME IN DEPLOYMENT YAML FILE
sh "sed -i '/^\\s*image:/ s|.*| image: $
{IMAGE_NAME}|' ${DEPLOYMENT_FILE}"
FROM anapsix/alpine-java
VOLUME /tmp
EXPOSE 8080
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.war"]
If you want with tag name also in K8S DEPLOYMENT USE THIS STAGE
stage("k8s deployment") {
steps {
script {
// Update the image in the deployment YAML file
sh "sed -i '/^\\s*image:/ s|.*| image: ${IMAGE_NAME}:$
{IMAGE_TAG}|' ${DEPLOYMENT_FILE}"