02.CI/CD:Jenkins pipeline 小教程-在pipeline中使用变量

在编写pipeline中,变量是不可缺少的,这节,写一个简单的脚本来调用变量。

我们在第一节中编写了第一个简单的脚本。 内容如下:

node {
   echo 'Hello World'
}

接下来,我们会通过修改这个脚本来完成随后的一些步骤。

1. 变量定义(全局)

增加全局的一个变量DEPLOY_ENVIRONMENT.

def DEPLOY_ENVIRONMENT = "dev"
node {
   echo "My Deploy Environment is: ${DEPLOY_ENVIRONMENT}"
}

如下图:
在这里插入图片描述

运行结果:
在这里插入图片描述

看到,我们已经顺利的调用了变量。

2. 通过变量判断

在这一节中,会判断设置的变量,并且将多个变量进行组合。

def DEPLOY_ENVIRONMENT = "dev"
def DEPLOY_VER = "1.1"
if (DEPLOY_ENVIRONMENT == "dev") {
    CURRENT_ENVIRONMENT="development"
} else if (DEPLOY_ENVIRONMENT == "test"){
    CURRENT_ENVIRONMENT="test"
}
def CURRENT_VER = "${CURRENT_ENVIRONMENT}-${DEPLOY_VER}"
node {
   echo "当前的部署环境为:${CURRENT_ENVIRONMENT}"
   echo "当前的部署版本为:${CURRENT_VER}"
}

我们先看下执行,再来讲解上面的脚本。
在这里插入图片描述

在这里插入图片描述

看下这里面的说明:

// 变量定义
def DEPLOY_ENVIRONMENT = "dev"
// 变量定义
def DEPLOY_VER = "1.1"
// if对变量进行判断。
if (DEPLOY_ENVIRONMENT == "dev") {
    CURRENT_ENVIRONMENT="development"
    // else if 的写法。
} else if (DEPLOY_ENVIRONMENT == "test"){
    CURRENT_ENVIRONMENT="test"
}
// 变量定义
def CURRENT_VER = "${CURRENT_ENVIRONMENT}-${DEPLOY_VER}"

node {
   echo "当前的部署环境为:${CURRENT_ENVIRONMENT}"
   echo "当前的部署版本为:${CURRENT_VER}"
}

变量差不多就这么多,如果要了解更深入的,可以查看pipeline相关的文档。
当然,我们在后面的小教程中,也会慢慢讲。



如果有什么问题,可以加入QQ群进行讨论。QQ群:839421316

谢谢支持。
在这里插入图片描述

