持续集成

扩展集成测试可以在持续集成(CI)服务上运行。@vscode/test-electron 库可帮助你在 CI 提供程序上设置扩展测试,并包含一个在 Azure Pipelines 上设置的示例扩展。你可以查看该构建流水线,或直接跳转到 azure-pipelines.yml 文件

自动发布

你还可以配置 CI 以自动发布扩展的新版本。

发布命令类似于使用 vsce 从本地环境进行发布,但你必须以安全的方式提供个人访问令牌(PAT)。通过将 PAT 存储为 VSCE_PAT 机密变量(secret variable)vsce 将能够使用它。机密变量不会被泄露,因此在 CI 流水线中使用它们是安全的。

Azure Pipelines

Azure Pipelines

Azure Pipelines 非常适合运行 VS Code 扩展测试,因为它支持在 Windows、macOS 和 Linux 上运行测试。对于开源项目,你可以获得无限的构建时长和 10 个免费的并行作业。本节介绍如何设置 Azure Pipelines 以运行你的扩展测试。

首先,在 Azure DevOps 上创建一个免费帐户,并为你的扩展创建一个 Azure DevOps 项目

然后,将以下 azure-pipelines.yml 文件添加到扩展仓库的根目录中。除了 Linux 上运行 VS Code 的无头(headless)CI 机器所必需的 xvfb 设置脚本外,该定义非常直接。

trigger:
  branches:
    include:
    - main
  tags:
    include:
    - v*

strategy:
  matrix:
    linux:
      imageName: 'ubuntu-latest'
    mac:
      imageName: 'macos-latest'
    windows:
      imageName: 'windows-latest'

pool:
  vmImage: $(imageName)

steps:

- task: NodeTool@0
  inputs:
    versionSpec: '10.x'
  displayName: 'Install Node.js'

- bash: |
    /usr/bin/Xvfb :99 -screen 0 1024x768x24 > /dev/null 2>&1 &
    echo ">>> Started xvfb"
  displayName: Start xvfb
  condition: and(succeeded(), eq(variables['Agent.OS'], 'Linux'))

- bash: |
    echo ">>> Compile vscode-test"
    yarn && yarn compile
    echo ">>> Compiled vscode-test"
    cd sample
    echo ">>> Run sample integration test"
    yarn && yarn compile && yarn test
  displayName: Run Tests
  env:
    DISPLAY: ':99.0'

最后,在你的 DevOps 项目中创建一个新的流水线,并将其指向 azure-pipelines.yml 文件。触发构建,这就完成了!

pipelines

你可以启用构建以在推送到分支甚至提交拉取请求(pull request)时持续运行。有关详细信息,请参阅构建流水线触发器

Azure Pipelines 自动发布

  1. 使用 Azure DevOps 机密变量说明VSCE_PAT 设置为机密变量。
  2. 安装 vsce 作为 devDependencies(使用 npm install @vscode/vsce --save-devyarn add @vscode/vsce --dev)。
  3. package.json 中声明一个 deploy 脚本,不要包含 PAT(默认情况下,vsce 会将 VSCE_PAT 环境变量用作个人访问令牌)。
"scripts": {
  "deploy": "vsce publish --yarn"
}
  1. 配置 CI,以便在创建标签(tag)时也运行构建。
trigger:
  branches:
    include:
    - main
  tags:
    include:
    - refs/tags/v*
  1. azure-pipelines.yml 中添加一个 publish 步骤,使用机密变量调用 yarn deploy
- bash: |
    echo ">>> Publish"
    yarn deploy
  displayName: Publish
  condition: and(succeeded(), startsWith(variables['Build.SourceBranch'], 'refs/tags/'), eq(variables['Agent.OS'], 'Linux'))
  env:
    VSCE_PAT: $(VSCE_PAT)

condition 属性告诉 CI 仅在特定情况下运行发布步骤。

