Authored by jimi

add deploy

#!groovy
// 此文件适用于的打包对象:
// 1、需要打包编译的
// 项目根目录下存在docker.sh与Dockerfile的,其中docker.sh用于打包,Dockerfile用于镜像的制作。
// 2、不需要编译的项目,直接制作镜像
// 比如NGINX之类的服务
pipeline {
agent { label 'master' }
parameters {
string(name:'projectName', defaultValue: 'qmc', description: 'GITLAB应用名称')
string(name:'projectGitGroup', defaultValue: 'test', description: 'GITLAB所在的组')
string(name:'deployName', defaultValue: 'yoho-ops-qmc', description: '应用名称')
string(name:'imageSource', defaultValue: 'yoho-ops', description: '镜像存储的仓库')
string(name:'imageVersion', defaultValue: '20190823001', description: '镜像的版本')
string(name:'branch', defaultValue: 'master', description: 'git分支名称')
}
//常量参数,初始确定后一般不需更改
environment{
QA_EMAIL='jimi.ji@yoho.cn'
NODE_ENV='production'
KUBERNETES_DEPLOY_YAML='git@git.yoho.cn:docker/k8s-deploy-yaml.git'
}
options {
//保持构建的最大个数
buildDiscarder(logRotator(numToKeepStr: '20'))
}
stages {
stage('init env') {
steps {
script {
if(params.projectName==''){
error "no project ${params.projectName}"
}
def IMAGEVERSION = params.imageVersion
PROJECT_WORK_DIR="${env.WORKSPACE}/${params.projectName}"
QCLOUD_IMAGE_STORE="ccr.ccs.tencentyun.com/${params.imageSource}/${params.deployName}:${IMAGEVERSION}"
if(fileExists("${PROJECT_WORK_DIR}/")){
echo "starting delete the dir ${PROJECT_WORK_DIR}"
sh "rm -rf ${PROJECT_WORK_DIR}"
}
}
echo "PRINT THE PARA projectName ${params.projectName}......"
echo "PRINT THE PARA projectGitGroup ${params.projectGitGroup}......"
echo "PRINT THE PARA deployName ${params.deployName}......"
echo "PRINT THE PARA imageSource ${params.imageSource}......"
echo "PRINT THE PARA clusterId ${params.clusterId}......"
echo "PRINT THE PARA branch ${params.branch}......"
echo "PRINT THE PARA PROJECT_WORK_DIR ${PROJECT_WORK_DIR}......"
echo "PRINT THE PARA QCLOUD_IMAGE_STORE ${QCLOUD_IMAGE_STORE}......"
}
}
stage('clone code') {
steps {
script {
echo "begin clone the project code"
sh "git clone -b ${params.branch} git@git.yoho.cn:${params.projectGitGroup}/${params.projectName}.git ${PROJECT_WORK_DIR}"
echo "starting check the project ${PROJECT_WORK_DIR}"
if(!fileExists("${PROJECT_WORK_DIR}/Dockerfile")){
echo "you need Dockerfile file in project ${params.projectName}"
error "you need Dockerfile file in project ${params.projectName}"
}
}
}
}
stage('clean package project') {
steps {
script {
if(fileExists("${PROJECT_WORK_DIR}/docker.sh")){
sh "sh ${PROJECT_WORK_DIR}/docker.sh ${PROJECT_WORK_DIR}"
}
}
}
}
stage('build push images') {
steps {
script {
sh "docker build -t ${QCLOUD_IMAGE_STORE} ${PROJECT_WORK_DIR}"
sh "docker push ${QCLOUD_IMAGE_STORE}"
}
}
}
}
post {
always {
script {
echo "Pipeline is done,begin send mail"
deleteDir()
}
}
failure {
script {
echo 'print all build and delploy info ---'
echo 'list the package result '
sh "tree ${PROJECT_WORK_DIR}"
}
}
}
}
... ...
#!groovy
pipeline {
agent { label 'master' }
parameters {
choice(name:'clusterId',choices:'yoho-ops\nyoho-web-gray\naws\nyoho-web\nyoho-media\nyoho-java\n', description: '集群的信息')
string(name:'deployName', defaultValue: 'yoho-ops-qmc', description: '应用名称')
string(name:'imageSource', defaultValue: 'yoho-ops', description: '镜像存储的仓库')
string(name:'imageVersion', defaultValue: '20190823001', description: '镜像的版本')
}
//常量参数,初始确定后一般不需更改
environment{
QA_EMAIL='jimi.ji@yoho.cn'
}
options {
//保持构建的最大个数
buildDiscarder(logRotator(numToKeepStr: '20'))
}
stages {
stage('init env') {
steps {
script {
if(params.projectName==''){
error "no project ${params.projectName}"
}
def IMAGEVERSION = params.imageVersion
QCLOUD_IMAGE_STORE="ccr.ccs.tencentyun.com/${params.imageSource}/${params.deployName}:${IMAGEVERSION}"
DEPLOY_YAML_WORK_DIR="${env.WORKSPACE}/k8s-deploy-yaml"
if(fileExists("${DEPLOY_YAML_WORK_DIR}/")){
echo "starting delete the deploy yaml : ${DEPLOY_YAML_WORK_DIR}"
sh "rm -rf ${DEPLOY_YAML_WORK_DIR}"
}
}
echo "PRINT THE PARA projectName ${params.projectName}......"
echo "PRINT THE PARA projectGitGroup ${params.projectGitGroup}......"
echo "PRINT THE PARA deployName ${params.deployName}......"
echo "PRINT THE PARA imageSource ${params.imageSource}......"
echo "PRINT THE PARA clusterId ${params.clusterId}......"
echo "PRINT THE PARA branch ${params.branch}......"
echo "PRINT THE PARA QCLOUD_IMAGE_STORE ${QCLOUD_IMAGE_STORE}......"
}
}
stage('clone code') {
steps {
script {
echo "begin clone the project code"
sh "git clone ${env.KUBERNETES_DEPLOY_YAML} ${DEPLOY_YAML_WORK_DIR}"
}
}
}
stage('bake deploy yaml') {
steps {
script {
sh "sed -i 's?REPLACE_QCLOUD_IMAGE_STORE?$QCLOUD_IMAGE_STORE?g' ${env.WORKSPACE}/k8s-deploy-yaml/${params.clusterId}/${params.deployName}.yaml"
}
}
}
stage('kubectldeploy project') {
steps {
script {
def DEPLOYYAML = "${env.WORKSPACE}/k8s-deploy-yaml/${params.clusterId}/${params.deployName}.yaml"
def KUBECONFIG = "${env.WORKSPACE}/k8s-deploy-yaml/${params.clusterId}/kube-config"
sh "/usr/local/bin/kubectl apply -f $DEPLOYYAML --kubeconfig=$KUBECONFIG"
}
}
}
stage('check service done') {
steps {
script {
sleep(30)
def KUBECONFIG = "${env.WORKSPACE}/k8s-deploy-yaml/${params.clusterId}/kube-config"
def CHECKKUBE= "/usr/local/bin/kubectl get deployment --all-namespaces --kubeconfig=$KUBECONFIG |grep ${params.deployName}-deployment | awk '{print \$3,\$4,\$5,\$6}'"
def SHOWPODINFO= "/usr/local/bin/kubectl get pods --all-namespaces --kubeconfig=$KUBECONFIG |grep ${params.deployName}"
for(int i = 0;i<=60;i++) {
def result = sh returnStdout: true ,script: "$CHECKKUBE"
result = result.trim()
print "request time is : "+i+"and the result is : "+result
if(result==''){
error 'the is no date,please check the result'
}
def compare = result.split(" ")
if(compare[3]==compare[0]&&compare[3]==compare[1]&&compare[3]==compare[2]){
print "----show the pods info ----"
sh "$SHOWPODINFO"
break
}
if(i==60){
error 'TEN MIN OVER,THE DEPLOY FAILURE ,PLEASES CHECK THE LOG'
}
sleep(10)
}
}
}
}
}
post {
always {
script {
echo "Pipeline is done"
deleteDir()
}
}
failure {
script {
echo 'print all build and delploy info ---'
echo 'list the deploy yaml '
sh "cat ${env.WORKSPACE}/k8s-deploy-yaml/${params.clusterId}/${params.deployName}.yaml"
}
}
}
}
... ...