是否可以在 Powershell 中编写自定义 Azure DevOps 任务?

Is it possible to author a custom Azure DevOps task in Powershell?

我们使用 Azure DevOps Server 2019(本地)。

我想在 Powershell 中编写自定义 Azure DevOps 任务。到目前为止,我在网上看到的例子都是关于用 Typescript 编写的。

我想知道 - 这是唯一的方法吗?例如,我们可以使用 Powershell 吗?

是的,您可以直接在自定义任务中使用 PS 脚本。您可以这样配置 task.json

"execution": {
    "PowerShell3": {
        "target": "script.ps1",
         "workingDirectory": "$(currentDirectory)"
    }
 }

是的,您可以在自定义生成任务中使用 PowerShell。

您需要在 task.json 中编辑此部分:

  "execution": {
    "PowerShell3": {
      "target": "ps-script.ps1",
      "workingDirectory": "$(currentDirectory)"
    }
  }

并且您需要安装 VstsTaskSdk Powershell 模块:

  • 打开 Powershell
  • 导航到您的扩展目录root/buildtask
  • 执行mkdir ps_modules然后导航到新目录
  • 你的 pwd 应该是 root/buildtask/ps_modules
  • 执行 Save-Module -Name VstsTaskSdk -Path . 将模块保存到磁盘。
  • 通过删除版本号来扁平化目录结构。例如,您将有一个 root/buildtask/ps_modules/VstsTaskSdk/0.10.0/* 的路径,现在应该显示为 root/buildtask/ps_modules/VstsTaskSdk/*

存在完整教程here

您还可以在 this GitHub 存储库中查看带有 PS 的自定义任务示例。

注意:它只适用于windows台机器。