-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.groovy
More file actions
77 lines (63 loc) · 2.5 KB
/
Copy pathscript.groovy
File metadata and controls
77 lines (63 loc) · 2.5 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
def checkout() {
scmSkip(deleteBuild: false, skipPattern:'.*\\[ci skip\\].*')
}
def incrementVersion() {
echo 'incrementing app version...'
sh 'mvn build-helper:parse-version versions:set \
-DnewVersion=\\\${parsedVersion.majorVersion}.\\\${parsedVersion.minorVersion}.\\\${parsedVersion.nextIncrementalVersion} \
versions:commit'
def matcher = readFile('pom.xml') =~ '<version>(.+)</version>'
def version = matcher[0][1]
env.IMAGE_NAME = "$version-$BUILD_NUMBER"
}
def buildJar() {
echo "building the application..."
sh 'mvn clean package'
}
def buildImage() {
echo "building the docker image..."
withCredentials([usernamePassword(credentialsId: 'docker-hub', passwordVariable: 'PASS', usernameVariable: 'USER')]) {
sh "docker build -t farnsworth1988/java-maven-app:${IMAGE_NAME} ."
sh 'echo $PASS| docker login -u $USER --password-stdin'
sh "docker push farnsworth1988/java-maven-app:${IMAGE_NAME}"
}
}
def provisionServer() {
dir('terraform') {
sh "terraform init"
sh "terraform apply --auto-approve"
EC2_PUBLIC_IP = sh(
script: "terraform output ec2_public_ip",
returnStdout: true
).trim()
}
}
def deployApp() {
echo "waiting for EC2 server to initialize"
sleep(time: 60, unit: "SECONDS")
echo "deploying docker image to EC2"
echo "${EC2_PUBLIC_IP}"
def shellCmd = 'bash /home/ubuntu/server-cmds.sh ${IMAGE_NAME} $DOCKER_CREDS_USR $DOCKER_CREDS_PSW'
def ec2Instance = "ubuntu@${EC2_PUBLIC_IP}"
sshagent(['Ubuntu']) {
sh "scp -o StrictHostKeyChecking=no docker-compose.yaml ${ec2Instance}:/home/ubuntu"
sh "scp -o StrictHostKeyChecking=no server-cmds.sh ${ec2Instance}:/home/ubuntu"
sh "ssh -o StrictHostKeyChecking=no ${ec2Instance} ${shellCmd}"
}
}
def commitUpdateVersion(){
withCredentials([usernamePassword(credentialsId: 'gitlab-credentials', passwordVariable: 'PASS', usernameVariable: 'USER')]){
/*sh 'git config --global user.email "nevero.anton@gmail.com"'
sh 'git config --global user.name "Jenkins"'*/
sh 'git remote set-url origin https://$USER:$PASS@gitlab.com/nevero.anton/my-java-maven-app.git'
sh 'git add .'
sh 'git commit -m "ci: version bump [ci skip]"'
sh 'git push origin HEAD:main'
}
}
def destroy(){
dir('terraform') {
sh "terraform destroy --auto-approve"
}
}
return this