-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathJenkinsfile
89 lines (80 loc) · 3.25 KB
/
Jenkinsfile
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
#!/usr/bin/env groovy
pipeline {
agent any
tools {
maven 'Maven'
}
environment {
//need to adjust with actual variable value
ECR_REPO_URL = '1234567891.dkr.ecr.us-east-1.amazonaws.com'
ECR_APP_NAME = 'jenkins-aws-java-maven-app'
SERVER_INSTANCE_IP = '000.000.00.00'
SERVER_INSTANCE_USER = 'ubuntu'
GIT_REPO_URL = 'github.com/user/repo-name.git'
IMAGE_REPO = "$ECR_REPO_URL/$ECR_APP_NAME"
}
stages {
stage('increment version') {
steps {
script {
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"
echo "############ ${IMAGE_REPO}"
}
}
}
stage('build app') {
steps {
script {
echo 'building the application...'
sh 'mvn clean package'
}
}
}
stage('build image') {
steps {
script {
echo 'building the docker image...'
withCredentials([usernamePassword(credentialsId: 'aws-ecr', passwordVariable: 'PASS', usernameVariable: 'USER')]) {
sh "docker build -t ${IMAGE_REPO}:${IMAGE_NAME} ."
sh "echo $PASS | docker login -u $USER --password-stdin ${ECR_REPO_URL}"
sh "docker push ${IMAGE_REPO}:${IMAGE_NAME}"
}
}
}
}
stage('Deploy') {
steps {
script {
// def ec2Instance = '[email protected]'
def ec2Instance = "$SERVER_INSTANCE_USER@$SERVER_INSTANCE_IP"
def deployCmd = "bash ./web-deploy.sh ${IMAGE_NAME}"
sshagent(['web-server-key']) {
sh "scp -o StrictHostKeyChecking=no web-deploy.sh ${ec2Instance}:/home/ubuntu"
sh "scp -o StrictHostKeyChecking=no docker-compose.yaml ${ec2Instance}:/home/ubuntu"
sh "ssh -o StrictHostKeyChecking=no ${ec2Instance} ${deployCmd}"
}
}
}
}
stage('commit version update') {
steps {
script {
withCredentials([string(credentialsId: 'GithubTokenSimple', variable: 'GITHUB_TOKEN')]) {
sh 'git config user.email "[email protected]"'
sh 'git config user.name "Jenkins"'
sh "git remote set-url origin https://${GITHUB_TOKEN}@$GIT_REPO_URL"
sh 'git add .'
sh 'git commit -m "ci: version bump"'
sh 'git push origin HEAD:main'
}
}
}
}
}
}