Skip to content

jenkins update #180

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 72 additions & 9 deletions Jenkinsfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,74 @@
pipeline {
agent any
stages {
stage("git_checkout") {
steps {
echo "cloning repository"
echo "repo cloned successfully"
}
}
pipeline {
agent any

environment {
MAVEN_HOME = tool 'maven3.9.0' // Maven tool installed in Jenkins
IMAGE_NAME = 'veedhi1995/onlinestore-java-tomcat'
KUBECONFIG = credentials('yamlcred') // Kubernetes config stored in Jenkins
}

stages {
stage("git checkout") {
steps {
git branch: 'veedhi', url: 'https://github.com/veedhi25/onlinebookstore.git'
}
}

stage("Build") {
steps {
script {
sh "${MAVEN_HOME}/bin/mvn clean package"
}
}
}

stage("Image Build & Push") {
steps {
script {
// Get latest Git commit hash
def GIT_COMMIT = sh(script: 'git rev-parse --short HEAD', returnStdout: true).trim()
def IMAGE_TAG = "${IMAGE_NAME}:${GIT_COMMIT}"

echo "Building Docker image: ${IMAGE_TAG}"

sh "docker build -t ${IMAGE_TAG} ."
echo "Image built successfully"

withCredentials([usernamePassword(credentialsId: 'hubcred', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) {
sh "echo ${PASSWORD} | docker login -u ${USERNAME} --password-stdin"
echo "Login successful"

echo "Pushing Docker image: ${IMAGE_TAG}"
sh "docker push ${IMAGE_TAG}"
echo "Image pushed successfully"
}

// Store IMAGE_TAG for next stages
env.BUILT_IMAGE_TAG = IMAGE_TAG
}
}
}

stage("Deploy to Kubernetes") {
steps {
script {
// Ensure the environment variable is accessible
def IMAGE_TAG = env.BUILT_IMAGE_TAG

echo "Deploying image: ${IMAGE_TAG} to Kubernetes"

// Set Kubernetes context
sh "kubectl config use-context kubernetes-admin@kubernetes"
sh "kubectl config current-context"

// Update Kubernetes deployment with the specific image tag
sh "kubectl set image deployment/onlinebookstore onlinebookstore=${IMAGE_TAG} -n onlinebookstore-ns"

// Restart the deployment to apply changes
sh "kubectl rollout restart deployment onlinebookstore -n onlinebookstore-ns"
echo "Deployment updated successfully with image ${IMAGE_TAG}"
}
}
}
}
}
11 changes: 11 additions & 0 deletions dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM tomcat:latest
# set env variables
ENV CATALINA_HOME /usr/local/tomcat
ENV APP_NAME onlinebookstore
#copy the war file to webappsdirectory
COPY target/${APP_NAME}.war ${CATALINA_HOME}/webapps/
#Expose port
EXPOSE 8080
#start tomcat
CMD [ "catalina.sh" , "run" ]