-
Notifications
You must be signed in to change notification settings - Fork 2
/
build.gradle
160 lines (135 loc) · 5.39 KB
/
build.gradle
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import com.bmuschko.gradle.docker.tasks.container.*
import com.bmuschko.gradle.docker.tasks.image.*
// NOTE: Project-related configuration properties are listed within .env file.
// The structure contains names of the properties.
def DARZEE_CONFIGURATION_NAMES = [
version: "DARZEE_VER",
port: "DARZEE_PORT",
dockerfilePath: "CREATED_DARZEE_DOCKERFILE_PATH"
]
buildscript {
ext {
// NOTE: Project configurations' actual parameter values.
darzeeConfiguration = [
DARZEE_VERSION: '${getDarzeeConfiurationParameter(${DARZEE_CONFIGURATION_NAMES.version})}',
DARZEE_PORT: '${getDarzeeConfiurationParameter(${DARZEE_CONFIGURATION_NAMES.port})}',
DARZEE_IMAGE_FULL_ID: 'emcmongoose/darzee',
DARZEE_IMAGE_NAME: 'darzee'
]
depVersion = [
gradle_docker_plugin: "3.6.1",
gradle_node_plugin: "1.2.0",
]
}
repositories {
mavenLocal()
mavenCentral()
jcenter()
maven {
url "https://plugins.gradle.org/m2"
}
}
dependencies {
classpath "com.bmuschko:gradle-docker-plugin:${depVersion.gradle_docker_plugin}"
classpath "com.moowork.gradle:gradle-node-plugin:${depVersion.gradle_node_plugin}"
}
}
apply plugin: 'com.bmuschko.docker-remote-api'
apply plugin: 'com.bmuschko.docker-java-application'
apply plugin: 'com.moowork.node'
apply plugin: 'base'
version rootProject.ext.darzeeConfiguration.DARZEE_VERSION
repositories {
mavenLocal()
mavenCentral()
jcenter()
}
node {
version = '8.12.0'
npmVersion = '6.4.1'
download = true
workDir = file("${project.buildDir}/node")
nodeModulesDir = file("${project.projectDir}")
}
task createDockerfile(type: Dockerfile) {
destFile = project.file(getDarzeeConfiurationParameter(DARZEE_CONFIGURATION_NAMES.dockerfilePath))
// NOTE: Stage 1. Building Angular app in builder
from "node:8.11.2-alpine as builder"
instruction "WORKDIR /usr/src/app"
instruction "COPY console/package.json ./"
instruction "RUN npm install"
instruction "COPY ./console ."
instruction "RUN npm run build "
// NOTE: Stage 2 - adding Prometheus into the image
from "prom/prometheus as prometheus"
instruction "COPY --from=builder /usr/src/app/dist/mongoose-console server"
/* NOTE: Stage 3. It contains:
* Serving the app via NodeJS server
* Exposing Prometheus into port 9090
*/
from "node:8.11.2-alpine as server"
instruction "COPY --from=prometheus . prometheus"
instruction "COPY console/server/node-server.js prometheus/prometheus/server/ "
instruction "RUN ls"
instruction "RUN cd prometheus/prometheus/server/ "
instruction "RUN npm install express@'>=3.0.0 <4.0.0' --save"
instruction "RUN npm install body-parser"
instruction "RUN npm install cors"
instruction "RUN npm install shelljs"
instruction "RUN npm install axios"
instruction "RUN cd .."
instruction "WORKDIR /"
instruction "RUN mv /prometheus/etc/prometheus/prometheus.yml /prometheus/prometheus/server/prometheus.yml"
instruction "COPY docker-related-files/entrypoint.sh entrypoint.sh"
instruction "RUN ls"
instruction "COPY .env .env"
instruction "RUN sed -i 's/\r\$//' entrypoint.sh && chmod +x entrypoint.sh"
instruction "EXPOSE 8080 9090"
instruction "ENTRYPOINT sh ./entrypoint.sh"
}
task buildImage(type: DockerBuildImage, dependsOn: 'createDockerfile') {
inputDir = project.file(".")
dockerFile = createDockerfile.destFile
imageId = darzeeConfiguration.DARZEE_IMAGE_ID
tags = [darzeeConfiguration.DARZEE_IMAGE_FULL_ID + ':' + getDarzeeConfiurationParameter(DARZEE_CONFIGURATION_NAMES.version), darzeeConfiguration.DARZEE_IMAGE_FULL_ID + ':testing', darzeeConfiguration.DARZEE_IMAGE_FULL_ID + ':kube']
doLast {
clearProductionFiles()
}
}
task tagImage(type: DockerTagImage, dependsOn: 'buildImage') {
targetImageId { buildImage.getImageId() }
tag darzeeConfiguration.DARZEE_IMAGE_NAME
repository darzeeConfiguration.DARZEE_IMAGE_NAME
}
task pushImage(type: DockerPushImage, dependsOn: 'tagImage'){
imageName darzeeConfiguration.DARZEE_IMAGE_FULL_ID
}
// Leaving "clear" task so it could be configured lately. Remaining clearProductionFiles() as a function in order ...
// ... to call it within other independent tasks.
task clear() {
clearProductionFiles()
}
clean.dependsOn clear
def clearProductionFiles() {
delete 'docker'
}
def getDarzeeConfiurationParameter(final String parameterName) {
// We're storing environment variables within .env file. The image tag ...
// ... (version of Mongoose Console), Console's exposing port, etc. is defined inside of it.
// The function retrives the value of given parameter name from the environment related environment variable.
final ENVIRONMENT_VARIABLES_FILENAME = ".env";
final ENVIRONMENT_VARIABLES_DELIMITER = "\n";
final DELIMITER_OF_VARIABLES_AND_VALUE = "=";
def environmentVariables = file(ENVIRONMENT_VARIABLES_FILENAME).text.split(ENVIRONMENT_VARIABLES_DELIMITER);
def targetParameterValue = "";
environmentVariables.each { environmentVariable ->
if (environmentVariable.contains(parameterName)) {
final versionPositionInStringArray = 1;
targetParameterValue = environmentVariable.split(DELIMITER_OF_VARIABLES_AND_VALUE)[versionPositionInStringArray];
}
}
return targetParameterValue;
}
def getConsoleExposingPort() {
return getDarzeeConfiurationParameter(DARZEE_CONFIGURATION_NAMES.port);
}