首页 > 其他分享 >jenkinsfile

jenkinsfile

时间:2024-11-16 12:07:10浏览次数:1  
标签:name app yaml sh build def jenkinsfile

k8s集群中用的jenkinsfile部署

java后台项目

# x项目 test-java-eladmin-ci

// harbor镜像仓库地址和密码信息
def registry = "harbor.solomon.com"
// jenkins中定义的连接harbor的用户名密码凭据
def registry_se = "d2318c77-8a32-4618-bced-d6cabb0454b4"
 
 
// gitlab地址和密码信息
def gitlab_url = "http://192.168.17.18/java/eladmin.git"
// jenkins中定义git连接gitlab的密码凭据
def gitlab_se = "6e58036d-21ad-42b4-a8d2-50fb700f92c7"
 
 
// jenkins中k8s-api 连线k8s集群的凭据信息
// jenkins中Config File provice插件定义的kubeconfig的内容
def k8s_auth = "782d66a2-d207-4398-b823-786b999db338"
 
 
// k8s连接harbor的证书(kubectl命令离开k8s集群时需要,例如目前的jenkins环境)
def secret_name = "registry-pull-secret"
 
// harbor仓库的项目前缀信息
def prefix = "library"
 
 
// 部署应用的服务名称
def app_name = "access-ultra-vires"
// 部署服务所在的命名空间、副本数、容器暴露的端口、svc的端口
def ns = "default"
def rc = 3
def cport = 8008 
def cluport = 8008 
// 构建编译的环境参数
def env = "default"
 
 
node(){
  stage("1.pull git code"){
    checkout([$class: 'GitSCM', branches: [[name: '*/dev']], userRemoteConfigs: [[credentialsId: "${gitlab_se}", url: "${gitlab_url}"]]]) 
        script {
            build_tag = sh(returnStdout: true, script: 'git rev-parse --short HEAD').trim()
            TM = sh(script: 'date +%Y%m%d%H%M')
        }
        image_name_build = "${registry}/${prefix}/${app_name}:${BUILD_NUMBER}"
        image_name_latest = "${registry}/${prefix}/${app_name}:latest"
         
  }
 
  stage("2.build code"){
      sh "mvn clean package -P${env} -DskipTests"
  }
  stage("3.build docker and push to harbor") {
        withCredentials([usernamePassword(credentialsId: "${registry_se}", passwordVariable: 'dockerHubPassword', usernameVariable: 'dockerHubUser')]) {
            sh "docker login -u ${dockerHubUser} -p ${dockerHubPassword} ${registry}"
            sh "docker build -t ${image_name_build}  -f Dockerfile .  --no-cache"
            sh "docker tag ${image_name_build} ${image_name_latest}"
            sh "docker push ${image_name_build}"
            sh "docker push ${image_name_latest}"
        }
    }
  stage("4.deploy to k8s") {
        // sh "/usr/bin/ossutil cp -uf oss://prod-***/api/blue/templete-v1.yaml templete-v1.yaml"
        sh "cp templete-v1.yaml ${app_name}-dep.yaml"
        sh "sed -i 's#SVC_NAME#${app_name}#g'  ${app_name}-dep.yaml"
        sh "sed -i 's#NS_NAME#${ns}#g'  ${app_name}-dep.yaml"
        sh "sed -i 's#RC_NUM#${rc}#g'  ${app_name}-dep.yaml"
        sh "sed -i 's#IMAGE_URL#${image_name_latest}#g'  ${app_name}-dep.yaml"
        sh "sed -i 's#CON_PORT#${cport}#g'  ${app_name}-dep.yaml"
        sh "sed -i 's#CLU_PORT#${cluport}#g'  ${app_name}-dep.yaml"
        sh "kubectl apply -f  ${app_name}-dep.yaml"
        echo "deploy success"
    }
}
View Code

 

vue前端项目

