-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJenkinsfile
More file actions
109 lines (104 loc) · 4.02 KB
/
Jenkinsfile
File metadata and controls
109 lines (104 loc) · 4.02 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
def ip_address
pipeline {
agent any
stages {
stage('git'){
steps {
git 'https://github.com/rishabh-28/SpringApplication.git'
}
}
stage('Mavenn'){
steps {
withMaven(maven:'mvn3'){
sh 'mvn clean'
sh 'mvn compile'
sh 'mvn package'
}
}
}
stage('SonarQube Analysis') {
steps {
withMaven(maven:'mvn3'){
withSonarQubeEnv('sonar') {
sh "$MAVEN_HOME/bin/mvn sonar:sonar"
}
}
}
}
stage('build docker image'){
steps {
sh 'docker build -t rrishabhbansal96/spring:1.0.0 .'
}
}
stage('Push docker image') {
steps {
withCredentials([string(credentialsId: 'docker-user', variable: 'dockerPwd')]){
sh "docker login -u rrishabhbansal96 -p ${dockerPwd}"
}
sh 'docker push rrishabhbansal96/spring:1.0.0'
}
}
stage('AWS'){
steps {
withCredentials([[$class: 'AmazonWebServicesCredentialsBinding', accessKeyVariable: 'AWS_ACCESS_KEY_ID', credentialsId: 'aws-user', secretKeyVariable: 'AWS_SECRET_ACCESS_KEY']]) {
sh 'ansible-playbook create-ec2.yml'
}
}
}
stage('Capture IP address') {
steps {
withCredentials([[$class: 'AmazonWebServicesCredentialsBinding', accessKeyVariable: 'AWS_ACCESS_KEY_ID', credentialsId: 'aws-user', secretKeyVariable: 'AWS_SECRET_ACCESS_KEY']]) {
script{
def get_ip='aws ec2 describe-instances --filters "Name=tag:Name,Values=ansible" --query "Reservations[*].Instances[*].PublicIpAddress" --output text --region us-west-2'
def output = sh script :"${get_ip}",returnStdout:true
ip_address = output
}
}
}
}
stage('Install Docker'){
steps {
script{
print ip_address
def dockerCMD = 'sudo yum install -y docker; sudo systemctl start docker; sudo systemctl enable docker; sudo chmod 777 /var/run/docker.sock'
sshagent(credentials: ['ssh-key']) {
sh "ssh -o StrictHostKeyChecking=no ec2-user@${ip_address} ${dockerCMD}"
}
}
}
}
stage('Pull Docker Container'){
steps {
script{
print ip_address
def dockerCMD = 'sudo docker pull rrishabhbansal96/spring:1.0.0'
sshagent(credentials: ['ssh-key']) {
sh "ssh -o StrictHostKeyChecking=no ec2-user@${ip_address} ${dockerCMD}"
}
}
}
}
stage('Run Docker Container'){
steps {
script{
print ip_address
def dockerCMD = 'sudo docker run -d -p 80:8885 rrishabhbansal96/spring:1.0.0'
sshagent(credentials: ['ssh-key']) {
sh "ssh -o StrictHostKeyChecking=no ec2-user@${ip_address} ${dockerCMD}"
}
}
}
}
}
post {
always {
echo 'One way or another, I have finished'
deleteDir() /* clean up our workspace */
//mail to: 'rrishabhbansal96@gmail.com', from: 'mywebsite2810@gmail.com', subject: "Complete Pipeline: ${currentBuild.fullDisplayName}", body: "Something is wrong with ${env.BUILD_URL}"
}
failure {
echo 'Failure!'
mail to: 'rrishabhbansal96@gmail.com', from: 'mywebsite2810@gmail.com', subject: "Failed Pipeline: ${currentBuild.fullDisplayName}", body: "Something is wrong with ${env.BUILD_URL}"
}
}
}