forked from schoolofdevops/vote
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathJenkinsfile.kube
106 lines (96 loc) · 3.25 KB
/
Jenkinsfile.kube
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
pipeline {
agent {
kubernetes {
label 'python-buildkit-agent'
defaultContainer 'jnlp'
yaml """
apiVersion: v1
kind: Pod
spec:
containers:
- name: python
image: python:alpine3.17
command:
- cat
tty: true
securityContext:
runAsUser: 0 # Run container as root
- name: buildkit
image: moby/buildkit:latest
securityContext:
privileged: true # Run BuildKit container in privileged mode
env:
- name: DOCKER_BUILDKIT
value: "1"
volumeMounts:
- name: buildkit-cache
mountPath: /var/lib/buildkit
volumes:
- name: buildkit-cache
emptyDir: {}
"""
}
}
environment {
DOCKER_CREDS = credentials('dockerlogin')
}
stages {
stage('Build App') {
steps {
container('python') {
echo 'Compiling vote app'
// Add any additional build commands if required
sh 'pip install -r requirements.txt'
}
}
}
stage('Test App') {
steps {
container('python') {
echo 'Running Unit Tests on vote app'
// Run unit tests here, placeholder for 'nosetests' or any test framework you use
sh 'pip install -r requirements.txt'
sh 'nosetests -v || true' // Replace with the correct test command
}
}
}
stage('Docker Build and Push with BuildKit') {
when {
branch "main"
}
steps {
container('buildkit') {
script {
// Print debug info for commit hash
echo "GIT_COMMIT: ${env.GIT_COMMIT}"
// Ensure commitHash is non-empty and fallback to 'latest' if necessary
def commitHash = env.GIT_COMMIT?.take(7) ?: 'latest'
// Ensure that the commitHash is valid and remove any extra colons
if (!commitHash) {
error("Commit hash is empty or invalid. Falling back to 'latest'.")
}
// Print debug info for commitHash
echo "Using image tag: ${commitHash}"
// Create a Docker config file with credentials
sh '''
mkdir -p /root/.docker
echo '{ "auths": { "https://index.docker.io/v1/": { "auth": "'$(echo -n $DOCKER_CREDS_USR:$DOCKER_CREDS_PSW | base64)'" } } }' > /root/.docker/config.json
'''
// Run BuildKit build and push command with valid tag format
sh """
buildctl build --frontend dockerfile.v0 \
--local context=. \
--local dockerfile=. \
--output type=image,name=docker.io/xxxxxx/vote:${commitHash},push=true
"""
}
}
}
}
}
post {
always {
echo 'CI Pipeline for vote is complete.'
}
}
}