-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJenkinsfile
More file actions
81 lines (73 loc) · 2.15 KB
/
Jenkinsfile
File metadata and controls
81 lines (73 loc) · 2.15 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
pipeline {
agent any
environment {
DOCKER_CREDS = credentials('docker-hub-credentials')
IMAGE_NAME = "kwamecody172/prodev-backend"
TAG = "${env.BUILD_NUMBER}"
}
stages {
stage('Checkout') {
steps {
checkout scm
}
}
stage('Setup Environment') {
steps {
sh 'echo "Setting up environment..."'
sh 'touch infra/docker/.env'
}
}
stage('Test') {
steps {
script {
try {
sh 'docker compose -f infra/docker/docker-compose.yml up -d db redis'
sh 'sleep 10'
sh 'docker compose -f infra/docker/docker-compose.yml run web python src/manage.py test src'
} finally {
sh 'docker compose -f infra/docker/docker-compose.yml down -v'
}
}
}
}
stage('Build Docker Image') {
steps {
script {
sh "docker build -t ${IMAGE_NAME}:${TAG} -t ${IMAGE_NAME}:latest -f infra/docker/Dockerfile ."
}
}
}
stage('Push to Docker Hub') {
when {
branch 'main'
}
steps {
script {
sh "echo ${DOCKER_CREDS_PSW} | docker login -u ${DOCKER_CREDS_USR} --password-stdin"
sh "docker push ${IMAGE_NAME}:${TAG}"
sh "docker push ${IMAGE_NAME}:latest"
}
}
}
stage('Deploy to K8s') {
when {
branch 'main'
}
steps {
sh "kubectl set image deployment/web web=${IMAGE_NAME}:${TAG}"
sh "kubectl set image deployment/celery-worker worker=${IMAGE_NAME}:${TAG}"
}
}
}
post {
always {
cleanWs()
}
success {
echo 'Pipeline succeeded!'
}
failure {
echo 'Pipeline failed.'
}
}
}