my-react-app/Jenkinsfile
2025-09-11 10:11:02 +05:30

50 lines
1.5 KiB
Groovy

pipeline {
agent any
environment {
DOCKERHUB_REPO = "vipin2025devops/myapp"
IMAGE_TAG = "${env.BUILD_NUMBER}"
}
stages {
stage('Checkout') {
steps {
checkout scm
}
}
stage('Build & Push Image') {
steps {
script {
docker.withRegistry('https://index.docker.io/v1/', 'dockerhub-creds') {
def img = docker.build("${DOCKERHUB_REPO}:${IMAGE_TAG}")
img.push()
// cleanup local image to save space
bat "docker image rm ${DOCKERHUB_REPO}:${IMAGE_TAG} || exit 0"
}
}
}
}
stage('Deploy to Kubernetes') {
steps {
withCredentials([file(credentialsId: 'kubeconfig', variable: 'KUBECONFIG')]) {
script {
bat """
powershell -Command "(Get-Content k8s/deployment.yaml) -replace 'IMAGE_PLACEHOLDER', '${DOCKERHUB_REPO}:${IMAGE_TAG}' | Set-Content k8s/deployment-to-apply.yaml"
kubectl --kubeconfig=%KUBECONFIG% apply -f k8s/deployment-to-apply.yaml
kubectl --kubeconfig=%KUBECONFIG% apply -f k8s/service.yaml
"""
}
}
}
}
}
post {
always {
echo "Pipeline finished: ${currentBuild.fullDisplayName}"
}
}
}