配置Jenkins連線kubernetes的Pod Template模板
阿新 • • 發佈:2021-07-01
配置Jenkins連線kubernetes模板
如果跑java 的流水線工程,顯示Jenkins從業務節點pending狀態,如果是排程出現cpu或者記憶體大小資源問題,可以到Jenkins "配置">"configure cloud" > "kubernetes" > "Template" > "java模板" 中修改resource的大小,使其可以分配
進入Jenkins配置->修改Configure Cloud模板
配置一個kubernetes節點
增加Pod Template配置
在最下面增加pod template配置,當前主要增加了預設的Jenkins-slave節點、Jenkins-slave-Java節點、Jenkins-slave-golang1.15節點、Jenkins-slave-python3.7節點、Jenkins-slave-nodejs12節點的模板。
分解顯示--> base Template
分解顯示--> Java Template
分解顯示--> golang1.15 Template
分解顯示--> nodejs12 Template
分解顯示--> Python 3.7 Template
以上就是Jenkins web 頁面連線kubernetes的配置,主要是連線Jenkins之後使其自動拉起Jenkins-slave元件。
附上釋出Java的Jenkinsfile檔案
Jenkinsfile檔案如下
void clone_stepsFunc() { script { def defaultTimeout = '10' def inputTimeout = '10' def validReg = '^[1-9]\\d*$' if (!inputTimeout.matches(validReg)) { inputTimeout = defaultTimeout } timeout(time: inputTimeout.toInteger(), unit: "MINUTES"){ def repositoryURL = "http://192.168.40.195:31101/root/testjava" def credentialsID = "" def relativeDirectory = "." env.RELATIVE_DIRECTORY = relativeDirectory def branch = "master" if ("".equals(branch)) { branch = "master" } def useBindCodeRepository = false useBindCodeRepository = true def isPr=false; def sourceBranch try { def alaudaDevopsInstance = alaudaDevops.newInstance() alaudaDevopsInstance.withCluster() { alaudaDevopsInstance.withProject(alaudaContext.getNamespace()){ def isTriggerByCodeRepoPushEvent try { isTriggerByCodeRepoPushEvent = alaudaEvent.isCodeRepoPushEvent() } catch (groovy.lang.MissingPropertyException e) { echo "Error when try to read event details from global variable, you probably are using a lower version Jenkins, reason: ${e.message}" isTriggerByCodeRepoPushEvent = false } // if pipeline is triggered by coderepository.push event, // we will clone the repository that has been pushed. if (isTriggerByCodeRepoPushEvent) { def eventContext = alaudaEvent.getCodeRepoPushContext() def codeRepository = alaudaDevopsInstance.selector("coderepository", eventContext.repoName).object() credentialsID = codeRepository.metadata.annotations.secretNamespace + '-' + codeRepository.metadata.annotations.secretName repositoryURL = codeRepository.spec.repository.cloneURL if(eventContext!=null){ if (eventContext.getEventType()=="PRBranch"){ branch = eventContext.targetBranch sourceBranch = eventContext.sourceBranch isPr=true }else { branch = eventContext.branch } } } else if (useBindCodeRepository) { def codeRepository = alaudaDevopsInstance.selector("coderepository", "demo1-operators-demo1-gitlab-sample-root-testjava").object() credentialsID = codeRepository.metadata.annotations.secretNamespace + '-' + codeRepository.metadata.annotations.secretName } } } } catch (err) { error err.getMessage() } if (credentialsID == "") { credentialsID = "demo1-p1-demo1-gitlab-root" } def scmVars=null if (isPr){ sh script: 'git config --global user.email "default"' sh script: 'git config --global user.name "Default"' scmVars = checkout([ $class: 'GitSCM', branches: [[name: sourceBranch]], extensions: [[ $class: 'PreBuildMerge', options: [ fastForwardMode: 'FF', mergeRemote: 'origin', mergeStrategy: 'DEFAULT', mergeTarget: branch] ],[ $class: 'SubmoduleOption', recursiveSubmodules: true, parentCredentials: true, reference: '', ],[ $class: 'RelativeTargetDirectory', relativeTargetDir: relativeDirectory ],[ $class: 'RecordLastChangeLog' ],[ $class: 'CheckoutOption', timeout: inputTimeout.toInteger() ]], userRemoteConfigs: [[ credentialsId: credentialsID, url: repositoryURL ]] ]) } else { scmVars = checkout([ $class: 'GitSCM', branches: [[name: branch]], extensions: [[ $class: 'SubmoduleOption', recursiveSubmodules: true, parentCredentials: true, reference: '', ],[ $class: 'RelativeTargetDirectory', relativeTargetDir: relativeDirectory ],[ $class: 'RecordLastChangeLog' ],[ $class: 'CheckoutOption', timeout: inputTimeout.toInteger() ]], userRemoteConfigs: [[ credentialsId: credentialsID, url: repositoryURL ]] ]) } dir(RELATIVE_DIRECTORY){ env.FROM_SCM = true env.SCM_AUTHOR = sh (script: 'git log -1 --pretty=format:"%an"',returnStdout: true).trim() env.SCM_COMMIT_INFO = sh (script: 'git log -1 --pretty=%B',returnStdout: true).trim() env.SCM_COMMIT = scmVars.GIT_COMMIT env.GIT_COMMIT = scmVars.GIT_COMMIT env.IS_PR = isPr if (isPr){ env.GIT_BRANCH = sourceBranch //compatible with multi-branch pipeline env.BRANCH_NAME = sourceBranch env.TARGET_BRANCH = branch } else { env.GIT_BRANCH = scmVars.GIT_BRANCH.replaceFirst("origin/","") //compatible with multi-branch pipeline env.BRANCH_NAME = scmVars.GIT_BRANCH.replaceFirst("origin/","") env.TARGET_BRANCH = "" } //compatible with old version pipeline env.GIT_BRANCH_AS_TAG = env.GIT_BRANCH.replaceAll("/","-") } alaudaPipeline.appendInfo(STAGE_NAME, [commit_id: scmVars.GIT_COMMIT, branch: scmVars.GIT_BRANCH, repo_url: repositoryURL as String], '_Clone') env.CREDENTIAL_ID = credentialsID env.CODE_REPO = repositoryURL env.TIMESTAMP = new Date().format("yyyyMMddHHmmss") } } } void maven_stepsFunc() { script { try{ container('java'){ sh """ mvn clean package """ } }catch(Exception e){ throw e }finally{ } } } void build_docker_stepsFunc() { script { def alaudaDevopsInstance = alaudaDevops.newInstance() def retryCount = 3 def repositoryAddr = '192.168.40.195:31104/demo1-p1/testjava'.replace("http://","").replace("https://","") def IMAGE_REPO = repositoryAddr def IMAGE_REGISTRY_SERVER = repositoryAddr.replaceAll("/.*","") def credentialId = '' credentialId = "demo1-p1-dockercfg--demo1-p1--demo1-p1-harbor" dir(RELATIVE_DIRECTORY) { container('tools'){ retry(retryCount) { def buildImages = [] withDockerRegistry([credentialsId: credentialId, url: 'https://'+IMAGE_REGISTRY_SERVER]) { def tagswithcomma = "latest,${TIMESTAMP},${GIT_COMMIT}" def tags = tagswithcomma.split(",") def incubatorimage = "${IMAGE_REPO}:${tags[0]}" def image = docker.build(incubatorimage, "-f Dockerfile .") tags.each { tag -> image.tag(tag) image.push(tag) buildImages.add("${IMAGE_REPO}:${tag}" as String) } } alaudaPipeline.appendInfo(STAGE_NAME, [build_image: buildImages], '_Docker') } } } } } pipeline{ agent {label "java"} environment{ ALAUDA_PROJECT = "demo1-p1" } options{ buildDiscarder(logRotator(numToKeepStr: '200')) } stages{ stage("clone"){ steps{ clone_stepsFunc() } } stage("maven"){ steps{ maven_stepsFunc() } } stage("build-docker"){ steps{ build_docker_stepsFunc() } } } }