【问题标题】:Publishing VS Code extension via Azure DevOps通过 Azure DevOps 发布 VS Code 扩展
【发布时间】:2019-10-01 21:41:25
【问题描述】:

阅读VSCode Publish Extension 文档后,我成功地使用vsce 手动发布了一个VSCode 扩展。

我想知道是否有办法通过 Azure DevOps 管道(构建或发布)自动发布扩展,而不是手动发布。

我尝试在那里使用vsce,但遇到了身份验证错误

资源不可用于匿名访问。需要客户端身份验证。

使用vsce publish -p <access_token> 是不可能的,因为管道是公开的,每个人都可以看到访问令牌...

那么,有没有办法通过 Azure DevOps Pipeline 甚至 Travis CI 自动发布 Visual Studio Code 扩展?

【问题讨论】:

  • 现在是官方文档的一部分 - code.visualstudio.com/api/working-with-extensions/…
  • @MoshFeu 有什么理由在文档中使用构建管道而不是 Azure DevOps 的发布管道发布扩展?
  • @MoshFeu 我用发布管道(而不是构建)尝试了你的步骤。一切正常,但是当我添加一个依赖项时,它开始在yarn deploy 中失败,并出现错误Command failed: npm list --production --parseable --depth=99999(我的依赖项丢失了)。我该如何解决这个问题?
  • 其实我从来没有尝试过使用发布管道。我会尝试并带着我的结论返回

标签: visual-studio-code azure-devops azure-pipelines vscode-extensions


【解决方案1】:

您可以将个人访问令牌添加为秘密变量,然后任何人都看不到它。

转到 Azure DevOps 到您的管道并单击“编辑”,而不是在左上角单击“变量”:

现在单击+ 图标并添加变量,选中复选框“将此值保密”:

现在可以这样使用了:$(PAT),例如:

vsce publish -p $(PAT)

变量值不会出现在 YAML 中:)

【讨论】:

    【解决方案2】:

    有没有办法自动发布 Visual Studio Code 扩展 通过 Azure DevOps Pipeline?

    当然可以!

    为了在 Azure Devops 中获得良好的 CI/CD 体验,我建议您将源代码存储在 Azure Devops 或 Github 中。

    • 构建\CI

    在构建中,大部分工作是更新 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)
    
    • 发行\CD

    构建成功后,设置此 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 代币是必不可少的代币,是其他代币无法替代的。此外,生成令牌时,需要选择所有可访问的组织。否则会导致权限错误。

    【讨论】:

      【解决方案3】:

      进一步@Shayki 的回答还有一些步骤,因为你不能只运行vsce publish -p $(PAT)

      1. 应该安装vsce(可以在devDependencies
      2. package.json 脚本中添加一个“部署”(或您喜欢的名称)脚本。
      "deploy": "vsce publish -p"
      
      1. azure-pipeline.yml 文件中添加“发布”步骤。 condition 仅用于在 master 上运行发布脚本,因此拉取请求不会发布。如果您配置了多个平台,也只能在 Linux 的构建中运行它。如果您只配置了一个(例如 windows),请将 Linux 替换为该平台
      - bash: |
         echo ">>> Publish"
         yarn deploy $(token)
       displayName: Publish
       condition: and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/master'), eq(variables['Agent.OS'], 'Linux'))
      

      Example azure-pipeline.yml

      【讨论】:

      • 太棒了!谢谢!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-13
      • 2020-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多