continuous-integration - 在 IF 语句中使用 Bitbucket 的 $BITBUCKET_TAG 变量
问题描述
我整天都在挠头试图弄清楚这一点。有人可以指出我为什么
- if [ $BITBUCKET_BRANCH == 'master' ]; echo "Do master branch stuff"; fi
如果我推送到的分支是master
.
但是当我试图通过标签来区分它时
- if [ $BITBUCKET_TAG == 'test-*' ]; then echo "Do test tag stuff"; fi
它被完全忽略,好像if
语句中的代码永远不会到达。
我究竟做错了什么?我尝试以多种方式更改语句,尝试使用正则表达式等均无济于事。任何帮助将不胜感激。
这是一个可重现的示例管道代码:
image: node:12.16.0
options:
docker: true
definitions:
steps:
- step: &if-test
name: If test
script:
- if [ $BITBUCKET_BRANCH == 'master' ]; then echo "Do master branch stuff"; fi
- if [ $BITBUCKET_TAG == 'test-*' ]; then echo "Do test tag stuff"; fi
- if [ $BITBUCKET_TAG == 'staging-*' ]; then echo "Do staging tag stuff"; fi
pipelines:
branches:
master:
- step: *if-test
tags:
'test-*':
- step: *if-test
'staging-*':
- step: *if-test
解决方案
问题是您编写"if"
语句的方式:
if [ $BITBUCKET_TAG == 'test-*' ];
这是一个 bash/unixif
语句,它将检查一个文本字符串"test-*"
作为分支名称,您可能不会使用它。
您应该使用“字符串包含”测试而不是“字符串等于”测试,如下所示:
if [[ $BITBUCKET_TAG == *"test-"* ]];
还要注意这里的yml
用法'test-*'
...
tags:
'test-*':
... 与 bash/shell 脚本的解释方式不同'test-*'
。
推荐阅读
- c++ - 为什么在某些情况下会发生名称隐藏而不是重新定义?
- javascript - 打字稿错误 2769 - 尝试将流程信息传递给数据状态以在模式上使用它
- python - 无需人工交互即可使用日历 API
- python-3.x - 使用通用和异常联合键入 Python 方法包装函数的最佳实践
- reactjs - 新反应。即时通讯有导航栏的问题。js代码没有响应
- angular - 通过 Angular ngFor 指令中的索引访问数组的值
- templates - Ember Octane - 在模板中显示来自控制器的跟踪数组的内容?
- php - 如何在 Walker Nav 中包含第二层子菜单?
- java - 如何将多个组件 (WebElements) 包装成一个组件?组件对象模型和页面对象模型
- cmake - CMake 文件(生成输出
[...]) 如何在多行上写 CONTENT