-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
89 lines (83 loc) · 3.63 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
pipeline {
agent any
environment {
KUBECONFIG = credentials('k8sconfig') // замените 'kubeconfig-id' на ваш ID секрета
VENV_DIR = 'venv' // Директория для виртуального окружения
}
stages {
stage("Checkout repo") { //Объявляем секцию с именем «Checkout repo»
steps {
checkout scmGit(branches: [[name: '${BRANCH_NAME}']],
userRemoteConfigs: [[credentialsId: '1', url: 'https://gitlab-pub.yadro.com/devops-school-2024/student/a.ukhanov.git']])
}
}
stage('Setup Python Environment') {
steps {
// Создание виртуального окружения
sh 'python3 -m venv $VENV_DIR'
// Активация виртуального окружения и установка зависимостей
sh """
. $VENV_DIR/bin/activate
pip install --upgrade pip
pip install -r requirements.txt
"""
}
}
stage('Run Tests') {
steps {
// Активация виртуального окружения и запуск тестов
sh """
. $VENV_DIR/bin/activate
pytest --junitxml=test-results.xml
"""
}
}
stage('Build docker app image') {
steps {
withCredentials([usernamePassword(credentialsId: '4', passwordVariable: 'HUB_PASSWORD',
usernameVariable: 'HUB_USERNAME')]) {
sh("buildah bud --tag ${HUB_USERNAME}/currency_app:latest -f ./Dockerfile .")
}
}
}
stage('Push docker image to docker hub') {
steps {
withCredentials([usernamePassword(credentialsId: '4', passwordVariable: 'HUB_PASSWORD',
usernameVariable: 'HUB_USERNAME')]) {
sh """
buildah login -u ${HUB_USERNAME} -p ${HUB_PASSWORD} docker.io
buildah push ${HUB_USERNAME}/currency_app:latest
buildah logout docker.io
"""
}
}
}
stage('Deploy to k8s') {
steps {
withCredentials([file(credentialsId: 'k8sconfig', variable: 'KUBECONFIG')]) {
script {
def serviceExists = sh(script: 'kubectl get service --selector=app=currency-app', returnStatus: true) == 0
if (serviceExists) {
echo 'Service exists, deleting...'
sh 'kubectl delete service --selector=app=currency-app'
}
def podExists = sh(script: 'kubectl get pod --selector=app=currency-app', returnStatus: true) == 0
if (podExists) {
echo 'Pod exists, deleting...'
sh 'kubectl delete pod --selector=app=currency-app'
}
sh 'kubectl apply -f ci_cd/service.yaml'
sh 'kubectl apply -f ci_cd/deployment.yaml'
}
}
}
}
}
post {
always {
// Архивируем артефакты тестов и публикуем их
archiveArtifacts artifacts: 'test-results.xml', allowEmptyArchive: true
junit 'test-results.xml'
}
}
}