forked from KubeDev/conversao-temperatura
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile
103 lines (99 loc) · 3.47 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
pipeline {
agent any
environment {
PROJECT_NAME = "conversao-temperatura"
REPOSITORY = "gabriel2012rissi/${env.PROJECT_NAME}"
VERSION = "v1.0.0"
}
stages {
stage('Checkout') {
steps {
script {
// Obtendo o hash do commit atual
COMMIT = "${GIT_COMMIT.substring(0,8)}"
if ("${env.BRANCH_NAME}" == "master") {
IMAGE_TAG = "${env.VERSION}"
} else {
IMAGE_TAG = "${env.BRANCH_NAME}"
}
}
}
}
stage('Static Analysis') {
environment {
sonarHome = tool 'SONAR_SCANNER'
}
steps {
// SonarQube Scanner
sh """
${sonarHome}/bin/sonar-scanner -e \
-Dsonar.projectKey=${env.PROJECT_NAME} \
-Dsonar.branch.name=${env.BRANCH_NAME} \
-Dsonar.sources=. \
-Dsonar.host.url=http://sonarqube:9000 \
-Dsonar.login=275ebb556ebdcf5f6ce6ad67e2272a8e6ada4faf
"""
}
}
stage('Build Application') {
steps {
script {
appImage = docker.build("${env.REPOSITORY}:${COMMIT}", "./src")
}
}
}
stage('Run Application') {
steps {
script {
// Criando a rede 'jenkins_test'
sh "docker network create jenkins_test-${env.BUILD_NUMBER} || true"
// Criando o container 'api'
sh """
docker run \
--detach \
--name app-${env.BUILD_NUMBER} \
--publish 8080:8080 \
--network jenkins_test-${env.BUILD_NUMBER} \
--network-alias app \
${appImage.id}
"""
}
}
post {
always {
echo 'Cleanning up...'
// Removendo o container 'api'
sh "docker rm -f app-${env.BUILD_NUMBER} || true"
sleep 10
// Removendo a rede 'jenkins_test'
sh "docker network rm jenkins_test-${env.BUILD_NUMBER} || true"
}
}
}
stage('Push to Docker Hub') {
steps {
script {
// Enviar as imagens para o repositório do Docker Hub
docker.withRegistry("https://registry.hub.docker.com", "dockerhub") {
if ("${env.BRANCH_NAME}" == "master") {
appImage.push("latest")
appImage.push("${VERSION}")
} else {
appImage.push("${IMAGE_TAG}")
}
}
}
}
}
stage('Trigger Manifest Update') {
steps {
script {
echo 'Triggering Manifest Update...'
build job: 'conversao-temperatura-manifest-update', parameters: [
string(name: "DOCKER_IMAGE", value: "${env.REPOSITORY}:${IMAGE_TAG}")
]
}
}
}
}
}