Started by user administrator Obtained uat_tc/frontend/Jenkinsfile from git http://47.100.45.4:8060/pipeline/dbm-ci.git [Pipeline] Start of Pipeline [Pipeline] node Running on Jenkins in /var/jenkins_home/workspace/prd-frontend-dbm-provider-miniapp-ext [Pipeline] { [Pipeline] stage [Pipeline] { (Declarative: Checkout SCM) [Pipeline] checkout The recommended git tool is: NONE using credential dbm-deploy > git rev-parse --resolve-git-dir /var/jenkins_home/workspace/prd-frontend-dbm-provider-miniapp-ext/.git # timeout=10 Fetching changes from the remote Git repository > git config remote.origin.url http://47.100.45.4:8060/pipeline/dbm-ci.git # timeout=10 Fetching upstream changes from http://47.100.45.4:8060/pipeline/dbm-ci.git > git --version # timeout=10 > git --version # 'git version 2.39.5' using GIT_ASKPASS to set credentials > git fetch --tags --force --progress -- http://47.100.45.4:8060/pipeline/dbm-ci.git +refs/heads/*:refs/remotes/origin/* # timeout=10 > git rev-parse refs/remotes/origin/master^{commit} # timeout=10 Checking out Revision 39c6e6fb53f67b249d725128e27fe95d10d8c634 (refs/remotes/origin/master) > git config core.sparsecheckout # timeout=10 > git checkout -f 39c6e6fb53f67b249d725128e27fe95d10d8c634 # timeout=10 Commit message: "前端部署脚本修改" > git rev-list --no-walk 39c6e6fb53f67b249d725128e27fe95d10d8c634 # timeout=10 [Pipeline] } [Pipeline] // stage [Pipeline] withEnv [Pipeline] { [Pipeline] stage [Pipeline] { (Declarative: Tool Install) [Pipeline] tool [Pipeline] envVarsForTool [Pipeline] tool [Pipeline] envVarsForTool [Pipeline] } [Pipeline] // stage [Pipeline] withEnv [Pipeline] { [Pipeline] stage [Pipeline] { (Checkout) [Pipeline] tool [Pipeline] envVarsForTool [Pipeline] tool [Pipeline] envVarsForTool [Pipeline] withEnv [Pipeline] { [Pipeline] git The recommended git tool is: NONE using credential dbm-deploy > git rev-parse --resolve-git-dir /var/jenkins_home/workspace/prd-frontend-dbm-provider-miniapp-ext/.git # timeout=10 Fetching changes from the remote Git repository > git config remote.origin.url http://47.100.45.4:8060/ifourthwall-frontend/dbm-provider-miniapp-ext.git # timeout=10 Fetching upstream changes from http://47.100.45.4:8060/ifourthwall-frontend/dbm-provider-miniapp-ext.git > git --version # timeout=10 > git --version # 'git version 2.39.5' using GIT_ASKPASS to set credentials > git fetch --tags --force --progress -- http://47.100.45.4:8060/ifourthwall-frontend/dbm-provider-miniapp-ext.git +refs/heads/*:refs/remotes/origin/* # timeout=10 > git rev-parse refs/remotes/origin/uat^{commit} # timeout=10 Checking out Revision d96cc2d276749987f956838156865e2c99d68c70 (refs/remotes/origin/uat) > git config core.sparsecheckout # timeout=10 > git checkout -f d96cc2d276749987f956838156865e2c99d68c70 # timeout=10 > git branch -a -v --no-abbrev # timeout=10 > git checkout -b uat d96cc2d276749987f956838156865e2c99d68c70 # timeout=10 Commit message: "合并分支 'test' 到 'uat'" First time build. Skipping changelog. [Pipeline] } [Pipeline] // withEnv [Pipeline] } [Pipeline] // stage [Pipeline] stage [Pipeline] { (Build) [Pipeline] tool [Pipeline] envVarsForTool [Pipeline] tool [Pipeline] envVarsForTool [Pipeline] withEnv [Pipeline] { [Pipeline] script [Pipeline] { [Pipeline] sh + rm -rf node_modules + node -v v18.20.3 + git remote set-url origin https://github.com/evrone/postcss-px-to-viewport.git + npm install --registry=https://registry.npmmirror.com npm warn deprecated yaeti@0.0.6: Package no longer supported. Contact Support at https://www.npmjs.com/support for more info. added 346 packages in 2s 77 packages are looking for funding run `npm fund` for details + npm run build:uat > cssccrane@0.0.0 build:uat > vite build --mode uat postcss-px-to-viewport: postcss.plugin was deprecated. Migration guide: https://evilmartians.com/chronicles/postcss-8-plugin-migration vite v5.4.20 building for uat... transforming... DEPRECATION WARNING [legacy-js-api]: The legacy JS API is deprecated and will be removed in Dart Sass 2.0.0. More info: https://sass-lang.com/d/legacy-js-api DEPRECATION WARNING [import]: Sass @import rules are deprecated and will be removed in Dart Sass 3.0.0. More info and automated migrator: https://sass-lang.com/d/import ╷ 1 │ @use "@/assets/css/constant.scss" as *;@import "./base.scss"; │ ^^^^^^^^^^^^^ ╵ src/assets/css/index.scss 1:48 root stylesheet DEPRECATION WARNING [import]: Sass @import rules are deprecated and will be removed in Dart Sass 3.0.0. More info and automated migrator: https://sass-lang.com/d/import ╷ 2 │ @import "./constant.scss"; │ ^^^^^^^^^^^^^^^^^ ╵ src/assets/css/index.scss 2:9 root stylesheet DEPRECATION WARNING [legacy-js-api]: The legacy JS API is deprecated and will be removed in Dart Sass 2.0.0. More info: https://sass-lang.com/d/legacy-js-api DEPRECATION WARNING [legacy-js-api]: The legacy JS API is deprecated and will be removed in Dart Sass 2.0.0. More info: https://sass-lang.com/d/legacy-js-api DEPRECATION WARNING [legacy-js-api]: The legacy JS API is deprecated and will be removed in Dart Sass 2.0.0. More info: https://sass-lang.com/d/legacy-js-api DEPRECATION WARNING [legacy-js-api]: The legacy JS API is deprecated and will be removed in Dart Sass 2.0.0. More info: https://sass-lang.com/d/legacy-js-api DEPRECATION WARNING [legacy-js-api]: The legacy JS API is deprecated and will be removed in Dart Sass 2.0.0. More info: https://sass-lang.com/d/legacy-js-api DEPRECATION WARNING [legacy-js-api]: The legacy JS API is deprecated and will be removed in Dart Sass 2.0.0. More info: https://sass-lang.com/d/legacy-js-api DEPRECATION WARNING [legacy-js-api]: The legacy JS API is deprecated and will be removed in Dart Sass 2.0.0. More info: https://sass-lang.com/d/legacy-js-api DEPRECATION WARNING [legacy-js-api]: The legacy JS API is deprecated and will be removed in Dart Sass 2.0.0. More info: https://sass-lang.com/d/legacy-js-api DEPRECATION WARNING [legacy-js-api]: The legacy JS API is deprecated and will be removed in Dart Sass 2.0.0. More info: https://sass-lang.com/d/legacy-js-api DEPRECATION WARNING [legacy-js-api]: The legacy JS API is deprecated and will be removed in Dart Sass 2.0.0. More info: https://sass-lang.com/d/legacy-js-api DEPRECATION WARNING [legacy-js-api]: The legacy JS API is deprecated and will be removed in Dart Sass 2.0.0. More info: https://sass-lang.com/d/legacy-js-api DEPRECATION WARNING [legacy-js-api]: The legacy JS API is deprecated and will be removed in Dart Sass 2.0.0. More info: https://sass-lang.com/d/legacy-js-api DEPRECATION WARNING [legacy-js-api]: The legacy JS API is deprecated and will be removed in Dart Sass 2.0.0. More info: https://sass-lang.com/d/legacy-js-api ✓ 430 modules transformed. Generated an empty chunk: "@antfu". Generated an empty chunk: "vue". Generated an empty chunk: "vue-demi". x Build failed in 1.87s error during build: [vite:esbuild-transpile] Transform failed with 2 errors: assets/vant-!~{00b}~.js:2878:6: ERROR: The symbol "bem" has already been declared assets/vant-!~{00b}~.js:5104:6: ERROR: The symbol "bem" has already been declared  The symbol "bem" has already been declared 2876| const Tabs = withInstall(stdin_default$f); 2877| 2878| const [name$c, bem] = createNamespace("picker-group"); | ^ 2879| const PICKER_GROUP_KEY = Symbol(name$c); 2880| extend({ The symbol "bem" has already been declared 5102| const Notify = withInstall(stdin_default$2); 5103| 5104| const [name$1, bem, t] = createNamespace("search"); | ^ 5105| const searchProps = extend({}, fieldSharedProps, { 5106| label: String,  at failureErrorWithLog (/var/jenkins_home/workspace/prd-frontend-dbm-provider-miniapp-ext/node_modules/esbuild/lib/main.js:1472:15) at /var/jenkins_home/workspace/prd-frontend-dbm-provider-miniapp-ext/node_modules/esbuild/lib/main.js:755:50 at responseCallbacks.<computed> (/var/jenkins_home/workspace/prd-frontend-dbm-provider-miniapp-ext/node_modules/esbuild/lib/main.js:622:9) at handleIncomingPacket (/var/jenkins_home/workspace/prd-frontend-dbm-provider-miniapp-ext/node_modules/esbuild/lib/main.js:677:12) at Socket.readFromStdout (/var/jenkins_home/workspace/prd-frontend-dbm-provider-miniapp-ext/node_modules/esbuild/lib/main.js:600:7) at Socket.emit (node:events:517:28) at addChunk (node:internal/streams/readable:368:12) at readableAddChunk (node:internal/streams/readable:341:9) at Readable.push (node:internal/streams/readable:278:10) at Pipe.onStreamRead (node:internal/stream_base_commons:190:23) [Pipeline] } [Pipeline] // script [Pipeline] } [Pipeline] // withEnv [Pipeline] } [Pipeline] // stage [Pipeline] stage [Pipeline] { (Deploy) Stage "Deploy" skipped due to earlier failure(s) [Pipeline] getContext [Pipeline] } [Pipeline] // stage [Pipeline] } [Pipeline] // withEnv [Pipeline] } [Pipeline] // withEnv [Pipeline] } [Pipeline] // node [Pipeline] End of Pipeline ERROR: script returned exit code 1 Finished: FAILURE
最新发布
09-13
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值