Repository navigation
Expand file tree
/
Copy pathJenkinsfile
More file actions
116 lines (106 loc) · 2.42 KB
/
Copy pathJenkinsfile
File metadata and controls
116 lines (106 loc) · 2.42 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
115
116
pipeline {
agent any
environment {
DOCKER_CREDS = credentials('docker-hub-credentials')
}
stages {
stage ('Build') {
agent any
when {
branch 'main'
}
steps {
dir('backend') {
sh '''#!/bin/bash
python3.9 -m venv venv
source venv/bin/activate
pip install -r requirements.txt
'''
}
}
}
stage ('Test') {
agent any
when {
branch 'main'
}
environment {
DJANGO_TEST_ENV = 'true'
}
steps {
dir('backend') {
sh '''#!/bin/bash
source venv/bin/activate
pip install pytest-django
python manage.py makemigrations
python manage.py migrate
pytest account/tests.py --verbose --junit-xml test-reports/results.xml
'''
}
}
}
stage('Cleanup') {
agent { label 'build-node' }
when {
branch 'main'
}
steps {
sh '''
# Only clean Docker system
docker system prune -f
# Safer git clean that preserves terraform state
git clean -ffdx -e "*.tfstate*" -e ".terraform/*"
'''
}
}
stage('Build & Push Images') {
agent { label 'build-node' }
when {
branch 'main'
}
steps {
sh 'echo ${DOCKER_CREDS_PSW} | docker login -u ${DOCKER_CREDS_USR} --password-stdin'
// Build and push backend
sh '''
docker build -t postig0x/ecomm_back:latest -f Dockerfile.backend .
docker push postig0x/ecomm_back:latest
'''
// Build and push frontend
sh '''
docker build -t postig0x/ecomm_front:latest -f Dockerfile.frontend .
docker push postig0x/ecomm_front:latest
'''
}
}
stage('Infrastructure') {
agent { label 'build-node' }
when {
branch 'main'
}
steps {
dir('Terraform') {
sh '''
terraform init
terraform apply -auto-approve \
-var="docker_user=${DOCKER_CREDS_USR}" \
-var="docker_pass=${DOCKER_CREDS_PSW}"
'''
}
}
}
}
post {
always {
agent { label 'build-node' }
when {
branch 'main'
}
steps {
sh '''
docker logout
docker system prune -f
'''
}
}
}
}