jenkins发版脚本更新

jenkins 项目名中明确了 是jar tar.gz war包
研发需要提供  项目名-地点-环境(研发、测试、生产)-应用项目名称(admin)-包格式(jar\war\gz)

deployment  名字  和  service名字  在 k8s中 定义为   应用项目名称
namespace 为   环境+项目名 (prodsubject 或者 testsubject)

ingress 映射的域名是    环境+应用项目名称(prodadmin.cinyi.com) 

 

delete_old_images() {
    #删除以前的镜像,节省本地空间
    if  docker images | grep $javaname ; then
        docker rmi -f `docker images | grep $javaname | awk '{print $3}'`
    fi
}


#镜像打包上传
image_push() {
    #处理-问题
    docker build -t 192.168.200.10/$namespace_name/$javaname:${BUILD_NUMBER} /testexport/docker-project-${GET_RESULT}/$javadir/
    docker push 192.168.200.10/$namespace_name/$javaname:${BUILD_NUMBER}
}


replace_k8s_yaml() {
    #替换模版中namespace
    sed "s/test-namespace/$namespace_name/g" /testexport/Docker-Template/${GET_RESULT}/namespaces.yaml >/testexport/docker-project-${GET_RESULT}/$javadir/${javaname}-namespaces.yaml

    #替换模版中 deployment文件
    sed "s/im-web/$javaname/g" /testexport/Docker-Template/${GET_RESULT}/im-web-deployment.yaml >/testexport/docker-project-${GET_RESULT}/$javadir/${javaname}-deployment.yaml
    sed -i "s/test-namespace/$namespace_name/g" /testexport/docker-project-${GET_RESULT}/$javadir/${javaname}-deployment.yaml

    # 获取namespace下configmap
    
    if `$k8s_apicurl -basic -u fengjian:fengjian --insecure -X GET $k8s_url/api/v1/namespaces/${namespace_name}/configmaps | grep configmap${namespace_name} >/dev/null`; then
        rm -rf /testexport/docker-project-${GET_RESULT}/$javadir/${javaname}-configmap.yaml
        sed "s/testihospital/${namespace_name}/g" /testexport/Docker-Template/configmap/test_get_configmap.py > /testexport/Docker-Template/configmap/${namespace_name}_get_configmap.py
        /usr/bin/python /testexport/Docker-Template/configmap/${namespace_name}_get_configmap.py | while read line; do
            if grep -v "#env" /testexport/docker-project-${GET_RESULT}/$javadir/${javaname}-deployment.yaml;then
                 sed -i  's/#env/env/g' /testexport/docker-project-${GET_RESULT}/$javadir/${javaname}-deployment.yaml
            fi
            name=`echo $line | awk -F ':' '{print $1}'`
            value=`echo $line | awk -F ':' '{print $2}'`
            sed "s/testenv/configmap${namespace_name}/g" /testexport/Docker-Template/configmap/configmap-env.yaml >> /testexport/docker-project-${GET_RESULT}/$javadir/${javaname}-configmap.yaml
            sed -i "s/mysql_server/$name/g"  /testexport/docker-project-${GET_RESULT}/$javadir/${javaname}-configmap.yaml
            
        done
    fi

    #替换模版中server yaml文件
    sed "s/im-web/$javaname/g" /testexport/Docker-Template/${GET_RESULT}/im-web-service.yaml >/testexport/docker-project-${GET_RESULT}/$javadir/${javaname}-service.yaml
    sed -i "s/test-namespace/$namespace_name/g" /testexport/docker-project-${GET_RESULT}/$javadir/${javaname}-service.yaml

    #替换镜像的版本号(时间为版本号)
    sed -i "s/latest/${BUILD_NUMBER}/g" /testexport/docker-project-${GET_RESULT}/$javadir/${javaname}-deployment.yaml
    
     # 启动pod的个数
    sed -i "s/numberpod/${numberpod}/g" /testexport/docker-project-${GET_RESULT}/$javadir/${javaname}-deployment.yaml
    
    #添加configmap文件
     sed -i "/env/r /testexport/docker-project-${GET_RESULT}/$javadir/${javaname}-configmap.yaml" /testexport/docker-project-${GET_RESULT}/$javadir/${javaname}-deployment.yaml

}

