– 代理会话日,2月19日

持续集成

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

自动发布

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

发布命令类似于使用 vsce 从本地环境发布,但你必须以某种方式安全地提供个人访问令牌 (PAT)。 通过将 PAT 存储为 VSCE_PAT 机密变量vsce 将能够使用它。 机密变量绝不会暴露,因此可以在 CI 管道中使用。

Azure Pipelines

Azure Pipelines

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

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

然后,将以下 azure-pipelines.yml 文件添加到扩展存储库的根目录。 除了 Linux 的 xvfb 设置脚本,该脚本对于在无头 Linux CI 机器上运行 VS Code 是必需的,该定义非常简单明了。

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

你可以启用构建在推送到分支时以及在拉取请求上持续运行。 请参阅 构建管道触发器 以了解更多信息。

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,以便在创建标签时也会运行构建
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,以便在创建标签时也会运行构建
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 设置为已屏蔽的变量。
  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.