-
Notifications
You must be signed in to change notification settings - Fork 128
/
Copy pathJenkinsfile-CD
50 lines (46 loc) · 2.56 KB
/
Jenkinsfile-CD
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
// JENKINS CD PIPELINE
// Here the image stored in Dockerhub will be deployed using K8s
// IMP NOTE: Store your EC2 instance username & key in Jenkins credentials
pipeline {
agent any
// DECLARE THE VARIABLES HERE:
environment {
NODE_IP = '<Node_Server_Private_IP>' // paste your Node-Server private IP here
EC2_NAME = "ec2-user" // enter your AWS EC2 username
PIPELINE_NAME = "CI-PIPELINE" // enter your pipeline name here
PROJECT_NAME = "DevopsProject1" // enter your Devops project name here
}
stages {
stage("1. Pull Files") {
// Copy K8s manifest files from Master-Server workspace to Node-server
steps {
sshagent(['my_ec2_creds']) { // install 'ssh agent' plugin & generate 'ssh-agent' pipeline syntax generator
sh "ssh -o StrictHostKeyChecking=no ${EC2_NAME}@${NODE_IP}" // enter your Node Server's private IP here
sh "scp /var/lib/jenkins/workspace/${PIPELINE_NAME}/${PROJECT_NAME}/deployment.yaml ${EC2_NAME}@${NODE_IP}:/home/ec2-user/"
sh "scp /var/lib/jenkins/workspace/${PIPELINE_NAME}/${PROJECT_NAME}/service.yaml ${EC2_NAME}@${NODE_IP}:/home/ec2-user/"
}
}
}
stage('2. Approval') {
// If 'manual approval' is required, then it is called 'Continuous Delivery'
// If it is totally automated, then it is called 'Continuous Deployment'
steps {
input message: 'Approve deployment?'
}
}
stage("3. Deployment") {
// K8s Deployment using Ansible
// i. Ensure to establish passwordless connection between Master & Node first
// ii. Your deployment.yaml file must contain the Dockerhub image within the container block
steps {
sshagent(['my_ec2_creds']) { // store EC2 username & pem-key in Jenkins credentials
sh "ssh -o StrictHostKeyChecking=no ${EC2_NAME}@${NODE_IP}" // enter your Node-Server private IP here
sh "ssh -o StrictHostKeyChecking=no ${EC2_NAME}@${NODE_IP} kubectl apply -f deployment.yaml"
sh "ssh -o StrictHostKeyChecking=no ${EC2_NAME}@${NODE_IP} kubectl apply -f service.yaml"
sh "ssh -o StrictHostKeyChecking=no ${EC2_NAME}@${NODE_IP} kubectl rollout restart deploy"
sh "ssh -o StrictHostKeyChecking=no ${EC2_NAME}@${NODE_IP} kubectl get service"
}
}
}
}
}