forked from otomato-gh/usersapi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJenkinsfile
More file actions
142 lines (121 loc) · 4.73 KB
/
Copy pathJenkinsfile
File metadata and controls
142 lines (121 loc) · 4.73 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
pipeline {
agent any
environment {
COMPONENT = "users-api"
REGISTRY = "${env.DOCKER_REGISTRY}"
CLUSTER_NAME = "${env.EKS_CLUSTER_NAME}"
AWS_REGION = "${env.GLOBAL_AWS_REGION}"
BANDIT_FAIL = ""
GRYPE_FAIL = ""
NAMESPACE = "default"
}
stages {
stage('Lint and Test') {
steps {
withPythonEnv("/usr/bin/python3.9") {
script {
sh '''
docker run -d --name postgres -e POSTGRES_PASSWORD=password -e POSTGRES_DB=userdb \
-p 5432:5432 postgres
'''
sh '''
python -m pip install --upgrade pip
pip install flake8 pylint black bandit bandit[sarif] nose bandit_sarif_formatter
pip install -r requirements.txt
'''
sh 'black --check .'
sh '''
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics --ignore="F821,F822"
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=120 --statistics
'''
sh 'pylint **/*.py || true'
sh '''
export POSTGRES_HOST=localhost
export POSTGRES_PORT=5432
python -m nose tests.py
'''
sh '''
bandit -r . -f sarif -o bandit-report.sarif || echo "BANDIT_FAIL=true"
'''
}
}
}
}
stage('Bandit Report Results') {
when {
expression { env.BANDIT_FAIL == "true" }
}
steps {
echo "Bandit scan failed!"
error("Exiting due to Bandit failure")
}
}
stage('Docker Build and Push') {
steps {
withCredentials([usernamePassword(credentialsId: 'docker-creds', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) {
sh '''
echo "$PASSWORD" | docker login -u "$USERNAME" --password-stdin
docker build -t ${COMPONENT}:latest .
docker tag ${COMPONENT}:latest ${REGISTRY}/${COMPONENT}:latest
docker push ${REGISTRY}/${COMPONENT}:latest
grype ${REGISTRY}/${COMPONENT}:latest --fail-on medium -o sarif > grype-report.sarif || echo "GRYPE_FAIL=true"
'''
}
}
}
stage('Grype Report Results') {
when {
expression { env.GRYPE_FAIL == "true" }
}
steps {
echo "Grype scan failed!"
error("Exiting due to Grype failure")
}
}
stage('AWS Authentication & Update Kubeconfig') {
steps {
script {
withCredentials([[
$class: 'AmazonWebServicesCredentialsBinding',
credentialsId: 'aws-creds',
accessKeyVariable: 'AWS_ACCESS_KEY_ID',
secretKeyVariable: 'AWS_SECRET_ACCESS_KEY'
]]) {
sh "aws eks --region ${AWS_REGION} update-kubeconfig --name ${CLUSTER_NAME}"
}
}
}
}
stage('Deploy Helm Chart') {
steps {
script {
withCredentials([[
$class: 'AmazonWebServicesCredentialsBinding',
credentialsId: 'aws-creds',
accessKeyVariable: 'AWS_ACCESS_KEY_ID',
secretKeyVariable: 'AWS_SECRET_ACCESS_KEY'
]]) {
sh '''
helm repo add bitnami2 https://charts.bitnami.com/bitnami
helm dependency build ./chart
kubescape scan ./chart --format sarif --output kubescape-report.sarif
helm upgrade --install ${COMPONENT} ./chart -n ${NAMESPACE} -f ./chart/values.yaml
'''
}
}
}
}
stage('Archive Reports') {
steps {
archiveArtifacts artifacts: '*.sarif', fingerprint: true
}
}
}
post {
always {
script {
sh 'docker stop postgres || true && docker rm postgres || true'
}
}
}
}