git - Azure DevOps - 条件/表达式允许我跳过阶段内的任务

标签 git azure azure-devops yaml azure-pipelines

我目前正在构建一个项目,一旦我们有了 pull 请求描述,我就可以对 git 标签进行一些语义版本控制。但是,我想知道如何构建一个条件或 if 语句,如果我们没有 pull 请求以确保我们在运行测试场景时不会创建标签,则允许我跳过 git 版本控制。

我的代码在这里(阶段):

# Creates tag on repo by branch name and build id
  - stage: Creating_Git_Tag
    jobs:
    - job: CalculateVersion
      displayName: Semantic versioning
      steps:
      # Checkout with persist credentials
        - checkout: self
          persistCredentials: true

         # Install GitVersion
        - task: gitversion/setup@0
          displayName: Install GitVersion
          inputs:
            versionSpec: '5.x'

        # Retrieve Pull Request Description
        - task: PullRequestDescription@0
          name: RetrievePullRequestDescription
          displayName: Retrieve Pull Request description
          inputs:
            action: 'view'
            outputVariable: 'PullRequest.DescriptionContent'
            isOutput: true
            stripIdentifiers: false

        # Add git commit message that will be picked up by GitVersion ("+semver: patch/minor/major")
        # Depending on the Pull Request description, where the developer has marked the type of change
        - task: PowerShell@2
          displayName: Add git commit message for SemVer
          inputs:
            targetType: inline
            script: |
              Write-Host "Configuring git author info.." -ForegroundColor Cyan
            
              git config user.email "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d3a6a0b6a1fdb7b6a7b2bfa093b7b6a7b2babfa0fdb0bcbe" rel="noreferrer noopener nofollow">[email protected]</a>"
              git config user.name "user.details"
              Write-Host "Doing git checkout..." -ForegroundColor Cyan
              git checkout -b $("$(System.PullRequest.SourceBranch)".replace('refs/heads/', ''))
              Write-Host "Checking Pull Request description..." -ForegroundColor Cyan
              $PRdesc = "$(RetrievePullRequestDescription.PullRequest.DescriptionContent)"
              if ($PRdesc -match '(\[x\] \bFix\b)') {
                Write-Host "Adding git (empty) commit message to mark this branch as a 'patch' SemVer increment." -ForegroundColor Cyan
                git commit -a -m "+semver: patch [skip azurepipelines]" --allow-empty
              } elseif ($PRdesc -match '(\[x\] \bMinor\b)') {
                Write-Host "Adding git (empty) commit message to mark this branch as a 'minor' SemVer increment." -ForegroundColor Cyan
                git commit -a -m "+semver: minor [skip azurepipelines]" --allow-empty
              } elseif ($PRdesc -match '(\[x\] \bMajor\b)') {
                Write-Host "Adding git (empty) commit message to mark this branch as a 'major' SemVer increment." -ForegroundColor Cyan
                git commit -a -m "+semver: major [skip azurepipelines]" --allow-empty
              } else {
                Write-Host "##vso[task.LogIssue type=error;]Please select the type of change in the Pull Request description, and Re-queue the validation." -ForegroundColor Cyan
                $PRdesc
                exit 1
              }
              Write-Host "Doing git push.." -ForegroundColor Cyan
              git push --set-upstream origin $("$(System.PullRequest.SourceBranch)".replace('refs/heads/', ''))
              Write-Host "Done." -ForegroundColor Cyan
            
        # Determine the semantic version & test * Tes
        - task: gitversion/execute@0
          displayName: Determine SemVer      

有谁知道如何为此运行 if 语句,或者我该怎么做?您是否有我可以遵循的示例或我必须遵循的指导?

最佳答案

如果管道运行是为了 pull 请求,您可以将以下条件添加到您想要运行的任何任务/步骤/脚本中,否则它将被跳过。

    - task: PowerShell@2
      displayName: Pull request check
      condition: ne(variables['System.PullRequest.PullRequestId'],'')
      inputs:
        targetType: inline
        script: |
             echo "Executing as this run is for a pull request"

关于git - Azure DevOps - 条件/表达式允许我跳过阶段内的任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70687095/

相关文章:

git 分支工作流

Git:如何使用开发中所做的更改来更新本地功能分支?

git - 获取 Visual Studio 2017 以使用 ssh-agent/ssh-add for git

linux - Git 不会忽略文件权限更改

Azure 无效的 SSL 绑定(bind) 以下主机名与证书不匹配 :

azure - 获取 Cosmos DB 集合中每个逻辑分区的大小

spring - 在Linux虚拟机中运行嵌入式tomcat java Spring应用程序(Microsoft Azure)

azure-devops - 用于游泳的 Azure DevOps REST API

git - 实现 TFS pull 请求

azure - Get-AzRecoveryServicesVault 在 devops 发布管道中抛出 "Could not load file or assembly Microsoft.Azure.PowerShell.Authentication"