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

110
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' DOCKERHUB_USER = 'vipin2025devops'
K8S_DEPLOYMENT = 'k8s/deployment.yaml' DEPLOYMENT_FILE = 'k8s/deployment.yaml'
K8S_SERVICE = 'k8s/service.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') {
steps {
powershell """
docker build -t ${DOCKERHUB_USER}/${IMAGE_NAME}:${IMAGE_TAG} .
"""
}
}
stage('Push Image to Docker Hub') { stage('Build & Push Docker Image') {
steps { steps {
withCredentials([usernamePassword( script {
credentialsId: 'dockerhub-creds', withCredentials([usernamePassword(credentialsId: 'dockerhub-credentials', usernameVariable: 'DOCKER_USER', passwordVariable: 'DOCKER_PASS')]) {
usernameVariable: 'DOCKER_USER', sh """
passwordVariable: 'DOCKER_PASS')]) { echo $DOCKER_PASS | docker login -u $DOCKER_USER --password-stdin
powershell """ docker build -t vipin2025devops/myapp:latest .
echo $env:DOCKER_PASS | docker login -u $env:DOCKER_USER --password-stdin docker push vipin2025devops/myapp:latest
docker push ${DOCKERHUB_USER}/${IMAGE_NAME}:${IMAGE_TAG}
"""
}
}
}
stage('Deploy to Kubernetes') {
steps {
powershell """
kubectl apply -f ${K8S_DEPLOYMENT}
kubectl apply -f ${K8S_SERVICE}
"""
}
}
stage('Verify Deployment') {
steps {
powershell """
kubectl get pods -o wide
""" """
} }
} }
} }
} }
stage('Verify Docker Image') {
steps {
powershell """
docker images | Select-String '${DOCKERHUB_USER}/${IMAGE_NAME}'
"""
}
}
stage('Deploy to Kubernetes') {
steps {
withCredentials([file(credentialsId: 'kubeconfig-file', variable: 'KUBECONFIG_PATH')]) {
powershell """
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 """
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."
}
}
}