从Azure DevOps管道中检索提交的更改

huangapple go评论90阅读模式
英文:

Retrieve changes from commit in Azure DevOps pipeline

问题

  • 我想创建以下解决方案:
  • 创建一个新分支并添加子文件夹和配置文件
  • 将更改提交到分支
  • 提交触发 ADO 管道
  • 管道检索添加的子文件夹名称和内容

在我的机器上,这个配置有效:

{
    - task: PowerShell@2
        displayName: 'Retrieve subFolderName'
        inputs:
            targetType: 'inline'
            script: |
                $changedFiles = git diff main --name-only --relative -- "**/myDefinition.json"
                $folders = ""
                Write-Verbose "ChangedFiles:" -Verbose
                $changedFiles | Out-String | Write-Output

                foreach ($file in $changedFiles) {
                    Write-Verbose "File: $file" -Verbose
                    if ($file -like "*/myDefinition.json") {
                    $folders = Split-Path -Parent $file
                    }
                }

                if ($folders) {
                    Write-Output "Path to myDefinition.json: $folders"
                    $subFolderName = $($folders.split('\'))[-1]
                    Write-Output "SubscriptionName: $subFolderName"
                }
                else {
                    Write-Error "No myDefinition.json found in the commit."
                }
}

然而,在管道中运行时,我收到以下错误消息:
fatal: bad revision 'main'

我尝试添加 git fetch,但然后我得到:fatal: could not read Password for <>

git diff 是否适合这里,还是我应该使用不同的命令?

英文:

I want to create the following solution:

  • A new branch is created and a subfolder + configuration file is added
  • The changes are committed to the branch
  • An ADO pipeline is triggered by the commit
  • The pipeline retrieves the name of the added subfolder and content

On my machine this configuration works:

{
- task: PowerShell@2
    displayName: &#39;Retrieve subFolderName&#39;
    inputs:
        targetType: &#39;inline&#39;
        script: |
            $changedFiles = git diff main --name-only --relative -- &quot;**/myDefinition.json&quot;
            $folders = &quot;&quot;
            Write-Verbose &quot;ChangedFiles:&quot; -Verbose
            $changedFiles | Out-String | Write-Output

            foreach ($file in $changedFiles) {
                Write-Verbose &quot;File: $file&quot; -Verbose
                if ($file -like &quot;*/myDefinition.json&quot;) {
                $folders = Split-Path -Parent $file
                }
            }

            if ($folders) {
                Write-Output &quot;Path to myDefinition.json: $folders&quot;
                $subFolderName = $($folders.split(&#39;\&#39;))[-1]
                Write-Output &quot;SubscriptionName: $subFolderName&quot;
            }
            else {
                Write-Error &quot;No myDefinition.json found in the commit.&quot;
            }

However, when I run it in the pipeline, I get this error message:
fatal: bad revision 'main'

I tried to add git fetch, but then I get: fatal: could not read Password for <>

Is git diff the way to go here, or should I use a different command?

答案1

得分: 0

经过许多次迭代,我终于让这段代码工作了:

trigger:
  branches:
    include:
    - alz/*

pool:
  vmImage: 'ubuntu-latest'

steps:
  - checkout: self
    persistCredentials: true

  - task: PowerShell@2
    displayName: 'Retrieve MyDefinition.json'
    inputs:
        targetType: 'inline'
        script: |
            echo ======Git-Version=====
            git --version
            echo ======Git-Branch======
            git branch
            echo ======BuildVersion======
            echo $(Build.SourceVersion)
            echo ======Git-Fetch===========
            git fetch origin "+refs/heads/*:refs/remotes/origin/*"
            echo ======Checkout-Branch===========
            git checkout -B main origin/main
            echo ======Git-Branch======
            git branch
            echo ======Git-Status======
            git status
            echo ======Git-Diff======
            git diff $(Build.SourceVersion) --name-only --relative
            echo ======Script======
            $changedFiles = git diff $(Build.SourceVersion) --name-only --relative -- "**/MyDefinition.json"
            $folders = ""
            Write-Verbose "ChangedFiles:" -Verbose
            $changedFiles | Out-String | Write-Output

            if($changedFiles -ne $null) {
              foreach ($file in $changedFiles) {
                  Write-Verbose "File: $file" -Verbose
                  if ($file -like "*/MyDefinition.json") {
                  $folders = Split-Path -Parent $file
                  }
              }
            }
            else {
              Write-Warning "No MyDefinition.json found in the commit!"
            }

            if ($folders) {
                Write-Output "Path to MyDefinition.json: $folders"
                $subFolderName = $($folders.split('/'))[-1]
                Write-Output "SubscriptionName: $subFolderName"
            }
            else {
                Write-Warning "No MyDefinition.json found in the commit."
            }            
英文:

After a lot of iterations, I finally got this code to work:

trigger:
  branches:
    include:
    - alz/*

pool:
  vmImage: &#39;ubuntu-latest&#39;

steps:
  - checkout: self
    persistCredentials: true

  - task: PowerShell@2
    displayName: &#39;Retrieve MyDefinition.json&#39;
    inputs:
        targetType: &#39;inline&#39;
        script: |
            echo ======Git-Version=====
            git --version
            echo ======Git-Branch======
            git branch
            echo ======BuildVersion======
            echo $(Build.SourceVersion)
            echo ======Git-Fetch===========
            git fetch origin &quot;+refs/heads/*:refs/remotes/origin/*&quot;
            echo ======Checkout-Branch===========
            git checkout -B main origin/main
            echo ======Git-Branch======
            git branch
            echo ======Git-Status======
            git status
            echo ======Git-Diff======
            git diff $(Build.SourceVersion) --name-only --relative
            echo ======Script======
            $changedFiles = git diff $(Build.SourceVersion) --name-only --relative -- &quot;**/MyDefinition.json&quot;
            $folders = &quot;&quot;
            Write-Verbose &quot;ChangedFiles:&quot; -Verbose
            $changedFiles | Out-String | Write-Output

            if($changedFiles -ne $null) {
              foreach ($file in $changedFiles) {
                  Write-Verbose &quot;File: $file&quot; -Verbose
                  if ($file -like &quot;*/MyDefinition.json&quot;) {
                  $folders = Split-Path -Parent $file
                  }
              }
            }
            else {
              Write-Warning &quot;No MyDefinition.json found in the commit!&quot;
            }

            if ($folders) {
                Write-Output &quot;Path to MyDefinition.json: $folders&quot;
                $subFolderName = $($folders.split(&#39;/&#39;))[-1]
                Write-Output &quot;SubscriptionName: $subFolderName&quot;
            }
            else {
                Write-Warning &quot;No MyDefinition.json found in the commit.&quot;
            }

huangapple
  • 本文由 发表于 2023年7月12日 23:29:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76672218.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定