tiger项目Jenkins后端项目pipeline

pipeline {
agent any
environment {
dockerhub_credentials = credentials('ea8728ad-b76d-461a-aa86-4633d919baac')
K8S_CONFIG = credentials('df50c584-6739-4bd4-ba4a-53e322729f04')
image_tag = sh(returnStdout: true,script: 'echo `date +"%Y%m%d%H%M"_``git describe --tags --always`').trim()
}
parameters {
string(name: 'project_name', defaultValue: 'tiger-acct-admin', description: '项目名称')
string(name: 'git_url', defaultValue: 'http://git.xxx.com/Tiger/tiger-acct-center.git', description: '仓库地址')
string(name: 'branch_name', defaultValue: 'dev', description: 'git分支')
string(name: 'profiles', defaultValue: 'dev', description: '环境')
string(name: 'replicas', defaultValue: '1', description: '项目副本数')
}
stages{
stage('代码检出') {
steps{
checkout([$class: 'GitSCM', branches: [[name: "*/${params.branch_name}"]],
            	doGenerateSubmoduleConfigurations: false,
            	extensions: [],
            	submoduleCfg: [],
                userRemoteConfigs: [[
                    credentialsId: 'd2183ac7-b4a7-4ef5-86ce-22190ae33526',
                	url: "${params.git_url}"]]])
}
}
stage("编译打包"){
steps{
sh "mvn clean package -Dfile.encoding=UTF-8 -DskipTests=true"
}
}
stage('构建镜像|推送') {
steps {
sh "pwd"
sh "cp -rf ${env.WORKSPACE}/${params.project_name}/target/${params.project_name}.jar ${env.WORKSPACE}/deploy/${params.branch_name}/app.jar"
sh "docker login -u ${dockerhub_credentials_USR} -p ${dockerhub_credentials_PSW} 104.168.133.40"
sh "cd ${env.WORKSPACE}/deploy/${params.branch_name}/ && docker build -t 104.168.133.40/xxx/${params.project_name}:${image_tag} ."
sh "docker push 104.168.133.40/xxx/${params.project_name}:${image_tag}"
sh "rm -rf app.jar"
}
}
stage('生成发布模板') {
steps {
sh "sed -e 's#{{APP_NAME}}#${params.project_name}#g;s#{{NAMESPACE}}#${params.project_name}-${params.profiles}#g;s#{{APP_REPLICAS}}#${params.replicas}#g;s#{{PROFILES}}#${params.profiles}#g;s#{{IMAGE_URL}}#104.168.133.40/xxx/${params.project_name}#g;s#{{IMAGE_TAG}}#${image_tag}#g;s#{{HOST}}#${params.hostname}#g' ${env.WORKSPACE}/deploy/${params.branch_name}/k8s.tpl > k8s.yaml"
stash name: "k8s.yaml", includes: "k8s.yaml"
sh "cat k8s.yaml"
}
}
stage("发布到K8S") {
steps {
unstash("k8s.yaml")
sh "kubectl apply -f k8s.yaml --server https://104.168.135.42:6443 --kubeconfig /tmp/config && sleep 6 && kubectl get po,svc,ing -ntiger-${params.branch_name} --server https://104.168.135.42:6443 --kubeconfig /tmp/config|grep ${params.project_name}"
}
}
}
}

主要更改如上(发布到K8S),复制集群的config文件到指定目录,复制/usr/bin/kubectl 到jenkins这台机器即可


本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!