git Gitlab 有条件地执行阶段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39988497/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Gitlab execute stage conditionally
提问by deepdive
There are 3 stages - build, test and deploy in .gitlab-ci.yml
.
有 3 个阶段——构建、测试和部署.gitlab-ci.yml
。
A nightly regression test stage needs to be run - well nightly
:)
需要运行每晚的回归测试阶段 - 好吧nightly
:)
Here's the relevant .gitlab-ci.yml
code:
这是相关的.gitlab-ci.yml
代码:
stages:
- build
- test
- deploy
build_project:
stage: build
script:
- cd ./some-dir
- build-script.sh
except:
- tags
#Run this only when say variable 'NIGHTLY_TEST == True'. But HOW?
nightly_regression_test_project:
stage: test
script:
- cd ./some-dir
- execute test-script
Tagging daily to only
run test
stage is not preferable.
每天标记only
运行test
阶段是不可取的。
Any other idea?
还有其他想法吗?
采纳答案by trueCamelType
In case anyone is looking for this now, gitlab has now implemented a scheduled build feature with variable overwriting (incredibly handy). Documentation found here.
如果现在有人正在寻找这个,gitlab 现在已经实现了一个带有变量覆盖的预定构建功能(非常方便)。文档在这里找到。
For anyone interested in the instructions for this feature when this answer was given, here it goes:
对于在给出此答案时对此功能的说明感兴趣的任何人,请看这里:
Using Pipeline Schedules
使用管道计划
In order to schedule a pipeline:
为了调度管道:
- Navigate to your project's Pipelines -> Schedulesand click the New Schedulebutton.
- Fill in the form
- Hit Save pipeline schedulefor the changes to take effect.
- 导航到项目的Pipelines -> Schedules并单击New Schedule按钮。
- 填写表格
- 点击保存管道计划以使更改生效。
My favorite feature of this is the scheduled pipeline variables.
我最喜欢的功能是预定的管道变量。
The variables documentation can be found here, but the most useful information for me was the priority, which I will retype here:
可以在此处找到变量文档,但对我来说最有用的信息是优先级,我将在此处重新输入:
Priority of Variables
变量的优先级
The variables can be overwritten and they take precedence over each other in this order:
变量可以被覆盖,并且它们按以下顺序相互优先:
- Trigger variables or scheduled pipeline variables (take precedence over all)
- Project-level secret variables or protected secret variables
- Group-level secret variables or protected secret variables
- YAML-defined job-level variables
- YAML-defined global variables
- Deployment variables
- Predefined variables (are the lowest in the chain)
- 触发变量或计划管道变量(优先于所有变量)
- 项目级秘密变量或受保护的秘密变量
- 组级秘密变量或受保护的秘密变量
- YAML 定义的作业级变量
- YAML 定义的全局变量
- 部署变量
- 预定义变量(链中最低的)
Hope this helps. I'm delighted that they added this feature.
希望这可以帮助。我很高兴他们添加了此功能。
回答by Andreas Volkmann
except
and only
can specify variables that will trigger them.
except
并且only
可以指定将触发它们的变量。
You can use the following in your .gitlab-ci.yml:
您可以在 .gitlab-ci.yml 中使用以下内容:
build1:
stage: build
script:
- echo "Only when NIGHTLY_TEST is false"
except:
variables:
- $NIGHTLY_TEST
test1:
stage: test
script:
- echo "Only when NIGHTLY_TEST is true"
only:
variables:
- $NIGHTLY_TEST
回答by Connor Shea
There's not currently a way to run a job depending on the environmental variables (you can always open a feature request!). You could use a simple Bash command to immediately exit if the environment variable doesn't exist, though.
目前没有根据环境变量运行作业的方法(您始终可以打开功能请求!)。但是,如果环境变量不存在,您可以使用简单的 Bash 命令立即退出。
Something like:
就像是:
stages:
- build
- test
- deploy
build_project:
stage: build
script:
- cd ./some-dir
- build-script.sh
except:
- tags
# Run this only when NIGHTLY_TEST environment variable exists.
nightly_regression_test_project:
stage: test
script:
- [ -z "$NIGHTLY_TEST" ] && exit 1;
- cd ./some-dir
- execute test-script
If the variable doesn't exist the tests that follow it won't run. Otherwise, they will.
如果变量不存在,它后面的测试将不会运行。否则,他们会。
Hope that helps!
希望有帮助!
回答by frakman1
Select CI/CD -> Schedules on the left frame of your project:
在项目的左侧框架中选择 CI/CD -> Schedules:
Create a new Schedule:
创建一个新的时间表:
Add your NIGHTLY_TEST
variable and set it to True
:
添加您的NIGHTLY_TEST
变量并将其设置为True
:
Add the only
and variables
section to your gitlab-ci.yml
file:
将only
和variables
部分添加到您的gitlab-ci.yml
文件中:
nightly_regression_test_project:
stage: test
script:
- cd ./some-dir
- execute test-script
only:
variables:
- $NIGHTLY_TEST == "True"
-- UPDATE -- Using the new rules based logic, you can do this instead:
-- 更新 -- 使用基于新规则的逻辑,您可以改为执行此操作:
nightly_regression_test_project:
stage: test
script:
- cd ./some-dir
- execute test-script
rules:
- if: $NIGHTLY_TEST == "True"
when: always
回答by D_Almighty
I just implemented this "feature" by following the example found hereUse crontab and curl(I use Linux, because why not?) to set off a trigger to run your nightly tests.
我只是按照此处找到的示例实现了这个“功能” 使用 crontab 和 curl(我使用 Linux,因为为什么不呢?)触发一个触发器来运行你的夜间测试。
30 0 * * * curl --request POST --form token=TOKEN --form ref=master https://gitlab.example.com/api/v3/projects/9/trigger/builds