持续集成
Deno 的内置工具使您能够轻松为项目设置持续集成(CI)流水线。您可以使用相应的命令 deno test
、deno lint
和 deno fmt
来测试、代码检查和格式化代码。此外,您还可以在流水线中通过 deno coverage
从测试结果生成代码覆盖率报告。
设置基本流水线 跳转到标题
您可以在 GitHub Actions 中为 Deno 项目设置基本流水线。本页解释的概念也大体适用于其他 CI 提供商,例如 Azure Pipelines、CircleCI 或 GitLab。
为 Deno 构建流水线通常从检出仓库和安装 Deno 开始
name: Build
on: push
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: denoland/setup-deno@v2
with:
deno-version: v2.x # Run with latest stable Deno.
要扩展工作流,请添加您可能需要的任何 deno
子命令
# Check if the code is formatted according to Deno's default
# formatting conventions.
- run: deno fmt --check
# Scan the code for syntax errors and style issues. If
# you want to use a custom linter configuration you can add a configuration file with --config
- run: deno lint
# Run all test files in the repository and collect code coverage. The example
# runs with all permissions, but it is recommended to run with the minimal permissions your program needs (for example --allow-read).
- run: deno test --allow-all --coverage=cov/
# This generates a report from the collected coverage in `deno test --coverage`. It is
# stored as a .lcov file which integrates well with services such as Codecov, Coveralls and Travis CI.
- run: deno coverage --lcov cov/ > cov.lcov
跨平台工作流 跳转到标题
作为 Deno 模块维护者,您可能希望确保您的代码在当今所有主流操作系统(Linux、MacOS 和 Windows)上都能正常运行。通过运行并行作业矩阵可以实现跨平台工作流,每个作业在不同的操作系统上运行构建。
jobs:
build:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
steps:
- run: deno test --allow-all --coverage cov/
注意:GitHub Actions 在处理 Windows 风格的行尾(CRLF)方面存在已知问题。这可能会导致在 Windows 上运行的作业的流水线中运行 deno fmt
时出现问题。为防止此问题,请在运行 actions/checkout@v4
步骤之前,将 Actions 运行器配置为使用 Linux 风格的行尾。
git config --system core.autocrlf false
git config --system core.eol lf
如果您正在使用实验性或不稳定的 Deno API,您可以包含一个运行 Deno canary 版本的矩阵作业。这有助于及早发现破坏性更改。
jobs:
build:
runs-on: ${{ matrix.os }}
continue-on-error: ${{ matrix.canary }} # Continue in case the canary run does not succeed
strategy:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
deno-version: [v1.x]
canary: [false]
include:
- deno-version: canary
os: ubuntu-latest
canary: true
加速 Deno 流水线 跳转到标题
减少重复 跳转到标题
在跨平台运行中,流水线的某些步骤不一定需要在每个操作系统上都运行。例如,在 Linux、MacOS 和 Windows 上生成相同的测试覆盖率报告会有些多余。在这种情况下,您可以使用 GitHub Actions 的 if
条件关键字。下面的示例展示了如何仅在 ubuntu
(Linux) 运行器上运行代码覆盖率生成和上传步骤。
- name: Generate coverage report
if: matrix.os == 'ubuntu-latest'
run: deno coverage --lcov cov > cov.lcov
- name: Upload coverage to Coveralls.io
if: matrix.os == 'ubuntu-latest'
# Any code coverage service can be used, Coveralls.io is used here as an example.
uses: coverallsapp/github-action@master
with:
github-token: ${{ secrets.GITHUB_TOKEN }} # Generated by GitHub.
path-to-lcov: cov.lcov
缓存依赖项 跳转到标题
随着项目规模的增长,通常会包含越来越多的依赖项。Deno 会在测试期间下载这些依赖项,如果工作流每天运行多次,这可能会成为一个耗时的过程。一个常见的提速解决方案是缓存依赖项,这样它们就不需要重新下载。
Deno 将依赖项存储在本地缓存目录中。在流水线中,通过在 denoland/setup-deno
上启用 cache: true
选项,可以在工作流之间保留缓存。
steps:
- uses: actions/checkout@v4
- uses: denoland/setup-deno@v2
with:
cache: true
起初,当此工作流运行时,缓存仍然是空的,deno test
等命令仍然需要下载依赖项,但当作业成功时,缓存的依赖项内容会保存下来,后续的任何运行都可以从缓存中恢复它们,而无需重新下载。
为了演示,假设您有一个项目使用了来自 @std/log
的日志记录器
{
"imports": {
"@std/log": "jsr:@std/log@0.224.5"
}
}
为了增加此版本,您可以更新依赖项,然后重新加载缓存并在本地更新锁文件。
deno install --reload --frozen=false
运行此操作后,您应该会看到锁文件内容的更改。当此更改被提交并通过流水线运行时,您应该会看到新的缓存,并在随后的任何运行中使用它。
默认情况下,缓存会自动根据以下内容进行键控:
- GitHub job_id
- 运行器操作系统和架构
- 项目中
deno.lock
文件的哈希值
可以通过 cache-hash
输入自定义用作缓存键一部分的默认哈希值(${{ hashFiles('**/deno.lock') }}
)。
- uses: denoland/setup-deno@v2
with:
# setting `cache-hash` implies `cache: true` and will replace
# the default cache-hash of `${{ hashFiles('**/deno.lock') }}`
cache-hash: ${{ hashFiles('**/deno.json') }}