通过 YAML 发布管道运行 azure powershell 脚本

时间:2021-02-18 11:26:59

标签: azure powershell azure-devops yaml azure-powershell

我有我的正常和工作发布管道,通过给定的部署组执行一些任务:

enter image description here

  1. 复制脚本
  2. 执行该 powershell 脚本(在部署组中定义的目标机器上)
  3. 删除脚本

我知道 YAML 不支持部署组,但是(我很幸运!)到目前为止我的部署组只有一台机器,我们称之为 MyTestVM

所以我想要实现的主要是简单地在那个 vm 上执行一个 powershell 脚本。通常,发布管道发生的情况是,您在 VM 上安装了一个触手/发布代理,您的部署目标(位于部署组内)与之相连,并且您的发布管道(感谢部署组)规范)能够在机器上使用该发布代理并在 VM 本身上做任何它想做的事情。

我需要同样的...但是通过 YAML !我知道 YAML 中有 PowerShellOnTargetMachines 命令可用,但我不想使用它。它使用 PSSession,它需要 SSL 证书和许多其他东西。我只想使用 VM 上已经存在的代理!

到目前为止我所拥有的:

pool: 'Private Pool'

steps:
- task: DownloadPipelineArtifact@2
  inputs:
    buildType: 'specific'
    project: 'blahblah'
    definition: 'blah'
    buildVersionToDownload: 'latest'
    targetPath: '$(Pipeline.Workspace)'
    
- task: CopyFiles@2
  displayName: 'Copy Files to: C:\TestScript'
  inputs:
    SourceFolder: '$(Pipeline.Workspace)/Scripts/'
    Contents: '**/*.ps1'
    TargetFolder: 'C:\TestScript'
    CleanTargetFolder: true
    OverWrite: true

第一部分只是下载包含我的脚本的 Artifact。然后老实说,我什至不确定我是否需要在第二部分复制脚本..首先是因为我认为它不会将脚本复制到 VM 目标工作区,而是将它复制到 VM 中azure 管道代理已安装。第二:我想我可以从我的工件中引用它......但这不是重要的部分。 如何让我的 YAML 管道以与正常发布管道相同的方式使用安装在 VM 上的发布代理?

2 个答案:

答案 0 :(得分:0)

要让作业在您想要的特定发布代理上运行,您可以做两件事:

创建一个池并只将您的发布代理放入其中。

void SqlQueryItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QStyledItemDelegate::paint(painter, option, index);
    SqlQueryItem* item = getItem(index);

    if (item->isUncommitted())
    {
        painter->setPen(item->isCommittingError() ? QColor(Qt::red) : QColor(Qt::blue));
        painter->setBrush(Qt::NoBrush);
        painter->drawRect(option.rect.x(), option.rect.y(), option.rect.width()-1, option.rect.height()-1);
    }

    if (item->isLimitedValue())
    {
        QString text = displayText(item->getValue(), option.locale);
        int textWidth = option.fontMetrics.boundingRect(text).width();
        int margin = QApplication::style()->pixelMetric(QStyle::PM_FocusFrameHMargin, nullptr, option.widget) + 1; // from QCommonStyle source code
        if (option.rect.width() >= (textWidth + LOAD_FULL_VALUE_BUTTON_SIZE + 4 + (margin * 2)))
        {
            QStyleOptionButton button = fullValueButtonOption;
            button.rect = getLoadFullValueButtonRegion(option.rect);
            button.state = QStyle::State_Enabled | QStyle::State_MouseOver;
            if (lmbPressedOnButton)
                button.state |= QStyle::State_Sunken | QStyle::State_Active;

            QApplication::style()->drawControl(
                        mouseOverFullDataButton ? QStyle::CE_PushButton : QStyle::CE_PushButtonLabel,
                        &button, painter);
        }
    }
}

使用现有池,并为您的代理发布/要求功能。

在代理机器上,添加一个系统环境变量(例如,pool: 'My Pool with only one release agent' 。给它一个类似 MyCustomCapability 的值

然后你的管道变成:

1

如果只有这个代理设置了这个环境变量,那么只有这个代理可以执行作业

答案 1 :(得分:0)

以某种方式达成了解决方案。首先值得一提的是,由于部署组不使用 YAML 管道,因此继续的方法是创建一个环境并将目标 VM 添加为资源。

所以我不需要创建自己的托管代理或任何特殊的东西,因为问题是目标本身而不是运行管道的代理。 通过创建环境并向该环境添加资源(在我的情况下为 VM),我们还在目标本身上创建了一个新的发布代理。因此,我的目标 VM 现在将有 2 个发布代理:一个可以被普通发布管道使用,一个是新的,附加到 Azure Devops 上的环境资源,可以被 YAML 管道使用。

现在我终于可以访问我的虚拟机了:

- stage: PerformScriptInVM
  jobs:
  - deployment: VMDeploy
    pool:
      vmImage: 'windows-latest'
    # watch out: this creates an environment if it doesn’t exist
    environment:
      name: My Environment Name
      resourceType: VirtualMachine
    strategy:
      runOnce:
        deploy:
        steps:
        - task: DownloadPipelineArtifact@2
          inputs:
            buildType: 'specific'
            project: 'blahblahblah'
            definition: 'blah'
            buildVersionToDownload: 'latest'
            targetPath: '$(Pipeline.Workspace)'
        - task: PowerShell@2
          displayName: 'PowerShell Script'
          inputs:
            targetType: filePath
            filePath: '$(Pipeline.Workspace)/Scripts/TestScript.ps1'
            arguments: 'whatever your script needs..'