如何在 gitlab CI 中对合并请求应用 if else 条件?
How to apply if else conditions in gitlab CI on merge requests?
我正在尝试使用 gitlab CI 实现以下可能的流程。因为我将拥有多个分支,例如 (dev-*, release- *, feature- *) 并愿意管理它们。最初我只是想知道我是否可以实现这个流程。我尝试在 gitlabCI 中使用“if”语句,但由于我刚刚开始使用它,所以我无法创建和理解 gitlab 提供的功能。
gitlab中的欲望流CI:
if (MR from feature* to Dev OR MR from feature* to release)
{
echo "Unit Testing";
echo "Code quality analysis";
}
if (feature* MERGE to Dev)
{
echo "Build Docker image with tag $Build_ID";
}
if (feature* MERGE to Release:<release_version>)
{
echo "Build docker image with $release_version"
}
MR
参考合并请求。
feature
、dev
、release
是分支。
我在 another post, which you can check. But in summary, you can use workflow
and rules
to control the execution flow of the pipeline and stages. You can read more about them Gitlab CI workflow and Gitlab CI rules 页上写了一个完整的答案。
在这种情况下,您可以像下面的例子那样使用rules
。仅当以下 if
语句之一评估为真时,跑步者才会触发 test
阶段。
test:
stage: test
script:
- echo "Unit Testing";
- echo "Code quality analysis";
rules:
if: '$CI_MERGE_REQUEST_SOURCE_BRANCH_NAME =~ /^feature/ && $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == dev' # Merge request from feature to dev
if: '$CI_MERGE_REQUEST_SOURCE_BRANCH_NAME =~ /^feature/ && $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == release' # Merge request from feature to release
...
我正在尝试使用 gitlab CI 实现以下可能的流程。因为我将拥有多个分支,例如 (dev-*, release- *, feature- *) 并愿意管理它们。最初我只是想知道我是否可以实现这个流程。我尝试在 gitlabCI 中使用“if”语句,但由于我刚刚开始使用它,所以我无法创建和理解 gitlab 提供的功能。 gitlab中的欲望流CI:
if (MR from feature* to Dev OR MR from feature* to release)
{
echo "Unit Testing";
echo "Code quality analysis";
}
if (feature* MERGE to Dev)
{
echo "Build Docker image with tag $Build_ID";
}
if (feature* MERGE to Release:<release_version>)
{
echo "Build docker image with $release_version"
}
MR
参考合并请求。
feature
、dev
、release
是分支。
我在 another post, which you can check. But in summary, you can use workflow
and rules
to control the execution flow of the pipeline and stages. You can read more about them Gitlab CI workflow and Gitlab CI rules 页上写了一个完整的答案。
在这种情况下,您可以像下面的例子那样使用rules
。仅当以下 if
语句之一评估为真时,跑步者才会触发 test
阶段。
test:
stage: test
script:
- echo "Unit Testing";
- echo "Code quality analysis";
rules:
if: '$CI_MERGE_REQUEST_SOURCE_BRANCH_NAME =~ /^feature/ && $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == dev' # Merge request from feature to dev
if: '$CI_MERGE_REQUEST_SOURCE_BRANCH_NAME =~ /^feature/ && $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == release' # Merge request from feature to release
...