首页 > 解决方案 > 在 Travis yaml 中使用带有变量的 if 条件

问题描述

Travis 允许您在定义条件构建/阶段/作业的条件时使用内置的 Git 属性(例如,branchtagcommit_message等。更多信息在这里)。我有一个未包含在提供的列表中的属性:我想使用 Git 标签来确定一组阶段是否应该运行。因此,如果一个拉取请求上有一个 *release" 标签,那么它将运行额外的阶段,否则它们将被跳过。

我编写了一个脚本,如果包含或不包含发布标签,它将输出真或假,我希望用它来帮助决定一个阶段是否应该运行。这可能与.travis.yaml 有关吗?

本质上,我想知道具有以下设计的 .yaml 是否可以工作:

env:
  - IS_RELEASE = false

before_install:
  - git clone git@github.com:foo/bar.git foobar
  - foobar/check_labels_for_release_tag.sh  ${PR_NUMBER} ${GIT_TOKEN} # this script should set the IS_RELEASE above to either "true" or "false". It will use the Git API to check the PR to see if a label with the name "release" is on it or not.

jobs:
  include:
    - stage: Build
      name: "Build"
      script: foobar/build.sh

    - stage: Test
      name: "Test"
      script: foobar/test.sh

    # the following stage only runs if the IS_RELEASE variable is set to true
    - stage: Release
      name: "Release"
      script: foobar/release.sh
      if: IS_RELEASE = true

标签: continuous-integrationyamltravis-ci

解决方案


推荐阅读