相关信息
使用pipline流水线,创建Jenkins构建任务
json// 去掉shell命令的调试信息
def mysh(cmd, returnStatus) {
return sh (script: '#!/bin/sh -e\n'+ cmd, returnStatus: returnStatus)
}
pipeline {
// 选用节点
agent {
node 'node'
}
// 引用工具
tools{
maven 'maven'
git 'git_node'
}
// jenkins调试参数
options {
buildDiscarder(logRotator(numToKeepStr: '10'))
disableConcurrentBuilds()
timeout(time: 10, unit: 'MINUTES')
timestamps()
}
// 参数化构建
parameters {
// Gitlab代码分支
string(name: 'build_env', defaultValue: 'test', description: '代码分支')
// jar包环境
string(name: 'run_env', defaultValue: 'dev', description: '运行环境')
// gitlab
string(name: 'Gitlab_url', defaultValue: 'git@xxx:xxx', description: 'gitlab地址')
string(name: 'project_name', defaultValue: 'order', description: '项目名称')
string(name: 'project_version', defaultValue: '1.0.0-SNAPSHOT', description: '版本')
string(name: 'project_port', defaultValue: '10012', description: '端口')
// 定义deploy中name
string(name: 'MODULE_NAME', defaultValue: 'order')
// 引用到的k8s集群
string(name: 'K8S_CLUSTER', defaultValue: 'txyun')
// 容器镜像仓库
string(name: 'docker_repositity_url', defaultValue: 'xxx.xxx.tencentyun.com', description: 'docker仓库地址')
//命令空间
string(name: 'NSname', defaultValue: 'test', description: '命名空间')
string(name: 'TAG_name', defaultValue: 'test', description: '镜像版本')
// k8s集群config文件
string(name: 'Config_name', defaultValue: 'test-ns', description: 'config')
// 限制cpu mem
string(name: 'limits_cpu', defaultValue: '2000m', description: 'cpu')
string(name: 'limits_memory', defaultValue: '2048Mi', description: 'mem')
}
stages {
stage('代码检出'){
steps{
echo 'order code pull ${build_env}'
checkout([$class: 'GitSCM', branches: [[name: '*/${build_env}']], extensions: [], userRemoteConfigs: [[credentialsId: 'xxxxx-xxxxx-xxxxx-xxxxx-xxxxx', url: '${Gitlab_url}/${project_name}.git']]])
}
}
stage('构建镜像') {
steps {
echo "build jar and docker image starting... ${docker_repositity_url}"
sh 'mvn clean install -Dmaven.test.skip=true -P ${run_env}'
sh 'docker build --build-arg env_branch=${run_env} -t
// 通过tag来区分版本
${docker_repositity_url}/xhzx/${project_name}:${TAG_name}-$BUILD_ID .'
// 亮色输出提示信息
ansiColor("xterm") {
echo "\u001B[34m build ok...\u001B[0m"
}
}
}
stage('推送仓库') {
steps {
withCredentials([usernamePassword(credentialsId: 'harbor-xxx', passwordVariable: 'docker_password', usernameVariable: 'docker_username')]) {
mysh('docker login -u ${docker_username} -p ${docker_password} ${docker_repositity_url}',true)
mysh('docker push ${docker_repositity_url}/test/${project_name}:${TAG_name}-$BUILD_ID',true)
ansiColor("xterm") {
echo "\u001B[34m push ok...\u001B[0m"
}
}
}
}
stage('配置文件') {
steps {
ansiColor("xterm") {
echo "\u001B[34m 配置文件\u001B[0m"
}
mysh('cat /root/.kube/${Config_name} >/root/.kube/config',true)
mysh('rm -f ${MODULE_NAME}.yaml',true)
// 引用yaml模板
mysh(''' content=$(cat /data/jenkins/yaml/app-deploy.yaml)
eval "cat <<EOF
${content}
EOF" > ${MODULE_NAME}.yaml
''',true)
ansiColor("xterm") {
echo "\u001B[34m 配置文件完成...\u001B[0m"
}
}
}
stage('部署') {
steps {
sh ''' kubectl apply -f ${MODULE_NAME}.yaml
if [ $? -eq 0 ]; then
echo "部署应用成功,开始检查应用状态..."
kubectl rollout status deploy/${MODULE_NAME}-v${BUILD_ID} -n ${NSname}
if [ $? -eq 0 ]; then
echo "应用运行状态正常"
else
echo "应用运行状态异常"
fi
else
echo "部署应用失败"
fi'''
}
}
stage('清理目录') {
steps {
ansiColor("xterm") {
echo "\u001B[34m 清理工作目录...\u001B[0m"
}
deleteDir()
}
}
}
}
本文作者:曹操
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!