十、【Pipeline流水线】掌握参数插件化配置、使用Kaniko技术构建镜像 小杨_老师 2025年05月17日 AI大模型 预计阅读 3 分钟 原文 ### 掌握参数插件化配置 字符串/文本/选项/布尔/密码参数等 参数化构建 - 字符串类型的参数`parameters { string(name: 'DEPLOY_ENV', defaultValue: 'staging', description: '') }` - 文本参数,可以包含多行,例如:`parameters { text(name: 'DEPLOY_TEXT', defaultValue: 'One\nTwo\nThree\n', description: '') }` - 布尔参数,例如:`parameters { booleanParam(name: 'DEBUG_BUILD', defaultValue: true, description: '') }` - 选择参数,例如:`parameters { choice(name: 'CHOICES', choices: ['one', 'two', 'three'], description: '') }` - 密码参数,例如:`parameters { password(name: 'PASSWORD', defaultValue: 'SECRET', description: 'A secret password') }` - 扩展选择参数插件 `Extended Choice Parameter` - 参数类型: 单选、多选、复选框、多级单选、多级多选 - 时间参数插件 `Date Parameter Plugin` - 主动选择插件 `Active Choices` - 地址 https://github.com/jenkinsci/active-choices-plugin ```groovy pipeline { agent any parameters { activeChoice choiceType: 'PT_MULTI_SELECT', description: '这是一个构建项目', filterLength: 1, filterable: true, name: 'build_project', randomName: 'choice-parameter-1472486600783191', script: groovyScript(fallbackScript: [classpath: [], oldScript: '', sandbox: true, script: 'return ["error"]'], script: [classpath: [], oldScript: '', sandbox: true, script: 'return ["neo4j","mysql","es"]']) reactiveChoice choiceType: 'PT_SINGLE_SELECT', description: '这是一个giturl地址', filterLength: 1, filterable: false, name: 'giturl', randomName: 'choice-parameter-1472486603282618', referencedParameters: 'build_project', script: groovyScript(fallbackScript: [classpath: [], oldScript: '', sandbox: true, script: 'return ["error"]'], script: [classpath: [], oldScript: '', sandbox: true, script: '''if (build_project.equals("neo4j")){ return ["http://neo4j.git"] } else if (build_project.equals("mysql")){ return ["http://mysql.git"] } ''']) string defaultValue: 'peter', name: 'name' } stages { stage('test') { steps { echo "${build_project} ${giturl}" } } } } ``` ### 使用Kaniko技术构建镜像 Kaniko介绍 - kaniko 的构建上下文与您发送给 Docker 守护进程进行镜像构建的构建上下文非常相似;它代表一个包含 Dockerfile 的目录,kaniko 将使用该文件来构建您的映像。 参数介绍 ```md --cache-copy-layers 设置此标志以缓存复制层。 --cache 设置此标志以--cache=true选择使用 kaniko 进行缓存。 --cache-repo 设置此标志以指定将用于存储缓存层的远程存储库。 --dockerfile 要构建的 dockerfile 的路径。(默认“Dockerfile”) --destination 推送的镜像地址 ``` kaniko使用 ```dockerfile docker run --rm \ -v `pwd`:/workspace \ -v /root/.docker/config.json:/kaniko/.docker/config.json:ro \ gcr.io/kaniko-project/executor:latest \ --dockerfile=Dockerfile \ --destination=${docker_image} \ --cache-copy-layers \ --cache=true \ --cache-repo=${image_prefix} ``` 调试镜像 - kaniko 执行器镜像基于scratch,不包含shell。我们提供了gcr.io/kaniko-project/executor:debug一个调试映像,其中包含 kaniko 执行器映像以及要输入的 busybox shell。 ``` docker run -it --entrypoint=/busybox/sh gcr.io/kaniko-project/executor:debug ``` ```groovy #!groovy pipeline { agent { node { label "slave" } } environment { String year = new Date().format("yyyy") String month = new Date().format("MMdd") String day = new Date().format("HHmm") String second = new Date().format("ss") image_prefix = "registry.cn-hangzhou.aliyuncs.com/tool-bucket/muke" } stages { stage('1. 构建镜像'){ steps{ script{ try{ env.docker_image = "${image_prefix}:main-${year}${month}${day}${second}-${BUILD_ID}" retry(3) { sh """ docker run --rm \ -v `pwd`:/workspace \ -v /root/.docker/config.json:/kaniko/.docker/config.json:ro \ gcr.io/kaniko-project/executor:latest \ --dockerfile=Dockerfile \ --destination=${docker_image} \ --cache-copy-layers \ --cache=true \ --cache-repo=${image_prefix} \ """ } }catch (error){ env.error = sh (returnStdout: true, script: "echo 第1步构建镜像失败:${error}").trim() echo "Caught: ${error}" sh "exit 1" } } } } } } ```
评论区