forked from ecsworkshop2018/odin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
49 lines (43 loc) · 1.54 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
pipeline {
agent any
environment {
USER_NAME = "vikram"
SERVICE_NAME = "odin"
AWS_REGION = "us-east-1"
REPO_NAME = "${USER_NAME}/${SERVICE_NAME}"
ECR_REPO_URI = "738035286324.dkr.ecr.${AWS_REGION}.amazonaws.com/${REPO_NAME}"
}
stages {
stage("build-and-test") {
steps {
sh 'mvn clean install'
}
}
stage ('build-docker-image-and-push-to-registry') {
steps {
script {
GIT_SHA = sh(returnStdout: true, script: 'git rev-parse HEAD').trim()
buildDockerImageAndPushToECR(GIT_SHA, REPO_NAME, AWS_REGION, ECR_REPO_URI)
}
}
}
}
}
def buildDockerImageAndPushToECR(imageTag, repoName, region, repoUri) {
ensureDockerRepoExists(repoName, region)
sh "docker build . -t ${repoName}:${imageTag} --build-arg JAR_FILE=target/ecsworkshop.service1-1.0-SNAPSHOT.jar"
sh "docker tag ${repoName}:${imageTag} ${repoUri}:${imageTag}"
sh "\$(aws ecr get-login --no-include-email --region ${region})"
sh "docker push ${repoUri}:${imageTag}"
}
def ensureDockerRepoExists(repoName, region) {
script {
sh "\$(aws ecr get-login --no-include-email --region ${region})"
try {
sh "echo 'Ensure that ECR repository exists: ${repoName}'"
sh "aws ecr create-repository --repository-name ${repoName} --region ${region}"
} catch (e) {
/* ignore if repository already exists. */
}
}
}