create_update_k8s() {
    #定义namespace 为test:
    
    #创建namespaces
    if ! `$k8s_apicurl -basic -u fengjian:fengjian --insecure -X GET $k8s_url/api/v1/namespaces | grep ${namespace_name} >/dev/null` ;then
      $k8s_apicurl -basic -u fengjian:fengjian --insecure -H "content-Type: application/yaml" -X POST $k8s_url/api/v1/namespaces -d "$(cat /testexport/docker-project-${GET_RESULT}/${javadir}/${javaname}-namespaces.yaml)"
    fi

    #创建service
    #if `$k8s_apicurl -basic -u fengjian:fengjian --insecure -X GET $k8s_url/api/v1/namespaces/${namespace_name}/services  | grep "${javaname}" >/dev/null` ; then
    #     $k8s_apicurl -basic -u fengjian:fengjian --insecure -H "content-Type: application/yaml" -X PUT $k8s_url/api/v1/namespaces/${namespace_name}/services/${javaname}  -d "$(cat /testexport/docker-project-${GET_RESULT}/$javadir/${javaname}-service.yaml)"
    #else     
    #    $k8s_apicurl -basic -u fengjian:fengjian --insecure -H "content-Type: application/yaml" -X POST $k8s_url/api/v1/namespaces/${namespace_name}/services -d "$(cat /testexport/docker-project-${GET_RESULT}/$javadir/${javaname}-service.yaml)"
    #fi


    #创建service
    if `$k8s_apicurl -basic -u fengjian:fengjian --insecure -X GET $k8s_url/api/v1/namespaces/${namespace_name}/services  | grep "${javaname}" >/dev/null` ; then
         $k8s_apicurl -basic -u fengjian:fengjian --insecure -H "content-Type: application/yaml" -X DELETE $k8s_url/api/v1/namespaces/${namespace_name}/services/${javaname}
    fi
    
    $k8s_apicurl -basic -u fengjian:fengjian --insecure -H "content-Type: application/yaml" -X POST $k8s_url/api/v1/namespaces/${namespace_name}/services -d "$(cat /testexport/docker-project-${GET_RESULT}/$javadir/${javaname}-service.yaml)"
   
    
    
    
    #创建Deployment
    if `$k8s_apicurl -basic -u fengjian:fengjian --insecure -X GET $k8s_url/apis/apps/v1beta2/namespaces/${namespace_name}/deployments/ | grep "${javaname}" >/dev/null` ; then
        #滚动升级
        $k8s_apicurl -basic -u fengjian:fengjian --insecure -H "content-Type: application/yaml" -X PUT $k8s_url/apis/apps/v1beta2/namespaces/${namespace_name}/deployments/${javaname} -d "$(cat /testexport/docker-project-${GET_RESULT}/$javadir/${javaname}-deployment.yaml)"
    else
        $k8s_apicurl -basic -u fengjian:fengjian --insecure -H "content-Type: application/yaml" -X POST $k8s_url/apis/apps/v1beta2/namespaces/${namespace_name}/deployments/ -d "$(cat /testexport/docker-project-${GET_RESULT}/$javadir/${javaname}-deployment.yaml)"
    fi

}


