Fix credentials ID for Docker Hub push

This commit is contained in:
VIPIN 2025-09-12 15:13:29 +05:30
parent a06fc69fce
commit c643bffb51

60
Jenkinsfile vendored
View File

@ -2,57 +2,81 @@ pipeline {
agent any
environment {
DOCKERHUB_USER = 'vipin2025devops'
IMAGE_NAME = 'myapp'
IMAGE_TAG = 'latest'
K8S_DEPLOYMENT = 'k8s/deployment.yaml'
K8S_SERVICE = 'k8s/service.yaml'
DOCKERHUB_USER = 'vipin2025devops'
DEPLOYMENT_FILE = 'k8s/deployment.yaml'
SERVICE_FILE = 'k8s/service.yaml'
}
stages {
stage('Checkout Code') {
steps {
checkout scm
git branch: 'main',
url: 'https://git.opsmonsters.in/vipin.p/my-react-app.git',
credentialsId: 'git-opsmonsters-cred'
}
}
stage('Build Docker Image') {
stage('Build & Push Docker Image') {
steps {
powershell """
docker build -t ${DOCKERHUB_USER}/${IMAGE_NAME}:${IMAGE_TAG} .
script {
withCredentials([usernamePassword(credentialsId: 'dockerhub-credentials', usernameVariable: 'DOCKER_USER', passwordVariable: 'DOCKER_PASS')]) {
sh """
echo $DOCKER_PASS | docker login -u $DOCKER_USER --password-stdin
docker build -t vipin2025devops/myapp:latest .
docker push vipin2025devops/myapp:latest
"""
}
}
}
}
stage('Push Image to Docker Hub') {
stage('Verify Docker Image') {
steps {
withCredentials([usernamePassword(
credentialsId: 'dockerhub-creds',
usernameVariable: 'DOCKER_USER',
passwordVariable: 'DOCKER_PASS')]) {
powershell """
echo $env:DOCKER_PASS | docker login -u $env:DOCKER_USER --password-stdin
docker push ${DOCKERHUB_USER}/${IMAGE_NAME}:${IMAGE_TAG}
docker images | Select-String '${DOCKERHUB_USER}/${IMAGE_NAME}'
"""
}
}
}
stage('Deploy to Kubernetes') {
steps {
withCredentials([file(credentialsId: 'kubeconfig-file', variable: 'KUBECONFIG_PATH')]) {
powershell """
kubectl apply -f ${K8S_DEPLOYMENT}
kubectl apply -f ${K8S_SERVICE}
kubectl --kubeconfig $env:KUBECONFIG_PATH apply -f k8s/deployment.yaml --validate=false
kubectl --kubeconfig $env:KUBECONFIG_PATH apply -f k8s/service.yaml --validate=false
kubectl --kubeconfig $env:KUBECONFIG_PATH get pods -o wide
kubectl --kubeconfig $env:KUBECONFIG_PATH get svc
"""
}
}
}
stage('Verify Deployment') {
steps {
withCredentials([file(credentialsId: 'kubeconfig-file', variable: 'KUBECONFIG_PATH')]) {
powershell """
kubectl get pods -o wide
echo 'Pods Status:'
kubectl --kubeconfig $env:KUBECONFIG_PATH get pods -o wide
echo 'Services:'
kubectl --kubeconfig $env:KUBECONFIG_PATH get svc
"""
}
}
}
}
post {
success {
echo " Deployment successful!"
}
failure {
echo " Deployment failed. Check logs above."
}
}
}