Jenkins指定tag发布到k8s环境
Jenkins指定tag发布到k8s环境
1、Jenkins配置一个Pipeline 工程
首先要安装插件:https://www.cnblogs.com/Dev0ps/p/9125232.html
参数化构建过程
Pipeline选择Pipeline script from SCM
保存之后构建失败会提示找不到Jenkinsfile,我们只需要新建相应的Jenkinsfile文件即可
vim /root/.jenkins/workspace/sscp-xxx_sit@script/Jenkinsfile
node (){ //指定Slave标签 stage('Git Checkout') { checkout([$class: 'GitSCM', branches: [[name: '${Tag}']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[url: 'git@code.aliyun.com:xx/xx.git']]]) } //代码编译 stage('Maven Build') { sh ''' export JAVA_HOME=/usr/local/java /usr/local/maven/bin/mvn clean package -Ppresit -Dmaven.test.skip=true ''' } stage('Build and Push Image') { sh ''' REPOSITORY=172.31.182.143/presit/sscp-xxx:${Tag} cp Dockerfile target/ cd target docker build -t $REPOSITORY . docker login -u admin -p Harbor%12345 172.31.182.143 docker push $REPOSITORY ''' } stage('Deploy to k8s') { sh ''' K8S_MASTER="172.31.182.152" APP="sscp-xxx" ENV="presit" ssh ${K8S_MASTER} -t "cd /root/helm/source/sscp-config/sscp-${ENV}/sscp-deploy-config;git checkout ${ENV};git pull" ssh ${K8S_MASTER} cp /root/helm/source/sscp-config/sscp-${ENV}/sscp-deploy-config/${APP}/values.yaml /root/helm/sscp-${ENV}/${APP}/values.yaml ssh ${K8S_MASTER} helm upgrade ${APP}-${ENV} -f /root/helm/sscp-${ENV}/${APP}/values.yaml --set app.updateVersion=`date +%s` chartmuseum/${APP} --set app.image.tag=${Tag} ''' } }
再次构建效果:
如对您有帮助,支持下呗!
微信
支付宝