forked from carloshn90/user-api-fargate
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Jenkinsfile
114 lines (101 loc) · 4.62 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/env groovy
pipeline {
agent any
options {
buildDiscarder(logRotator(numToKeepStr: '10'))
disableConcurrentBuilds()
timeout(time: 1, unit: 'HOURS')
timestamps()
}
tools {
jdk 'openjdk-11'
maven 'maven 3.6.3'
dockerTool 'docker-latest'
}
environment {
POM_VERSION = getVersion()
JAR_NAME = getJarName()
AWS_ECR_REGION = 'eu-west-1'
AWS_ECS_SERVICE = 'ch-dev-user-api-service'
AWS_ECS_TASK_DEFINITION = 'ch-dev-user-api-taskdefinition'
AWS_ECS_COMPATIBILITY = 'FARGATE'
AWS_ECS_NETWORK_MODE = 'awsvpc'
AWS_ECS_CPU = '256'
AWS_ECS_MEMORY = '512'
AWS_ECS_CLUSTER = 'ch-dev'
AWS_ECS_TASK_DEFINITION_PATH = './ecs/container-definition-update-image.json'
}
stages {
stage('Build & Test') {
steps {
withMaven(options: [artifactsPublisher(), mavenLinkerPublisher(), dependenciesFingerprintPublisher(disabled: true), jacocoPublisher(disabled: true), junitPublisher(disabled: true)]) {
sh "mvn -B -U clean package"
}
}
}
stage('Build Docker Image') {
steps {
withCredentials([string(credentialsId: 'AWS_REPOSITORY_URL_SECRET', variable: 'AWS_ECR_URL')]) {
script {
docker.build("${AWS_ECR_URL}:${POM_VERSION}", "--build-arg JAR_FILE=${JAR_NAME} .")
}
}
}
}
stage('Push image to ECR') {
steps {
withCredentials([string(credentialsId: 'AWS_REPOSITORY_URL_SECRET', variable: 'AWS_ECR_URL')]) {
withAWS(region: "${AWS_ECR_REGION}", credentials: 'personal-aws-ecr') {
script {
def login = ecrLogin()
sh('#!/bin/sh -e\n' + "${login}") // hide logging
docker.image("${AWS_ECR_URL}:${POM_VERSION}").push()
}
}
}
}
}
stage('Deploy in ECS') {
steps {
withCredentials([string(credentialsId: 'AWS_EXECUTION_ROL_SECRET', variable: 'AWS_ECS_EXECUTION_ROL'),string(credentialsId: 'AWS_REPOSITORY_URL_SECRET', variable: 'AWS_ECR_URL')]) {
script {
updateContainerDefinitionJsonWithImageVersion()
sh("/usr/local/bin/aws ecs register-task-definition --region ${AWS_ECR_REGION} --family ${AWS_ECS_TASK_DEFINITION} --execution-role-arn ${AWS_ECS_EXECUTION_ROL} --requires-compatibilities ${AWS_ECS_COMPATIBILITY} --network-mode ${AWS_ECS_NETWORK_MODE} --cpu ${AWS_ECS_CPU} --memory ${AWS_ECS_MEMORY} --container-definitions file://${AWS_ECS_TASK_DEFINITION_PATH}")
def taskRevision = sh(script: "/usr/local/bin/aws ecs describe-task-definition --task-definition ${AWS_ECS_TASK_DEFINITION} | egrep \"revision\" | tr \"/\" \" \" | awk '{print \$2}' | sed 's/\"\$//'", returnStdout: true)
sh("/usr/local/bin/aws ecs update-service --cluster ${AWS_ECS_CLUSTER} --service ${AWS_ECS_SERVICE} --task-definition ${AWS_ECS_TASK_DEFINITION}:${taskRevision}")
}
}
}
}
}
post {
always {
withCredentials([string(credentialsId: 'AWS_REPOSITORY_URL_SECRET', variable: 'AWS_ECR_URL')]) {
junit allowEmptyResults: true, testResults: 'target/surfire-reports/*.xml'
publishHTML([allowMissing: true, alwaysLinkToLastBuild: false, keepAll: false, reportDir: 'target/site/jacoco-ut/', reportFiles: 'index.html', reportName: 'Unit Testing Coverage', reportTitles: 'Unit Testing Coverage'])
jacoco(execPattern: 'target/jacoco-ut.exec')
deleteDir()
sh "docker rmi ${AWS_ECR_URL}:${POM_VERSION}"
}
}
}
}
def getJarName() {
def jarName = getName() + '-' + getVersion() + '.jar'
echo "jarName: ${jarName}"
return jarName
}
def getVersion() {
def pom = readMavenPom file: './pom.xml'
return pom.version
}
def getName() {
def pom = readMavenPom file: './pom.xml'
return pom.name
}
def updateContainerDefinitionJsonWithImageVersion() {
def containerDefinitionJson = readJSON file: AWS_ECS_TASK_DEFINITION_PATH, returnPojo: true
containerDefinitionJson[0]['image'] = "${AWS_ECR_URL}:${POM_VERSION}".inspect()
echo "task definiton json: ${containerDefinitionJson}"
writeJSON file: AWS_ECS_TASK_DEFINITION_PATH, json: containerDefinitionJson
}