forked from ONSdigital/onse-lab-intro-to-kubernetes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile
110 lines (94 loc) · 3.05 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// VARIABLES
def github_id = 'anwilkie'
// vvv DO NOT EDIT THE VARIABLES BETWEEN THESE MARKERS vvv //
def git_commit = ''
def git_repository = "https://github.com/${github_id}/onse-lab-intro-to-kubernetes"
def image_name = "onseshared/${github_id}-onse-lab-intro-to-kubernetes"
def kaniko_image = 'gcr.io/kaniko-project/executor:debug-539ddefcae3fd6b411a95982a830d987f4214251'
def kubectl_image = 'aklearning/onse-eks-kubectl-deployer:0.0.1'
def label = "build-${UUID.randomUUID().toString()}"
def namespace = "${github_id}"
def pod_yaml = """
kind: Pod
metadata:
name: build-pod
spec:
containers:
- name: kaniko
image: ${kaniko_image}
imagePullPolicy: Always
command:
- /busybox/cat
tty: true
volumeMounts:
- name: jenkins-docker-cfg
mountPath: /root/.docker
- name: kubectl
image: ${kubectl_image}
imagePullPolicy: Always
tty: true
- name: python-test
image: python:alpine3.7
tty: true
volumes:
- name: jenkins-docker-cfg
projected:
sources:
- secret:
name: regcred
items:
- key: dockerconfigjson
path: config.json
"""
// ^^^ DO NOT EDIT THE VARIABLES BETWEEN THESE MARKERS ^^^ //
// POD TEMPLATE AND BUILD STAGES
podTemplate(name: 'kaniko', label: label, yaml: pod_yaml) {
node(label) {
// PULL GIT REPOSITORY
git git_repository
// TEST STAGE
stage('Test') {
container(name: 'python-test', shell: '/bin/sh') {
sh 'pip install pipenv'
sh 'pipenv install --dev'
sh 'pipenv run python -m pytest'
}
}
// DOCKER IMAGE BUILD STAGE
stage('Build Docker image with Kaniko') {
git_commit = sh (
script: 'git rev-parse HEAD',
returnStdout: true
).trim()
image_name += ":${git_commit}"
echo "Building image ${image_name}"
container(name: 'kaniko', shell: '/busybox/sh') {
withEnv(['PATH+EXTRA=/busybox:/kaniko']) {
sh """#!/busybox/sh
/kaniko/executor -f `pwd`/Dockerfile -c `pwd` --skip-tls-verify --cache=true --destination=${image_name}
"""
}
}
}
// DEPLOY CODE TO KUBERNETES STAGE
stage('Deploy to Kubernetes') {
withCredentials([
string(credentialsId: 'AWS_ACCESS_KEY_ID', variable: 'AWS_ACCESS_KEY_ID'),
string(credentialsId: 'AWS_SECRET_ACCESS_KEY', variable: 'AWS_SECRET_ACCESS_KEY'),
string(credentialsId: 'KUBERNETES_SERVER', variable: 'KUBERNETES_SERVER'),
file(credentialsId: 'KUBERNETES_CA', variable: 'KUBERNETES_CA')
]) {
container(name: 'kubectl', shell: '/bin/sh',) {
sh '''kubectl config \
set-cluster kubernetes \
--server=$KUBERNETES_SERVER \
--certificate-authority=$KUBERNETES_CA
'''
sh "yq.v2 w -i kubernetes/deployment.yml 'spec.template.spec.containers[0].image' ${image_name}"
sh "kubectl create namespace ${namespace} || true"
sh "kubectl apply -n ${namespace} -f kubernetes/"
}
}
}
}
}