add_ingress() {
    
  
  # 假如使用域名方式访问
    if ! `$k8s_apicurl -basic -u fengjian:fengjian --insecure  -X  GET $k8s_url/apis/extensions/v1beta1/namespaces/${namespace_name}/ingresses | grep "${dnsname}.cinyi.com" >/dev/null`; then
        if [ $accessmode -eq 0 ] ; then
             cat /testexport/Docker-Template/ingress/ingress-domain-template.yaml > /testexport/docker-project-${GET_RESULT}/$javadir/${javaname}-ingress.yaml
             sed -i "s/default/${namespace_name}/g" /testexport/docker-project-${GET_RESULT}/$javadir/${javaname}-ingress.yaml
             sed -i "s/nginx/${dnsname}/g" /testexport/docker-project-${GET_RESULT}/$javadir/${javaname}-ingress.yaml
             sed -i "s/servicename/${javaname}/g" /testexport/docker-project-${GET_RESULT}/$javadir/${javaname}-ingress.yaml
             $k8s_apicurl -basic -u fengjian:fengjian --insecure  -H "content-Type: application/yaml" -X  POST $k8s_url/apis/extensions/v1beta1/namespaces/${namespace_name}/ingresses  -d "$(cat /testexport/docker-project-${GET_RESULT}/$javadir/${javaname}-ingress.yaml)"
        else
             cat /testexport/Docker-Template/ingress/ingress-domain-project-template.yaml > /testexport/docker-project-${GET_RESULT}/$javadir/${javaname}-ingress.yaml
             sed -i "s/default/${namespace_name}/g" /testexport/docker-project-${GET_RESULT}/$javadir/${javaname}-ingress.yaml
             sed -i "s/nginx/${dnsname}/g" /testexport/docker-project-${GET_RESULT}/$javadir/${javaname}-ingress.yaml
             sed -i "s/servicename/${javaname}/g" /testexport/docker-project-${GET_RESULT}/$javadir/${javaname}-ingress.yaml
             projectname=`echo ${javaname} | tr 'a-z' 'A-Z'`
             sed -i "s/projectname/${projectname}/g" /testexport/docker-project-${GET_RESULT}/$javadir/${javaname}-ingress.yaml
             $k8s_apicurl -basic -u fengjian:fengjian --insecure  -H "content-Type: application/yaml" -X  POST $k8s_url/apis/extensions/v1beta1/namespaces/${namespace_name}/ingresses  -d "$(cat /testexport/docker-project-${GET_RESULT}/$javadir/${javaname}-ingress.yaml)"
        fi
    else    
         #假如有dnsname.cinyi.com域名,那么更新
         if [ $accessmode -eq 0 ] ; then
             cat /testexport/Docker-Template/ingress/ingress-domain-template.yaml > /testexport/docker-project-${GET_RESULT}/$javadir/${javaname}-ingress.yaml
             sed -i "s/default/${namespace_name}/g" /testexport/docker-project-${GET_RESULT}/$javadir/${javaname}-ingress.yaml
             sed -i "s/nginx/${dnsname}/g" /testexport/docker-project-${GET_RESULT}/$javadir/${javaname}-ingress.yaml
             sed -i "s/servicename/${javaname}/g" /testexport/docker-project-${GET_RESULT}/$javadir/${javaname}-ingress.yaml
             #curl --cacert /root/ca.pem -basic -u fengjian:fengjian --insecure -X DELETE  https://192.168.20.171:6443/apis/extensions/v1beta1/namespaces/testsy/ingresses/adminserver
             #先删除
             $k8s_apicurl -basic -u fengjian:fengjian --insecure -X  DELETE $k8s_url/apis/extensions/v1beta1/namespaces/${namespace_name}/ingresses/${javaname} 
             #从新再创建
             $k8s_apicurl -basic -u fengjian:fengjian --insecure -H "content-Type: application/yaml" -X  POST  $k8s_url/apis/extensions/v1beta1/namespaces/${namespace_name}/ingresses  -d  "$(cat /testexport/docker-project-${GET_RESULT}/$javadir/${javaname}-ingress.yaml)" 
        else
            cat /testexport/Docker-Template/ingress/ingress-domain-project-template.yaml > /testexport/docker-project-${GET_RESULT}/$javadir/${javaname}-ingress.yaml
             sed -i "s/default/${namespace_name}/g" /testexport/docker-project-${GET_RESULT}/$javadir/${javaname}-ingress.yaml
             sed -i "s/nginx/${dnsname}/g" /testexport/docker-project-${GET_RESULT}/$javadir/${javaname}-ingress.yaml
             sed -i "s/servicename/${javaname}/g" /testexport/docker-project-${GET_RESULT}/$javadir/${javaname}-ingress.yaml
             projectname=`echo ${javaname} | tr 'a-z' 'A-Z'`
             sed -i "s/projectname/${projectname}/g" /testexport/docker-project-${GET_RESULT}/$javadir/${javaname}-ingress.yaml
             #先删除
             $k8s_apicurl -basic -u fengjian:fengjian --insecure -X  DELETE $k8s_url/apis/extensions/v1beta1/namespaces/${namespace_name}/ingresses/${javaname} 
             #重新创建
             $k8s_apicurl -basic -u fengjian:fengjian --insecure -H "content-Type: application/yaml" -X  POST $k8s_url/apis/extensions/v1beta1/namespaces/${namespace_name}/ingresses -d  "$(cat /testexport/docker-project-${GET_RESULT}/$javadir/${javaname}-ingress.yaml)" 
    
        fi
    fi

}