# 项目 vue-test-ci
// harbor镜像仓库地址和密码信息
def registry = "harbor.solomon.com"
// jenkins中定义的连接harbor的用户名密码凭据
def registry_se = "d2318c77-8a32-4618-bced-d6cabb0454b4"
 
 
// gitlab地址和密码信息
def gitlab_url = "http://192.168.17.18/vue/eladmin-web.git"
// jenkins中定义git连接gitlab的密码凭据
def gitlab_se = "6e58036d-21ad-42b4-a8d2-50fb700f92c7"
 
 
// jenkins中k8s-api 连线k8s集群的凭据信息
// jenkins中Config File provice插件定义的kubeconfig的内容
def k8s_auth = "782d66a2-d207-4398-b823-786b999db338"
 
 
// k8s连接harbor的证书(kubectl命令离开k8s集群时需要,例如目前的jenkins环境)
def secret_name = "registry-pull-secret"
 
 
// harbor仓库的项目前缀信息
def prefix = "library"
 
 
// 部署应用的服务名称
def app_name = "eladmin-web"
// 部署服务所在的命名空间、副本数、容器暴露的端口、svc的端口
def ns = "default"
def rc = 3
def cport = 80
def cluport = 80 
// 构建编译的环境参数
def env = "default"
 
 
node(){
  stage("1.pull git code"){
    checkout([$class: 'GitSCM', branches: [[name: '*/main']], userRemoteConfigs: [[credentialsId: "${gitlab_se}", url: "${gitlab_url}"]]]) 
        script {
            build_tag = sh(returnStdout: true, script: 'git rev-parse --short HEAD').trim()
            TM = sh(script: 'date +%Y%m%d%H%M')
        }
        image_name_build = "${registry}/${prefix}/${app_name}:${BUILD_NUMBER}"
        image_name_latest = "${registry}/${prefix}/${app_name}:latest"
         
  }
 
  stage("2.run build"){
      nodejs("npm"){
          sh("node -v && npm -v")
          sh("npm install")
          sh("npm run build:prod")
          sh("tar zcvf dist.tar.gz dist")
      }
  }
    stage("3.build docker and push to harbor") {
        withCredentials([usernamePassword(credentialsId: "${registry_se}", passwordVariable: 'dockerHubPassword', usernameVariable: 'dockerHubUser')]) {
            sh "docker login -u ${dockerHubUser} -p ${dockerHubPassword} ${registry}"
            sh "docker build -t ${image_name_build}  -f Dockerfile .  --no-cache"
            sh "docker tag ${image_name_build} ${image_name_latest}"
            sh "docker push ${image_name_build}"
            sh "docker push ${image_name_latest}"
            // 删除压缩包,等待下次构建
            sh "rm -f dist.tar.gz"
        }
    }
  stage("4.deploy to k8s") {
        sh "cp templete-v1.yaml ${app_name}-dep.yaml"
        sh "sed -i 's#SVC_NAME#${app_name}#g'  ${app_name}-dep.yaml"
        sh "sed -i 's#NS_NAME#${ns}#g'  ${app_name}-dep.yaml"
        sh "sed -i 's#RC_NUM#${rc}#g'  ${app_name}-dep.yaml"
        sh "sed -i 's#IMAGE_URL#${image_name_latest}#g'  ${app_name}-dep.yaml"
        sh "sed -i 's#CON_PORT#${cport}#g'  ${app_name}-dep.yaml"
        sh "sed -i 's#CLU_PORT#${cluport}#g'  ${app_name}-dep.yaml"
        sh "kubectl apply -f  ${app_name}-dep.yaml"
        echo "deploy success"
    }
  
}
View Code

 

  

标签:name,app,yaml,sh,build,def,jenkinsfile
From: https://www.cnblogs.com/zhuhaofeng/p/18549246

