-
Notifications
You must be signed in to change notification settings - Fork 1
/
Jenkinsfile
121 lines (105 loc) · 4.67 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
115
116
117
118
119
120
121
pipeline {
agent any
tools {
nodejs "nodejs"
terraform "terraform"
}
stages {
stage('Checkout') {
steps {
git 'https://github.com/inesachour/devops.git'
}
}
stage('Build Backend') {
steps {
script {
sh 'cd server && npm install'
}
}
}
stage('Build Frontend') {
steps {
script {
sh 'cd client && npm install'
}
}
}
stage('Run Tests') {
steps {
script {
sh 'cd server && npm test'
}
}
}
stage('Dockerize') {
steps {
// Build and push backend Docker image
script {
withCredentials([usernamePassword(credentialsId: 'dockerhub_credentials', usernameVariable: 'DOCKER_USERNAME', passwordVariable: 'DOCKER_PASSWORD')]) {
sh 'cd server && docker build -t backend-image .'
sh "docker login -u $DOCKER_USERNAME -p $DOCKER_PASSWORD docker.io"
sh 'docker tag backend-image inesachour/backend-image:latest'
sh 'docker push inesachour/backend-image:latest'
}
}
// Build and push frontend Docker image
script {
withCredentials([usernamePassword(credentialsId: 'dockerhub_credentials', usernameVariable: 'DOCKER_USERNAME', passwordVariable: 'DOCKER_PASSWORD')]) {
sh 'cd client && docker build -t frontend-image .'
sh "docker login -u $DOCKER_USERNAME -p $DOCKER_PASSWORD docker.io"
sh 'docker tag frontend-image inesachour/frontend-image:latest'
sh 'docker push inesachour/frontend-image:latest'
}
}
}
}
/*stage('Login to Azure with AzureServicePrincipal') {
steps {
script {
withCredentials([azureServicePrincipal(credentialsId: 'azure_service_principal', passwordVariable: 'AZURE_CLIENT_SECRET', usernameVariable: 'AZURE_CLIENT_ID')]) {
sh 'az login --service-principal -u $AZURE_CLIENT_ID -p $AZURE_CLIENT_SECRET -t $AZURE_TENANT_ID'
sh 'az account set -s $AZURE_SUBSCRIPTION_ID'
sh 'terraform init'
}
}
}
}*/
stage('Terraform') {
steps {
script {
withCredentials([usernamePassword(credentialsId: 'azure_credentials',passwordVariable: 'AZURE_CLIENT_SECRET', usernameVariable: 'AZURE_CLIENT_ID')]) {
sh 'az login -u $AZURE_CLIENT_ID -p $AZURE_CLIENT_SECRET'
sh 'terraform init'
sh 'terraform plan'
sh 'terraform apply --auto-approve'
}
}
}
}
stage('Deploy to Kubernetes - Backend') {
steps {
script {
withCredentials([azureServicePrincipal(credentialsId: 'azure_service_principal', passwordVariable: 'AZURE_CLIENT_SECRET', usernameVariable: 'AZURE_CLIENT_ID')]) {
//sh 'az aks install-cli'
sh 'az aks get-credentials --resource-group "devops_project_rg" --name "terraform-aks" --overwrite-existing'
sh 'kubectl apply -f backend-deployment.yaml'
sh 'kubectl apply -f backend-service.yaml'
}
}
}
}
stage('Deploy to Kubernetes - Frontend') {
steps {
script {
withCredentials([azureServicePrincipal(credentialsId: 'azure_service_principal', passwordVariable: 'AZURE_CLIENT_SECRET', usernameVariable: 'AZURE_CLIENT_ID')]) {
//sh 'az aks install-cli'
sh 'az aks get-credentials --resource-group "devops_project_rg" --name "terraform-aks" --overwrite-existing'
//sh 'kubectl delete service frontend && kubectl delete deployment frontend'
sh 'kubectl apply -f frontend-deployment.yaml'
sh 'kubectl apply -f frontend-service.yaml'
}
}
}
}
}
}