replace_content() {

    #创建/testexport/docker-project-gz/js/jar/war目录
    if [ -f /testexport/docker-project-$GET_RESULT/$javadir/ ] ;then
      rm -rf /testexport/docker-project-$GET_RESULT/$javadir/*
    else
      mkdir -p /testexport/docker-project-$GET_RESULT/$javadir/
    fi
  
  if [ "$1" == "gz" ] ;then

    #将编译后的war包拷贝到/testexport/docker-project-gz/$javadir目录下
    mv $javaname_path /testexport/docker-project-gz/$javadir/$javaname.tar.${GET_RESULT}
    
    #在/testexport/Docker-Template/gz 目录下有一个dockerfile模版,根据war包的名字替换成新的dockerfile
    
    #替换Dockerfile中的tar 名字
    sed "s/jenkins/$javaname/g" /testexport/Docker-Template/gz/Dockerfile >/testexport/docker-project-gz/$javadir/Dockerfile
        
    elif [ "$1"  == "war" ] ;then
     
      #将编译后的war包拷贝到/testexport/docker-project-war/$javadir目录下
      mv  $javaname_path  /testexport/docker-project-war/$javadir/$javaname.${GET_RESULT}
      
      #在/testexport/Docker-Template 目录下有一个dockerfile模版,根据war包的名字替换成新的dockerfile
      
      #替换Dockerfile中的war 名字
      sed "s/jenkins/$javaname/g" /testexport/Docker-Template/war/Dockerfile >/testexport/docker-project-war/$javadir/Dockerfile
  
    elif [ "$1"  == "jar" ] ;then
      
      #将编译后的war包拷贝到/testexport/docker-project-jar/$javadir目录下
      mv $javaname_path  /testexport/docker-project-jar/$javadir/$javaname.${GET_RESULT}
      
      #在/testexport/Docker-Template/jar 目录下有一个dockerfile模版,根据war包的名字替换成新的dockerfile
      
      #替换Dockerfile中的war 名字
      sed "s/jenkins/$javaname/g" /testexport/Docker-Template/jar/Dockerfile >/testexport/docker-project-jar/$javadir/Dockerfile
      /bin/cp /testexport/Docker-Template/jar/start.sh /testexport/docker-project-jar/$javadir/

    elif [ "$1"  == "js" ] ;then
      
      #将编译后的war包拷贝到/testexport/docker-project-js/$javadir目录下

      cd  $WORKSPACE
      #cnpm install --registry=http://192.168.20.71:8081/repository/npm-repo-proxy/
      cnpm install --registry=https://registry.npm.taobao.org
      cnpm install sass-loader node-sass -D
      cnpm install
      cnpm run build-empi
        
      mv $WORKSPACE/dist/empi    /testexport/docker-project-js/$javadir/
      
      mv $WORKSPACE/config/nginx.conf  /testexport/docker-project-js/$javadir/

      #导入Dockerfile模板
      cp  /testexport/Docker-Template/js/Dockerfile   /testexport/docker-project-js/$javadir/
      
      sed -i "s/172.16.201.50:9001/empirest/g" /testexport/docker-project-js/$javadir/nginx.conf
      sed -i "s/172.16.201.50:7001/webadmin/g" /testexport/docker-project-js/$javadir/nginx.conf
      sed -i "s/172.16.201.50:6001/authserver/g" /testexport/docker-project-js/$javadir/nginx.conf

  fi

  
  delete_old_images
    
  image_push

  replace_k8s_yaml
  
  create_update_k8s

  add_ingress


}


#镜像仓库地址
registry="192.168.200.10"
#调用的证书
k8s_apicurl="curl --cacert /root/ca.pem"
#调用地址
k8s_url="https://192.168.20.171:6443"

#取出项目目录subject-bj-prod-serverapi-gz
javadir=`echo $WORKSPACE | awk -F'/' '{print $5}'`

#判断结尾名称格式war|jar|gz
GET_RESULT=`echo $javadir | awk -F'-' '{print $NF}'`

#java的路径 /root/.jenkins/jobs/subject-bj-test-imserver/workspace/target/11.war
## 注意:如果存在分支情况,javaname_path 路径需要具体查看jenkins再做修改

#有分支
#javaname_path=`find $WORKSPACE/xxx/target/  -maxdepth 1  -type f | egrep "\.${GET_RESULT}$"`

#无分支
javaname_path=`find $WORKSPACE/target/  -maxdepth 1  -type f | egrep "\.${GET_RESULT}$"`

#获取jar名称 11.war
javaname_all=`echo $javaname_path | awk -F'/' '{print $NF}'`

#获取jar名称 11
javaname=`echo $javadir| awk -F'-' '{print $4}'`


#取出环境(生产或者测试)prod
environment=`echo $javadir | awk -F'-' '{print $3}'`

#获取namespace 
namespace_name=$environment`echo $javadir | awk -F'-' '{print $1}'`

#dns域名
#dnsname="xkim"
dnsname=$environment$javaname


case "$GET_RESULT" in
    war|jar|gz|js)
    replace_content $GET_RESULT
    ;;
    *)
    echo "未知"
    exit 1
    ;;
esac

 

posted @ 2018-05-26 17:42  fengjian1585  阅读(392)  评论(0编辑  收藏  举报