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 agent any
environment { environment {
DOCKERHUB_USER = 'vipin2025devops'
IMAGE_NAME = 'myapp' IMAGE_NAME = 'myapp'
IMAGE_TAG = 'latest' IMAGE_TAG = 'latest'
K8S_DEPLOYMENT = 'k8s/deployment.yaml' DOCKERHUB_USER = 'vipin2025devops'
K8S_SERVICE = 'k8s/service.yaml' DEPLOYMENT_FILE = 'k8s/deployment.yaml'
SERVICE_FILE = 'k8s/service.yaml'
} }
stages { stages {
stage('Checkout Code') { stage('Checkout Code') {
steps { 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 { steps {
powershell """ script {
docker build -t ${DOCKERHUB_USER}/${IMAGE_NAME}:${IMAGE_TAG} . 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 { steps {
withCredentials([usernamePassword(
credentialsId: 'dockerhub-creds',
usernameVariable: 'DOCKER_USER',
passwordVariable: 'DOCKER_PASS')]) {
powershell """ powershell """
echo $env:DOCKER_PASS | docker login -u $env:DOCKER_USER --password-stdin docker images | Select-String '${DOCKERHUB_USER}/${IMAGE_NAME}'
docker push ${DOCKERHUB_USER}/${IMAGE_NAME}:${IMAGE_TAG}
""" """
} }
} }
}
stage('Deploy to Kubernetes') { stage('Deploy to Kubernetes') {
steps { steps {
withCredentials([file(credentialsId: 'kubeconfig-file', variable: 'KUBECONFIG_PATH')]) {
powershell """ powershell """
kubectl apply -f ${K8S_DEPLOYMENT} kubectl --kubeconfig $env:KUBECONFIG_PATH apply -f k8s/deployment.yaml --validate=false
kubectl apply -f ${K8S_SERVICE} 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') { stage('Verify Deployment') {
steps { steps {
withCredentials([file(credentialsId: 'kubeconfig-file', variable: 'KUBECONFIG_PATH')]) {
powershell """ 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."
}
}
}