-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJenkinsfile
More file actions
114 lines (107 loc) · 4.04 KB
/
Jenkinsfile
File metadata and controls
114 lines (107 loc) · 4.04 KB
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
pipeline {
agent any
environment {
REGISTRY = 'deveshksh' // Docker Hub username
REGISTRY_CREDENTIALS = 'docker-hub-credentials' // Docker Hub credentials ID in Jenkins
PATH = "/opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin" // Updated PATH
}
stages {
stage('Test Environment') {
steps {
sh 'echo $PATH'
sh 'which dotnet'
sh 'dotnet --version'
}
}
stage('Checkout') {
steps {
git url: 'https://github.com/deveshksh/devopsProject.git', branch: 'master', credentialsId: 'github-credentials'
}
}
stage('Build and Test') {
steps {
echo "Building and testing the code..."
sh 'dotnet build store.sln'
sh 'dotnet test tests/CartMicroservice.UnitTests/CartMicroservice.UnitTests.csproj'
sh 'dotnet test tests/CatalogMicroservice.UnitTests/CatalogMicroservice.UnitTests.csproj'
sh 'dotnet test tests/IdentityMicroservice.UnitTests/IdentityMicroservice.UnitTests.csproj'
}
}
stage('Build and Push Docker Images') {
parallel {
stage('Catalog Microservice') {
steps {
script {
dockerBuildAndPush('catalog-microservice', 'src/microservices/CatalogMicroservice')
}
}
}
stage('Cart Microservice') {
steps {
script {
dockerBuildAndPush('cart-microservice', 'src/microservices/CartMicroservice')
}
}
}
stage('Identity Microservice') {
steps {
script {
dockerBuildAndPush('identity-microservice', 'src/microservices/IdentityMicroservice')
}
}
}
stage('Frontend Gateway') {
steps {
script {
dockerBuildAndPush('frontend-gateway', 'src/gateways/FrontendGateway')
}
}
}
stage('Backend Gateway') {
steps {
script {
dockerBuildAndPush('backend-gateway', 'src/gateways/BackendGateway')
}
}
}
stage('Frontend') {
steps {
script {
dockerBuildAndPush('frontend-microservice', 'src/uis/Frontend')
}
}
}
stage('Backend') {
steps {
script {
dockerBuildAndPush('backend-microservice', 'src/uis/Backend')
}
}
}
}
}
stage('Deploy') {
steps {
echo "Deploying microservices..."
sh '''
docker-compose down
docker-compose up -d
'''
}
}
}
}
def dockerBuildAndPush(serviceName, contextDir) {
def imageName = "${env.REGISTRY}/${serviceName}:latest"
withEnv(["IMAGE_NAME=${imageName}", "CONTEXT_DIR=${contextDir}"]) {
withCredentials([usernamePassword(credentialsId: env.REGISTRY_CREDENTIALS,
usernameVariable: 'DOCKER_USERNAME',
passwordVariable: 'DOCKER_PASSWORD')]) {
sh '''
echo "$DOCKER_PASSWORD" | docker login -u "$DOCKER_USERNAME" --password-stdin
docker build -t "$IMAGE_NAME" -f "$CONTEXT_DIR/Dockerfile" .
docker push "$IMAGE_NAME"
'''
}
}
}