forked from kubesphere/devops-python-sample
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile-online
71 lines (61 loc) · 2.03 KB
/
Jenkinsfile-online
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
pipeline {
agent {
node {
label 'base'
}
}
environment {
DOCKER_CREDENTIAL_ID = 'dockerhub'
KUBECONFIG_CREDENTIAL_ID = 'kubeconfig'
REGISTRY = 'docker.io'
DOCKERHUB_NAMESPACE = 'shaowenchen'
APP_NAME = 'devops-python-sample'
SONAR_CREDENTIAL_ID = 'sonar-token'
}
parameters {
string(name: 'BRANCH_NAME', defaultValue: 'master', description: '')
}
stages {
stage('拉取代码') {
steps {
container('base') {
checkout([$class: 'GitSCM', branches: [[name: '*/$BRANCH_NAME']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[url: 'https://github.com/shaowenchen/devops-python-sample.git']]])
}
/* if in scm, you can use: checkout scm */
}
}
stage('代码检查') {
steps {
container('base') {
withCredentials([string(credentialsId : "$SONAR_CREDENTIAL_ID" ,variable : 'SONAR_TOKEN' ,)]) {
withSonarQubeEnv('sonar') {
sh '''pip install pkgs/base/* & pip install pkgs/extra/*
coverage erase
cd ./src/
coverage run --source='.' manage.py test
coverage xml -i
'''
sh "sonar-scanner -Dsonar.branch=$BRANCH_NAME -Dsonar.login=$SONAR_TOKEN"
}
}
}
}
}
stage('推送镜像') {
steps {
container('base') {
withCredentials([usernamePassword(credentialsId : "$DOCKER_CREDENTIAL_ID" ,passwordVariable : 'DOCKER_PASSWORD' ,usernameVariable : 'DOCKER_USERNAME' ,)]) {
sh 'echo "$DOCKER_PASSWORD" | docker login $REGISTRY -u "$DOCKER_USERNAME" --password-stdin'
sh 'docker build -f Dockerfile-online -t $REGISTRY/$DOCKERHUB_NAMESPACE/$APP_NAME:$BUILD_NUMBER .'
sh 'docker push $REGISTRY/$DOCKERHUB_NAMESPACE/$APP_NAME:$BUILD_NUMBER'
}
}
}
}
stage('部署') {
steps {
kubernetesDeploy(enableConfigSubstitution: true, kubeconfigId: "$KUBECONFIG_CREDENTIAL_ID", configs: 'deploy/**')
}
}
}
}