持续集成
扩展集成测试可以在 CI 服务上运行。@vscode/test-electron
库帮助你在 CI 提供商上设置扩展测试,并包含 Azure Pipelines 上的示例扩展设置。你可以查看 构建管道 或直接跳转到 azure-pipelines.yml
文件。
自动发布
你还可以配置 CI 以自动发布扩展的新版本。
发布命令类似于使用 vsce
从本地环境发布,但你必须以某种安全的方式提供个人访问令牌 (PAT)。通过将 PAT 存储为 VSCE_PAT
密钥变量,vsce
将能够使用它。密钥变量永远不会公开,因此在 CI 管道中使用它们是安全的。
Azure Pipelines
Azure Pipelines 非常适合运行 VS Code 扩展测试,因为它支持在 Windows、macOS 和 Linux 上运行测试。对于开源项目,你将获得无限分钟数和 10 个免费并行作业。本节介绍如何设置 Azure Pipelines 以运行你的扩展测试。
首先,在 Azure DevOps 上创建一个免费帐户,并为你的扩展创建一个 Azure DevOps 项目。
然后,将以下 azure-pipelines.yml
文件添加到你的扩展仓库的根目录。除了在无头 Linux CI 机器中运行 VS Code 所必需的 Linux 的 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
文件。触发构建,瞧!
你可以启用构建,使其在推送到分支时甚至在拉取请求时持续运行。请参阅 构建管道触发器 了解更多信息。
Azure Pipelines 自动发布
- 使用 Azure DevOps 密钥说明将
VSCE_PAT
设置为密钥变量。 - 将
vsce
安装为devDependencies
(npm install @vscode/vsce --save-dev
或yarn add @vscode/vsce --dev
)。 - 在
package.json
中声明一个不带 PAT 的deploy
脚本(默认情况下,vsce
将使用VSCE_PAT
环境变量作为个人访问令牌)。
"scripts": {
"deploy": "vsce publish --yarn"
}
- 配置 CI,以便在创建标签时也运行构建
trigger:
branches:
include:
- main
tags:
include:
- refs/tags/v*
- 在
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 仅在某些情况下运行发布步骤。
在我们的示例中,condition 有三个检查
succeeded()
- 仅当测试通过时发布。startsWith(variables['Build.SourceBranch'], 'refs/tags/')
- 仅当标记(发布)构建时发布。eq(variables['Agent.OS'], 'Linux')
- 如果你的构建在多个代理(Windows、Linux 等)上运行,则包含此项。否则,删除 condition 的那部分。
由于 VSCE_PAT
是一个密钥变量,因此它不能立即用作环境变量。因此,我们需要显式地将环境变量 VSCE_PAT
映射到密钥变量。
GitHub Actions
你还可以配置 GitHub Actions 来运行你的扩展 CI。在无头 Linux CI 机器中,运行 VS Code 需要 xvfb
,因此如果 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 自动发布
- 使用 GitHub Actions 密钥说明将
VSCE_PAT
设置为加密密钥。 - 将
vsce
安装为devDependencies
(npm install @vscode/vsce --save-dev
或yarn add @vscode/vsce --dev
)。 - 在
package.json
中声明一个不带 PAT 的deploy
脚本。
"scripts": {
"deploy": "vsce publish --yarn"
}
- 配置 CI,以便在创建标签时也运行构建
on:
push:
branches:
- main
release:
types:
- created
- 向管道添加一个
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 仅在某些情况下运行发布步骤。
在我们的示例中,condition 有三个检查
success()
- 仅当测试通过时发布。startsWith(github.ref, 'refs/tags/')
- 仅当标记(发布)构建时发布。matrix.os == 'ubuntu-latest'
- 如果你的构建在多个代理(Windows、Linux 等)上运行,则包含此项。否则,删除 condition 的那部分。
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 自动发布
- 使用 GitLab CI 文档将
VSCE_PAT
设置为掩码变量。 - 将
vsce
安装为devDependencies
(npm install @vscode/vsce --save-dev
或yarn add @vscode/vsce --dev
)。 - 在
package.json
中声明一个不带 PAT 的deploy
脚本。
"scripts": {
"deploy": "vsce publish --yarn"
}
- 添加一个
deploy
作业,该作业使用掩码变量调用npm run deploy
,该作业仅在标签上触发。
deploy:
only:
- tags
script:
- npm run deploy
常见问题
我需要在持续集成中使用 Yarn 吗?
以上所有示例都指的是使用 Yarn 构建的假设项目,但可以调整为使用 npm、Grunt、Gulp 或任何其他 JavaScript 构建工具。