gitlab - 如何构建基于先前在同一管道中定义的 dotenv 变量的规则
问题描述
我正在管道中的作业中设置一个变量,并在工件/dotenv 中使用它。将变量用作规则中的条件似乎不起作用。如何设置环境变量并在另一个 Job 的规则中使用它?
非常感谢您的想法!
include:
- local: '/gitlab/cicd/.gitlab-ci_test.yml'
stages:
- build
- test
build_rule:
stage: build
rules:
- if: $CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_MESSAGE =~ /R::/m
script:
- echo "Hello World"
- |
echo "RUNTESTS=1" > gitlabcicd.env
artifacts:
reports:
dotenv: gitlabcicd.env
test_always:
stage: test
script:
- echo "TestEnv" $RUNTESTS
# prints TestEnv 1
test_sometimes:
stage: test
rules:
- if: $RUNTESTS
# $RUNTESTS == "1" doesnt work either
script:
- echo "Runs only if variable was set"
# doesnt run, even if it prints above
解决方案
需要在每个作业上加载 .env 文件
"Set variables":
stage: preparation
script:
- echo version=1.1 > varfile
- echo foo=bar >> varfile
artifacts:
paths:
- varfile
Dothings:
stage: run
script:
- load varfile
- echo ${foo}
推荐阅读
- python - 计算一个耦合表的属性
- javascript - Jquery UI Droppable revert不能再次删除
- python - ModuleNotFound 即使文件夹存在
- javascript - 如何在javascript中使用google maps api测量位置距离
- hive - 如何从多列中查找值计数
- firebase - firebase.auth.getRedirectResult 后无法在 Firestore 上创建用户配置文件
- python - 在 python 中,VSCode 调试器不会进入外部代码。无法弄清楚如何在 launch.json 中编辑“justMyCode”
- reactjs - 使用 MDBIcon 时不会出现搜索图标
- binary-tree - 一棵空的二叉树有多少个节点?
- regex - 将正则表达式元字符扩展为字符类