-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
104 lines (94 loc) · 3.86 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
stages {
stage('Pre-check Docker') {
steps {
script {
try {
// Check if Docker is installed
def dockerVersion = sh(script: 'docker --version', returnStdout: true).trim()
if (!dockerVersion) {
error "Docker is not installed or not in the PATH. Please install Docker."
}
// Check if Docker daemon is running
def dockerInfo = sh(script: 'docker info', returnStatus: true)
if (dockerInfo != 0) {
error "Docker daemon is not running. Please start the Docker service."
}
echo "Docker is available and running: ${dockerVersion}"
} catch (Exception e) {
error "Pre-check failed: ${e.message}"
}
}
}
}
stage('Checkout Repository') {
steps {
// Jenkins will automatically clone the repository with the Git SCM plugin
// The repository is available under the $WORKSPACE directory
echo "Repository checked out to: ${env.WORKSPACE}"
}
}
stage('Build Docker Image') {
steps {
// Assuming you have a Dockerfile in your repository
sh 'docker build -t delivery_metrics .'
}
}
stage('Run Application') {
steps {
// Run your application in a Docker container
sh '''
# Stop and remove existing container if it exists
docker rm -f delivery_metrics || true
# Run new container
docker run -d -p 8005:8005 --name delivery_metrics delivery_metrics
'''
}
}
stage('Run Prometheus & Grafana') {
steps {
sh '''
# Stop and remove existing Prometheus container if it exists
docker rm -f prometheus || true
# Run new Prometheus container
docker run -d --name prometheus -p 9095:9095 \
-v /var/lib/jenkins/workspace/Grafana/prometheus.yml:/etc/prometheus/prometheus.yml \
-v /var/lib/jenkins/workspace/Grafana/alert_rules.yml:/etc/prometheus/alert_rules.yml \
prom/prometheus
# Stop and remove existing Grafana container if it exists
docker rm -f grafana || true
# Run new Grafana container
docker run -d --name grafana -p 3000:3000 grafana/grafana
'''
}
}
stage('Cleanup') {
steps {
script {
// Stop and remove containers if they exist
sh '''
docker rm -f prometheus || true
docker rm -f grafana || true
docker rm -f delivery_metrics || true
# Optionally remove unused images
docker image prune -f
# Optionally remove unused volumes
docker volume prune -f
'''
}
}
}
}
// Post-build actions
post {
always {
// This will run even if the pipeline fails
sh '''
docker rm -f prometheus || true
docker rm -f grafana || true
docker rm -f delivery_metrics || true
'''
}
}
}