-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJenkinsfile
More file actions
69 lines (54 loc) · 2.23 KB
/
Copy pathJenkinsfile
File metadata and controls
69 lines (54 loc) · 2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
pipeline {
agent any
environment{
// Docker variables
DOCKER_USER = 'anil3494'
DOCKER_IMAGE = 'raindrop-restaurant'
// Kub Variables
KUB_MASTER_HOST = 'ubuntu@35.78.192.110'
KUB_MASTER_CREDS = 'k8s-master-ssh'
}
stages{
stage('Build Docker Image'){
steps{
echo 'Building Docker Image: ${DOCKER_USER}/${DOCKER_IMAGE} with tag as ${BUILD_NUMBER}'
sh 'docker build -t ${DOCKER_USER}/${DOCKER_IMAGE}:${BUILD_NUMBER} .'
echo "Docker image built successfully!"
}
}
stage('Push Docker Image to Docker Hub'){
steps {
echo "Pushing image to Docker Hub..."
// This block securely loads the credentials
withCredentials([usernamePassword(credentialsId: 'ak-dockerhub-creds', usernameVariable: 'DOCKER_USER_VAR', passwordVariable: 'DOCKER_PASS_VAR')]) {
sh '''
# Log in to Docker Hub using the credentials
# Using --password-stdin is secure and prevents the password from being logged
echo $DOCKER_PASS_VAR | docker login -u $DOCKER_USER_VAR --password-stdin
# Push the image that was built in the previous stage
docker push ${DOCKER_USER}/${DOCKER_IMAGE}:${BUILD_NUMBER}
'''
}
echo "Image pushed successfully!"
}
}
stage('Deploy to Kub'){
steps{
echo 'Deploying to Kubernetes Cluster...'
sshagent (credentials: [KUB_MASTER_CREDS]) {
sh '''
ssh -o StrictHostKeyChecking=no ${KUB_MASTER_HOST} \
"sudo kubectl set image deployment/raindrop-deployment raindrop-container=${DOCKER_USER}/${DOCKER_IMAGE}:${BUILD_NUMBER}"
'''
}
echo "Deployment to Kubernetes complete!"
}
}
}
post{
always{
// echo 'Pipeline CP 3 DONE -- Image built and pushed to Docker Hub'
echo 'Pipeline CP 4 DONE -- Full CI/CD complete!'
}
}
}