简体   繁体   English

DevOps YAML 管道和变量

[英]DevOps YAML Pipeline and variables

I'm a bit of a beginner with YAMl and PS, so bear with me a bit.我是 YAMl 和 PS 的初学者,所以请耐心等待。

The scenario:场景:

I have SQL in Source Control using Redgate SoC and a build pipeline that contains the Redgate Build Extension, I have a task that checks whether there were any changes to Functions, SP's, Tables etc and a condition in the Redgate Build task that will skip the task if there were no changes to the above folders, when there are only scripts(data changes) that were pushed, and all of this works great.我有 SQL 在源代码控制中使用 Redgate SoC 和包含 Redgate 构建扩展的构建管道,我有一个任务检查函数、SP、表等是否有任何更改,以及 Redgate 构建任务中的条件将跳过如果上述文件夹没有更改,则只有脚本(数据更改)被推送,并且所有这些都很好。 My next challenge (deployment pipeline) was to check if there was a Redgate Build performed to skip the deployment of the Redgate Deployment task.我的下一个挑战(部署管道)是检查是否执行了 Redgate 构建以跳过 Redgate 部署任务的部署。

My thinking here was, create a variable that I can save in the build pipeline and then use in the deployment pipeline (using the Variable Tools extension), under run this task (in the Redgate Deploy Pipeline section, set it to Custom Conditions and then check if the variable is true or not.我的想法是,创建一个我可以保存在构建管道中的变量,然后在部署管道中使用(使用变量工具扩展),在运行此任务下(在 Redgate 部署管道部分,将其设置为自定义条件,然后检查变量是否为真。

Following is my YAML Build pipeline:以下是我的 YAML 构建管道:

trigger:
  - dev

variables:
  FunctionsFlag: $false
  ProceduresFlag: $false
  TablesFlag: $false
  DataFlag: $false
  ViewsFlag: $false
  RGDeployFlag: $false

jobs:
  - job: AdventureWorks
    timeoutInMinutes: 0
    pool: Enterprise
    cancelTimeoutInMinutes: 1
    steps:
      - checkout: self
        clean: true

      - powershell: |
          ## get the changed files
          $files=$(git diff HEAD HEAD~ --name-only)
          $temp=$files -split ' '
          $count=$temp.Length
          echo "Total changed $count files"
            For ($i=0; $i -lt $temp.Length; $i++)
              {
              $name=$temp[$i]
              echo "this is $name file"
              if ($name -like 'Functions/*')
              {
              ##set the flag variable FunctionsFlagFlag to true
              Write-Host "##vso[task.setvariable variable=FunctionsFlag]$True"
              Write-Host "##vso[task.setvariable variable=RGDeployFlag]$True"
              ##Set-Variable -Name $(RGDeployFlag) -Value $True
              echo "The answer is $(RGDeployFlag)"
              }
              if ($name -like 'Procedures/*')
              {
              ##set the flag variable StoredProceduresFlagFlag to true
              Write-Host "##vso[task.setvariable variable=ProceduresFlag]$True"
              }
              if ($name -like 'Tables/*')
              {
              ##set the flag variable TablesFlagFlag to true
              Write-Host "##vso[task.setvariable variable=TablesFlag]$True"
              }
              if ($name -like 'Data/*')
              {
              ##set the flag variable StoredProceduresFlagFlag to true
              Write-Host "##vso[task.setvariable variable=DataFlag]$True"
              }
              if ($name -like 'Views/*')
              {
              ##set the flag variable TablesFlagFlag to true
              Write-Host "##vso[task.setvariable variable=ViewsFlag]$True"
              }
              if (($name -like 'Functions/*') -or ($name -like 'Procedures/*') -or ($name -like 'Tables/*') -or ($name -like 'Data/*') -or($name -like 'Views/*'))
              {
              ##set the flag variable RGDeployFlag to true
              Write-Host "##vso[task.setvariable variable=RGDeployFlag;isoutput=true]$True"
              $RGDeployFlag=$True
              echo "The answer is $(RGDeployFlag)"
              }
              
            echo "The answer is $(RGDeployFlag)"
            }
  
      - task: RedgateSqlChangeAutomationBuild@4
        inputs:
          operation: Build
          dbFolder: RootFolder
          packageName: AdventureWorks
          tempServerTypeBuild: localDB
          buildAdvanced: true
          compareOptionsBuild: 'NoTransactions, IgnoreFileGroups'
          dataCompareOptionsBuild: 'DisableAndReenableDMLTriggers, SkipFkChecks'
          transactionIsolationLevelBuild: readCommitted
          queryBatchTimeoutBuild: '0'
          excludeEnviromentVariables: 'AGENT_*;BUILD_*;SYSTEM_*'
          nugetPackageVersionSelector: Specific
          nugetPackageVersion: '1.0'
          nugetPackageVersionUseBuildId: true
          requiredVersionOfDlma: latestInstalled
        condition: and(succeeded(), or(eq(variables.FunctionsFlag, true),eq(variables.ProceduresFlag, true),eq(variables.TablesFlag, true),eq(variables.DataFlag, true),eq(variables.ViewsFlag, true)))
      - task: CopyFiles@2
        inputs:
          SourceFolder: '$(System.DefaultWorkingDirectory)/Scripts'
          Contents: '**'
          TargetFolder: '$(System.ArtifactsDirectory)/Scripts'
          CleanTargetFolder: true
          OverWrite: true
          preserveTimestamp: true
      - task: variabledehydration@0
        inputs:
          prefixes: 'GITVERSION,BUILD,RGDeployFlag'
          outpath: '$(Build.ArtifactStagingDirectory)'
      - task: PublishBuildArtifacts@1
        displayName: 'Publish Artifact: drop'
        inputs:
          PathtoPublish: $(System.ArtifactsDirectory)

Release Pipeline Custom Conditions So my main problem is setting the variable to be used in the deployment pipeline.发布管道自定义条件所以我的主要问题是设置要在部署管道中使用的变量。 Here is where I require some guidance:这是我需要一些指导的地方:

Am I supposed to create multiple stages and or jobs in order to do this, or is my YAML structure correct, and that I'm just setting the values incorrectly?我是否应该为此创建多个阶段和/或作业,或者我的 YAML 结构是否正确,而我只是错误地设置了值?

Inside the if statements of my PS section, I have been trying a few tests to manipulate the outcome, but so far with little success.在我的 PS 部分的 if 语句中,我一直在尝试一些测试来操纵结果,但到目前为止收效甚微。

Regardless of how I set the variable, it keeps evaluating to false.无论我如何设置变量,它都会一直评估为假。

I look forward to your comments.我期待你的评论。

Regards问候

Wic威克

So I figured out why this did not work:所以我想出了为什么这不起作用:

Firstly, I had to remove the variable 'declaration' from the YAML as I was setting it to be cast in stone:首先,我必须从 YAML 中删除变量“声明”,因为我将其设置为一成不变:

Added an initialization for my variable.为我的变量添加了初始化。

And lastly added the set variable.最后添加了 set 变量。

Voila, I am able to use that variable in my release pipeline.瞧,我可以在我的发布管道中使用该变量。

The complete YAML pipeline now looks like:完整的 YAML 管道现在如下所示:

 trigger: - dev ## Removed the variables section here jobs: - job: AdventureWorks timeoutInMinutes: 0 pool: Enterprise cancelTimeoutInMinutes: 1 steps: - checkout: self clean: true - powershell: | ## Initialize my variable here $RGDeployFlag = $false ## get the changed files $files=$(git diff HEAD HEAD~ --name-only) $temp=$files -split ' ' $count=$temp.Length echo "Total changed $count files" For ($i=0; $i -lt $temp.Length; $i++) { $name=$temp[$i] echo "this is $name file" if ($name -like 'Functions/*') { ##set the flag variable FunctionsFlagFlag to true Write-Host "##vso[task.setvariable variable=FunctionsFlag]$True" } if ($name -like 'Procedures/*') { ##set the flag variable StoredProceduresFlagFlag to true Write-Host "##vso[task.setvariable variable=ProceduresFlag]$True" } if ($name -like 'Tables/*') { ##set the flag variable TablesFlagFlag to true Write-Host "##vso[task.setvariable variable=TablesFlag]$True" } if ($name -like 'Data/*') { ##set the flag variable StoredProceduresFlagFlag to true Write-Host "##vso[task.setvariable variable=DataFlag]$True" } if ($name -like 'Views/*') { ##set the flag variable TablesFlagFlag to true Write-Host "##vso[task.setvariable variable=ViewsFlag]$True" } if (($name -like 'Functions/*') -or ($name -like 'Procedures/*') -or ($name -like 'Tables/*') -or ($name -like 'Data/*') -or($name -like 'Views/*')) { ## set the flag variable RGDeployFlag to true $RGDeployFlag = $true } ## Test to see what the variable value is "RGDeployFlag: $RGDeployFlag" } - task: RedgateSqlChangeAutomationBuild@4 inputs: operation: Build dbFolder: RootFolder packageName: AdventureWorks tempServerTypeBuild: localDB buildAdvanced: true compareOptionsBuild: 'NoTransactions, IgnoreFileGroups' dataCompareOptionsBuild: 'DisableAndReenableDMLTriggers, SkipFkChecks' transactionIsolationLevelBuild: readCommitted queryBatchTimeoutBuild: '0' excludeEnviromentVariables: 'AGENT_*;BUILD_*;SYSTEM_*' nugetPackageVersionSelector: Specific nugetPackageVersion: '1.0' nugetPackageVersionUseBuildId: true requiredVersionOfDlma: latestInstalled condition: and(succeeded(), or(eq(variables.FunctionsFlag, true),eq(variables.ProceduresFlag, true),eq(variables.TablesFlag, true),eq(variables.DataFlag, true),eq(variables.ViewsFlag, true))) - task: CopyFiles@2 inputs: SourceFolder: '$(System.DefaultWorkingDirectory)/Scripts' Contents: '**' TargetFolder: '$(System.ArtifactsDirectory)/Scripts' CleanTargetFolder: true OverWrite: true preserveTimestamp: true - task: variabledehydration@0 inputs: prefixes: 'GITVERSION,BUILD,RGDeployFlag' outpath: '$(Build.ArtifactStagingDirectory)' - task: PublishBuildArtifacts@1 displayName: 'Publish Artifact: drop' inputs: PathtoPublish: $(System.ArtifactsDirectory)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 azure devops YAML 重命名文件夹管道任务 - Rename folder pipeline task with azure devops YAML 如何在 Azure DevOps yaml 管道的每个循环中使用变量 - How to use a variable in each loop in Azure DevOps yaml pipeline PowerShell Hashtable 作为 Azure Devops YAML 管道 Z78E6221F6393D1356681DB398F14CE6 - PowerShell Hashtable as Azure Devops YAML Pipeline output Variable Azure DevOps 中的 PowerShell 任务中的管道变量是否可用? - Are pipeline variables available in PowerShell task in Azure DevOps? 电源 shell 命令在 Azure Devops 中的 Yaml 管道中不起作用 - The power shell command do not work in Yaml pipeline in Azure Devops 是否有选项如何通过代码在Azure devops中创建新的管道变量 - Is there option how create new pipeline variables in Azure devops via code Azure DevOps Pipeline - Power shell脚本,使用变量复制文件 - Azure DevOps Pipeline - Power shell script , Copy Files using Variables 如何使用powershell读取azure devops管道中的环境变量? - How to read environment variables in azure devops pipeline using powershell? Azure DevOps发布管道 - 在括号内使用带有管道变量的Powershell - Azure DevOps Release Pipelines - Using Powershell with Pipeline variables inside brackets Azure DevOps Pipeline 变量定义在构建前自动更改 - Azure DevOps Pipeline variables define automatically change before build
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM