首页 > 解决方案 > 禁用 AzureFileCopy@2 预作业?

问题描述

我正在使用 Azure Devops Pipelines (YAML)。

我有一个 AzureFileCopy@2 任务,它将文件从源复制到存储帐户中。存储帐户由早期的 ARM 部署任务动态创建(ARM 任务输出 SA 名称,然后将其解析为变量以供以后使用)。

AzureFileCopy@2 任务完美运行并将所有文件复制到存储帐户中。但是,我在运行中注意到 AzureFileCopy@2 任务实际上运行了两次——一次由我执行,一次作为“预作业”。预作业当然会失败,并警告它无法引用存储帐户(因为在那个阶段我还没有创建变量)。

幸运的是,这只是一个警告,但每次运行时都有这个警告相当烦人。我相信不能禁用前作业(尽管我可以放弃这是一个功能增强)所以有没有更好的方法来处理这种可能很常见的情况?

提前致谢

编辑:添加了混淆的 YAML:

variables:
  My.Configuration: 'Release'
  My.SQLProject: 'contoso.api'
  My.ARMProject: 'contoso.azure.templates'
  My.IntEnvironment: 'i'
  My.ResourceGroupNumber: 66
  My.ArtifactLocation: 'drop'

# BUILD STAGES ARE HERE  

- stage: 'Stage_Deploy'
  displayName: 'Stage Deploy'

  jobs:
  - deployment: 'Job_Deploy'
    pool:
      vmImage: 'windows-2019'
    displayName: 'Job Deploy'
    environment: 'env1'
    strategy:
     runOnce:
       deploy:
         steps:
         - download: none

          - task: DownloadPipelineArtifact@2
           displayName: 'Download Pipeline Artifacts from Drop'
           inputs:
             buildType: 'current'
             targetPath: '$(Pipeline.Workspace)'

         - task: AzureResourceManagerTemplateDeployment@3
           displayName: 'ARM Deployment'
           inputs:
             deploymentScope: 'Resource Group'
             azureResourceManagerConnection: 'CONTOSO CONNECTION'
             subscriptionId: 'aaaaaaaa-0000-0000-00000-aaaaaaaaaaaaa'
             action: 'Create Or Update Resource Group'
             resourceGroupName: 'contoso-$(My.IntEnvironment)-eun-core-$(My.ResourceGroupNumber)-rg'
             location: 'North Europe'
             templateLocation: 'Linked artifact'
             csmFile: '$(Pipeline.Workspace)/$(My.ArtifactLocation)/$(My.ARMProject)/azuredeploy.json'
             csmParametersFile: '$(Pipeline.Workspace)/$(My.ArtifactLocation)/$(My.ARMProject)/azuredeploy.parameters.json'
             overrideParameters: '-environment $(My.IntEnvironment)'
             deploymentMode: 'Incremental'
             deploymentOutputs: 'ARMOutput'

         - task: PowerShell@2
           condition: true
           displayName: 'Parse ARM Template Outputs'
           inputs:
             targetType: filePath
             filePath: '$(Pipeline.Workspace)/$(My.ArtifactLocation)/$(My.ARMProject)/Parse-ARMOutput.ps1'
             arguments: '-ARMOutput ''$(ARMOutput)'''

         - task: AzureFileCopy@2
           condition: true
           displayName: 'Copy Static Web Content to SA'
           inputs:
             SourcePath: '$(Pipeline.Workspace)/$(My.ArtifactLocation)'
             azureSubscription: 'CONTOSO CONNECTION'
             Destination: AzureBlob
             storage: '$(ARM.AppDataStorageName)'
             ContainerName: static

然后,当我运行它时,会发生以下阶段: 1. 初始化作业 2. 预作业:将静态 Web 内容复制到 SA

正是这个预工作,在调试中显示了这一点:

##[debug]StorageAccountRM=$(ARM.AppDataStorageName)
<other debug lines followed by...>
##[warning]Can\'t find loc string for key: StorageAccountDoesNotExist

稍后,“将静态 Web 内容复制到 SA”任务作为正常任务运行并且运行良好。

标签: azure-devopsazure-pipelines

解决方案


Fortunately, it's only a warning but it is rather annoying to have that warning in every run. I believe that pre-jobs can't be disabled (though I could drop that is a a feature enhancement) so is there a better way of handing this presumably common scenario?

Sorry but I'm afraid it's not supported to disable the warning. The warning occurs because it's by design.

(The source code of AzureFileCopyV2 task causes this behavior.)

More details:

We can find source of that task here. It contains one task.json file in which defines content like this:

  "instanceNameFormat": "$(Destination) File Copy",
    "prejobexecution": {
        "Node": {
            "target": "PreJobExecutionAzureFileCopy.js"
        }
    },

The task.json file describes the build or release task and is what the build/release system uses to render configuration options to the user and to know which scripts to execute at build/release time. Since the task.json has definition like prejobexecution, the predefined pre-job task will do the check about the inputs defined in PreJobExecutionAzureFileCopy.js file. Including the Storage Account.

So this is something by design of the code of AzureFileCopyV2 task, we can't disable the warning in pre-job task. If you do want to resolve that warning, you can consider using AzureFileCopyV1 in which doesn't define the prejobexecution, but this is not recommended. Compared with Version1, Version2 has some improvement and fixes some old issues.


推荐阅读