英文:
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: '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."
}
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: '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."
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论