forked from danjac/realworld
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJenkinsfile
More file actions
107 lines (104 loc) · 3.19 KB
/
Jenkinsfile
File metadata and controls
107 lines (104 loc) · 3.19 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
pipeline {
agent {
kubernetes {
yaml '''
apiVersion: v1
kind: Pod
metadata:
labels:
some-label: some-label-value
spec:
containers:
- name: build
image: the9thlime/builder:latest
command:
- cat
tty: true
volumeMounts:
- mountPath: /var/run/docker.sock
name: docker-sock
- name: mysql
image: mysql:latest
tty: true
env:
- name: MYSQL_ROOT_PASSWORD
value: '123'
- name: MYSQL_DATABASE
value: 'test_db'
ports:
- containerPort: 3306
name: mysql
volumes:
- name: docker-sock
hostPath:
path: /var/run/docker.sock
'''
retries 2
}
}
stages {
stage('Wait for MySQL') {
steps {
container('mysql') {
sh '''
echo "Waiting for MySQL to be ready..."
while ! mysqladmin ping -h"127.0.0.1" --silent; do
sleep 1
done
echo "MySQL is up and running!"
'''
}
}
}
stage('Run test') {
steps {
container('build') {
sh '''
echo $DATABASE_NAME
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt
cd django/
python manage.py test
'''
}
}
}
stage('Build docker image') {
steps {
container('build') {
script {
withDockerRegistry([credentialsId: "dockerhub", url: 'https://index.docker.io/v1/']) {
sh 'docker build -t the9thlime/realworldtt:0.0.${BUILD_NUMBER} .'
sh 'docker push the9thlime/realworldtt:0.0.${BUILD_NUMBER}'
sh 'docker tag the9thlime/realworldtt:0.0.${BUILD_NUMBER} the9thlime/realworldtt:latest'
sh 'docker push the9thlime/realworldtt:latest'
}
}
}
}
}
stage('Update Kubernetes Manifest') {
steps {
withCredentials([sshUserPrivateKey(credentialsId: 'github', keyFileVariable: 'KEY')]){
container('build') {
script {
sh """
mkdir -p ~/.ssh && echo -e "Host github.com\n\tStrictHostKeyChecking no\n" > ~/.ssh/config
eval `ssh-agent -s`
trap "ssh-agent -k" EXIT
ssh-add "$KEY"
git config --global user.email "ayushj0909@outlook.com"
git config --global user.name "Ayush Jain"
git clone git@github.com:The9thLime/realworldtt.git
cd realworldtt/k8s/base
sed -i 's/realworldtt:[^ ]*/realworldtt:0.0.${BUILD_NUMBER}/' deployment-main.yml
git add deployment-main.yml && git commit -m "Patched with new image version" && git push
"""
}
}
}
}
}
}
}