有没有办法自动发布 Visual Studio Code 扩展
通过 Azure DevOps Pipeline?
当然可以!
为了在 Azure Devops 中获得良好的 CI/CD 体验,我建议您将源代码存储在 Azure Devops 或 Github 中。
在构建中,大部分工作是更新 VSIX 清单中的版本,构建\创建包。对于增加的版本,这里我使用了 VSTS 中支持的counter 表达式功能来实现:
counter('name', seed)
在变量声明块中使用这个表达式。详细完整的构建过程,请参考我的示例 YAML 代码:
trigger:
- '*'
pool:
vmImage: 'windows-2019'
variables:
VersionPatch: $[counter('versioncount', 24)]
solution: '**/*.sln'
BuildPlatform: 'Any CPU'
BuildConfiguration: 'Release'
name: 2.0.$(VersionPatch)
steps:
- task: UseDotNet@2
inputs:
packageType: 'sdk'
version: '3.0.100'
includePreviewVersions: true
- task: NuGetToolInstaller@1
inputs:
versionSpec: 5.1.0
- task: PowerShell@2
displayName: Update version
inputs:
filePath: 'Build\VersionUpdate.ps1'
arguments: '$(Build.BuildNumber)'
pwsh: true
- task: NuGetCommand@2
inputs:
command: 'restore'
- task: DotNetCoreCLI@2
displayName:
inputs:
command: 'restore'
projects: 'tests/**/*.csproj'
vstsFeed: '{My feed ID}'
includeNuGetOrg: false
- task: VSBuild@1
inputs:
solution: '**\*.sln'
maximumCpuCount: true
platform: '$(BuildPlatform)'
configuration: '$(BuildConfiguration)'
- task: VSTest@2
inputs:
platform: '$(BuildPlatform)'
configuration: '$(BuildConfiguration)'
- task: CopyFiles@2
inputs:
SourceFolder: '$(Build.SourcesDirectory)'
Contents: |
Build/**
**/*.vsix
**/*.nupkg
README.md
TargetFolder: '$(Build.ArtifactStagingDirectory)'
- task: PublishPipelineArtifact@0
inputs:
artifactName: 'ExtensionDrop'
targetPath: '$(Build.ArtifactStagingDirectory)'
在 UpdateVersion.ps1 文件中:
$VerbosePreference="Continue"
$version = $args[0]
if (!$version) {
$version = "0.0.0"
}
Write-Host "This Version is: $version"
$FullPath = Resolve-Path $PSScriptRoot\..\src\Merlin.Compiler.Vsix\source.vsixmanifest
Write-Host $FullPath
[xml]$content = Get-Content $FullPath
$content.PackageManifest.Metadata.Identity.Version = $version
$content.Save($FullPath)
构建成功后,设置此 repos 的发布管道。在发布时,使用 powershell 脚本和VsixPublisher.exe 发布vsix 文件。
$PAToken = $args[0]
$VsixPath = "$PSScriptRoot\..\src\Merlin.Compiler.Vsix\bin\Release\Merlin.Compiler.Vsix"
$ManifestPath = "$PSScriptRoot\ExtensionManifest.json"
$Installation = & "${env:ProgramFiles(x86)}\Microsoft Visual Studio\Installer\vswhere.exe" -latest -prerelease -format json | ConvertFrom-Json
$Path = $Installation.installationPath
$VsixPublisher = Join-Path -Path $Path -ChildPath "VSSDK\VisualStudioIntegration\Tools\Bin\VsixPublisher.exe" -Resolve
& $VsixPublisher publish -payload $VsixPath -publishManifest $ManifestPath -personalAccessToken $PAToken -ignoreWarnings "VSIXValidatorWarning01,VSIXValidatorWarning02,VSIXValidatorWarning08"
在CD中,使用VS中存在的VsixPublisher.exe来发布vsix文件。
您可以在变量选项卡中设置 PAToken,然后将其设置为机密。因此,它不会对其他人公开。这里的 PAT 代币是必不可少的代币,是其他代币无法替代的。此外,生成令牌时,需要选择所有可访问的组织。否则会导致权限错误。