相关文章

  • Pipeline流水线通过git拉取Jenkinsfile报错 error: RPC failed; result=22, HTTP code
    Pipeline流水线通过git拉取Jenkinsfile报错error:RPCfailed;result=22,HTTPcode=404在学习共享库时使用通过git拉取jenkinsfile时,报错在排查gitlab服务状态,网络通讯,防火墙规则以及Jenkins凭据均可以正常使用,最后发现的时在URL填写中缺少.git结尾所导致的,在了解后得知在......
  • Jenkinsfile添加清理工作空间
    前端node由于依赖的版本问题会需要使用清理工作空间1.在parameters中添加booleanParam(name:'CleanWorkspace',defaultValue:false,description:'清理工作空间,默认不清理!')2.在stages.stage.steps.script初始化信息中添......
  • 4.7 在 VS Code 中校验 Jenkinsfile
    在日常工作中,我经常需要创建或修改很多Jenkinsfile,有时还会发生错误。这是一个非常繁琐的流程——修改Jenkinsfile,提交、推送,然后等Jenkins提醒你少加了一个括号。Command-linePipelineLinter(https://jenkins.io/doc/book/pipeline/development/)可以有效地减少编写Jenk......
  • 4.1 开始编写Jenkinsfile
    为什么使用pipeline?本质上,jenkins是一个自动化引擎,它支持许多自动模式。流水线向Jenkins添加了一组强大的工具,支持用例、简单的持续集成到全面的持续交付流水线。通过对一系列的发布任务建立标准的模板,用户可以利用更多流水线的特性,比如:代码化:流水线是在代码中实现的,通常会......
  • Docker+Jenkins+Pipline如何获取git插件环境变量(提交sha、分支等)以及Jenkinsfile中
    场景Docker中部署Jenkins+Pipline流水线基础语法入门:https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/139672283上面介绍了环境搭建以及Pipeline的Jenkinsfile的常用写法。如果需要通过Jenkins插件获取git相关的信息,比如上一次提交的SHA,分支名称等信息,然后需要输......
  • jenkinsfile流水线
     参考:https://blog.csdn.net/wx17343624830/article/details/136906665参考:https://blog.csdn.net/Step_By_XXX/article/details/131470414pipeline{agentanytools{maven'Maven-3.9.6'}parameters{gitParameterbranchFilter:......
  • DevOps系列---【jenkinsfile使用sshpass发送到另一台服务器】
    1.首先在宿主机安装sshpass2.把物理机的sshpass复制到容器中whichsshpasscp$(whichsshpass)/usr/local/app/dockercpsshpass容器id:/usr/local/bin/sshpass3.在jenkinsfile中添加#在stages中添加stagestage('部署TEST服务'){steps{sh"sshpass-proot......
  • jenkins pipline 和 jenkinsfile
    JenkinsPipeline(或简称为"Pipeline")是一套插件,将持续交付的实现和实施集成到Jenkins中。JenkinsPipeline提供了一套可扩展的工具,用于将“简单到复杂”的交付流程实现为“持续交付即代码”。JenkinsPipeline的定义通常被写入到一个文本文件(称为Jenkinsfile)中,该文件可以被......
  • pipeline编译(jenkinsfile)
    1.pipeline概念  Pipeline是Jenkins中最为灵活的job构建方式,可实现像流水线一样调度Jenkins任务,  通过Jenkinsfile描述整个持续集成流程2.pipeline编写风格:声明式风格、脚本式风格  声明式:格式有强规范性(优势:可读性强。缺点:不灵活、代码冗长)  脚本式:使用gro......
  • Jenkinsfile使用k8s agent构建失败:Container jnlp was terminated (Exit Code: 1, Rea
    问题描述Jenkinsfile使用k8sagent构建失败jenkins报错截图:查看pod app-system-23-wmx8b-5lnl2-lxvlr的jnlp容器日志:分析处理一般构建失败大都是jnlp容器问题。经以下日志分析发现jenkins主节点和slave节点的jdk版本不一致导致该提示JavaJDK版本不对:hudson/slaves/SlaveComputer......