跳转至

Create custom checks(创建自定义检查)

Custom checks can be created as a Gradle ↗ tasks. These tasks should be added to the appropriate inner build.gradle files, within the language subfolders. Here is an example of a Gradle task that prints Hello World when executed (note that the doLast method creates a task action that runs when the task executes, rather than at configuration time):

// Define custom task
task customTask {
    doLast {
        println "Hello World"
    }
 }

In order for tasks to get executed during CI checks, there must be a CI task that depends on your custom task. This is also defined in the same build.gradle file. In the following example, the check task would be executed after customTask in CI; this is where the Hello World message will appear in the CI logs.

// Add CI dependency on custom task
 project.tasks.check.dependsOn customTask

In order for tasks to get executed at the end of the task list, you would use the following syntax instead.

// Execute custom task after CI checks are published
project.tasks.publish.configure { finalizedBy customTask }

To add dependencies, you can use the following CI tasks: project.tasks.check, project.tasks.test, and project.tasks.publish. We strongly recommend you do not use or rely on other CI tasks (for example, internal tasks) as these are not guaranteed and are subject to change.

More documentation of the functionality provided by Gradle build scripts is available in the Gradle docs ↗.

:::callout{theme="neutral"} Adding custom CI checks to the ci.yml file is not recommended as this file will be overwritten each time the repo is upgraded. :::


中文翻译


创建自定义检查

自定义检查可作为 Gradle 任务创建。这些任务应添加到语言子文件夹内相应的内部 build.gradle 文件中。以下是一个 Gradle 任务示例,执行时会打印 Hello World(请注意,doLast 方法创建的任务操作会在任务执行时运行,而非在配置阶段):

// 定义自定义任务
task customTask {
    doLast {
        println "Hello World"
    }
 }

为了让任务在 CI 检查期间执行,必须有一个 CI 任务依赖于您的自定义任务。这也在同一个 build.gradle 文件中定义。在以下示例中,检查任务将在 CI 中的 customTask 之后执行;此时 Hello World 消息会出现在 CI 日志中。

// 添加对自定义任务的 CI 依赖
 project.tasks.check.dependsOn customTask

若要让任务在任务列表末尾执行,则应改用以下语法:

// 在 CI 检查发布后执行自定义任务
project.tasks.publish.configure { finalizedBy customTask }

要添加依赖项,您可以使用以下 CI 任务:project.tasks.checkproject.tasks.testproject.tasks.publish。我们强烈建议您不要使用或依赖其他 CI 任务(例如内部任务),因为这些任务无法保证稳定性,且可能随时变更。

有关 Gradle 构建脚本提供功能的更多文档,请参阅 Gradle 文档 ↗

:::callout{theme="neutral"} 不建议将自定义 CI 检查添加到 ci.yml 文件中,因为每次仓库升级时该文件都会被覆盖。 :::