在我们的示例中,条件包含三个检查:

  • succeeded() - 仅在测试通过时发布。
  • startsWith(variables['Build.SourceBranch'], 'refs/tags/') - 仅在标记(发布)构建时发布。
  • eq(variables['Agent.OS'], 'Linux') - 如果你的构建在多个代理(Windows、Linux 等)上运行,请包含此项。如果不是,请删除条件的该部分。

由于 VSCE_PAT 是一个机密变量,它不能直接作为环境变量使用。因此,我们需要显式地将环境变量 VSCE_PAT 映射到机密变量。

GitHub Actions

你也可以配置 GitHub Actions 来运行你的扩展 CI。在无头 Linux CI 机器中,需要 xvfb 才能运行 VS Code,因此如果当前操作系统是 Linux,请在启用了 Xvfb 的环境中运行测试。

on:
  push:
    branches:
      - main

jobs:
  build:
    strategy:
      matrix:
        os: [macos-latest, ubuntu-latest, windows-latest]
    runs-on: ${{ matrix.os }}
    steps:
    - name: Checkout
      uses: actions/checkout@v4
    - name: Install Node.js
      uses: actions/setup-node@v4
      with:
        node-version: 18.x
    - run: npm install
    - run: xvfb-run -a npm test
      if: runner.os == 'Linux'
    - run: npm test
      if: runner.os != 'Linux'

GitHub Actions 自动发布

  1. 使用 GitHub Actions 机密说明VSCE_PAT 设置为加密机密。
  2. 安装 vsce 作为 devDependencies(使用 npm install @vscode/vsce --save-devyarn add @vscode/vsce --dev)。
  3. package.json 中声明一个 deploy 脚本,不要包含 PAT。
"scripts": {
  "deploy": "vsce publish --yarn"
}
  1. 配置 CI,以便在创建标签(tag)时也运行构建。
on:
  push:
    branches:
    - main
  release:
    types:
    - created
  1. 在流水线中添加一个 publish 作业,使用机密变量调用 npm run deploy
- name: Publish
  if: success() && startsWith(github.ref, 'refs/tags/') && matrix.os == 'ubuntu-latest'
  run: npm run deploy
  env:
    VSCE_PAT: ${{ secrets.VSCE_PAT }}

if 属性告诉 CI 仅在特定情况下运行发布步骤。

在我们的示例中,条件包含三个检查:

  • success() - 仅在测试通过时发布。
  • startsWith(github.ref, 'refs/tags/') - 仅在标记(发布)构建时发布。
  • matrix.os == 'ubuntu-latest' - 如果你的构建在多个代理(Windows、Linux 等)上运行,请包含此项。如果不是,请删除条件的该部分。

GitLab CI

GitLab CI 可用于在无头 Docker 容器中测试和发布扩展。这可以通过拉取预配置的 Docker 镜像,或在流水线期间安装 xvfb 及运行 Visual Studio Code 所需的库来完成。

image: node:12-buster

before_script:
  - npm install

test:
  script:
    - |
      apt update
      apt install -y libasound2 libgbm1 libgtk-3-0 libnss3 xvfb
      xvfb-run -a npm run test

GitLab CI 自动发布

  1. 使用 GitLab CI 文档VSCE_PAT 设置为屏蔽变量(masked variable)。
  2. 安装 vsce 作为 devDependencies(使用 npm install @vscode/vsce --save-devyarn add @vscode/vsce --dev)。
  3. package.json 中声明一个 deploy 脚本,不要包含 PAT。
"scripts": {
  "deploy": "vsce publish --yarn"
}
  1. 添加一个 deploy 作业,使用屏蔽变量调用 npm run deploy,该作业将仅在标签上触发。
deploy:
  only:
    - tags
  script:
    - npm run deploy

常见问题

我需要为持续集成使用 Yarn 吗?

上述所有示例都引用了一个使用 Yarn 构建的假设项目,但它们同样可以改编为使用 npmGruntGulp 或任何其他 JavaScript 构建工具。

© . This site is unofficial and not affiliated with Microsoft.