开发: 持续提交代码并能够通过持续集成(CI)过程快速获取反馈,在通过CI验证后,能够自动化部署到开发环境,以便后续的进一步功能测试(手动/自动自动化测试)等;
测试: 在需要对项目功能进行验证时,可以一键部署测试环境,并且在此环境基础上可以完成功能验收(手动),以及全量的自动化验收测试等;
运维:一键部署生产环境,同时发布创建版本,以便在发布异常时能够快速回归。
stage('Build And Test') {
steps {
dir('containerization-spring-with-helm') {
sh 'docker build -t yunlzheng/spring-sample:$GIT_COMMIT .'
}
}
}
在Build And Test阶段,我们直接通过源码中的Dockerfile定义了整个持续集成阶段的任务,通过docker的Multi-Stage Builds特性,持续集成的所有任务全部通过Dockerfile进行定义,这样无论是在本地还是持续集成服务器中,我们都可以非常方便的进行运行CI任务。
stage('Publish Docker And Helm') {
steps {
withDockerRegistry([credentialsId: 'dockerhub', url: '']) {
sh 'docker push yunlzheng/spring-sample:$GIT_COMMIT'
}
script {
def filename = 'containerization-spring-with-helm/chart/values.yaml'
def data = readYaml file: filename
data.image.tag = env.GIT_COMMIT
sh "rm $filename"
writeYaml file: filename, data: data
}
script {
def filename = 'containerization-spring-with-helm/chart/Chart.yaml'
def data = readYaml file: filename
data.version = env.GIT_COMMIT
sh "rm $filename"
writeYaml file: filename, data: data
}
dir('containerization-spring-with-helm') {
sh 'helm push chart https://repomanage.rdc.aliyun.com/helm_repositories/26125-play-helm --username=$HELM_USERNAME --password=$HELM_PASSWORD --version=$GIT_COMMIT'
}
}
}
environment {
HELM_USERNAME = credentials('HELM_USERNAME')
HELM_PASSWORD = credentials('HELM_PASSWORD')
}
stage('Deploy To Dev') {
steps {
dir('containerization-spring-with-helm') {
dir('chart') {
sh 'helm upgrade spring-app-dev --install --namespace=dev --set ingress.host=dev.spring-example.local .'
}
}
}
}
stage('Deploy To Stageing') {
steps {
input 'Do you approve staging?'
dir('containerization-spring-with-helm') {
dir('chart') {
sh 'helm upgrade spring-app-staging --install --namespace=staging --set ingress.host=staging.spring-example.local .'
}
}
}
}
在Jenkinsfile中我们分别定义了两个阶段Deploy To Dev和Deploy To Stageing。我们通过Kubernetes的命名空间划分单独的开发环境和测试环境。并且通过覆盖ingress.host确保能够通过ingress域名dev.spring-example.local和staging.spring-example.local访问到不同环境。 对于Staging环境而言,通过input确保该流程一定是通过人工确认的。
helm upgrade spring-app-staging --install --namespace=staging --set ingress.host=staging.spring-example.local .
stage('Deploy To Production') {
steps {
input 'Do you approve production?'
script {
env.RELEASE = input message: 'Please input the release version',
ok: 'Deploy',
parameters: [
[$class: 'TextParameterDefinition', defaultValue: '0.0.1', description: 'Cureent release version', name: 'release']
]
}
echo 'Deploy and release: $RELEASE'
script {
def filename = 'containerization-spring-with-helm/chart/Chart.yaml'
def data = readYaml file: filename
data.version = env.RELEASE
sh "rm $filename"
writeYaml file: filename, data: data
}
dir('containerization-spring-with-helm') {
dir('chart') {
sh 'helm lint'
sh 'helm upgrade spring-app-prod --install --namespace=production --set ingress.host=production.spring-example.local .'
}
sh 'helm push chart https://repomanage.rdc.aliyun.com/helm_repositories/26125-play-helm --username=$HELM_USERNAME --password=$HELM_PASSWORD --version=$RELEASE'
}
}
}
在最后一个Deploy To Production阶段中,与Dev和Stageing的部署不同在于当人工确认部署测试环境之后,我们需要用户手动输入当前发布的版本,以确保对当前发布的Chart版本能完成一个基线的定义:
https://github.com/yunlzheng/project-samples