-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdockerpush
More file actions
66 lines (65 loc) · 2.77 KB
/
dockerpush
File metadata and controls
66 lines (65 loc) · 2.77 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
def remote = [:]
remote.name = "ubuntu"
remote.allowAnyHosts = true
def ID
def IP
def STATE
node{
withCredentials(
[[
$class: 'AmazonWebServicesCredentialsBinding',
accessKeyVariable: 'AWS_ACCESS_KEY_ID',
credentialsId: 'aws-client', // ID of credentials in Jenkins
secretKeyVariable: 'AWS_SECRET_ACCESS_KEY'
]]) {
stage("create EC2 instance"){
sh 'aws configure set region us-east-2'
ID = sh (script: 'aws ec2 run-instances --image-id ami-05c1fa8df71875112 --count 1 --instance-type t2.micro --key-name KEY_AWS --security-group-ids sg-d77dc5b4 --subnet-id subnet-a9541dd3 --region us-east-2 --query \'Instances[0].InstanceId\'',returnStdout: true)
sh 'sleep 1m'
}
stage("get the EC2 external ip"){
remote.host = sh (script: "aws ec2 describe-instances --query \'Reservations[0].Instances[0].PublicIpAddress\' --instance-ids $ID",returnStdout: true)
}
withCredentials([sshUserPrivateKey(credentialsId: 'KEY_AWS', keyFileVariable: 'identity', passphraseVariable: '', usernameVariable: 'userName')]) {
remote.user = userName
remote.identityFile = identity
stage("install dokcer.io") {
sh 'sudo apt-get update'
sh 'sudo apt-get install docker.io -y'
sh 'sudo systemctl start docker'
sh 'sudo systemctl enable docker'
}
stage("install openjdk"){
sh 'sudo apt-get update'
sh 'sudo apt-get install openjdk-8-jdk -y'
}
stage("install maven"){
sh 'sudo apt-get update'
sh 'sudo apt-get install maven -y'
}
stage('Checkout') {
echo 'Checkout-ing project'
git 'https://github.com/islajd/test.git'
echo 'Checkout Success!'
}
stage('Build Artifact') {
sh "mvn clean install"
}
stage('Create artifact copy') {
sh 'cp target/demo-*.jar target/demo.jar'
}
stage('Create Docker Image') {
sh 'sudo docker build -t islajd/test:prove .'
}
stage("push image to docker hub"){
withCredentials([usernamePassword(credentialsId: 'docker-acc', passwordVariable: 'dockerHubPassword', usernameVariable: 'dockerHubUser')]) {
sh "sudo docker login -u ${env.dockerHubUser} -p ${env.dockerHubPassword}"
sh 'sudo docker push islajd/test:prove'
}
}
}
stage("terminate EC2 instance"){
sh "aws ec2 terminate-instances --instance-ids $ID"
}
}
}