![](/img/trans.png)
[英]how to pass Powershell script filepath from azure devops repo into Azure devops build pipeline
[英]Azure DevOps: How to retrieve a build artifact from build Azure Pipeline from a PowerShell Script in Release Pipeline?
我發布了一個構建工件,發布到 $(Build.ArtifactStagingDirectory)/drop,其 artifactName 為“some_sidebar”,工件發布位置是 Azure Pipeline。
如果發布任務中只有 PowerShell Skript,我現在如何在發布管道中檢索該工件?
這是代碼特定部分:
$path = ".\_some_sidebar\drop"
#$path = $(Build.Repository.LocalPath)
$SPFolderName = "Style Library/_some_sidebar";
# Upload template list
$status = "Uploading template list to Location: " + $SPFolderName
Write-Host $status
$te = Add-PnPFile -Path $path"\some_sidebar.js" -Folder $SPFolderName -Checkout
Set-PnPFileCheckedIn -Url $te.ServerRelativeUrl
我收到以下錯誤:
Uploading template list to Location: Style Library/_some_sidebar
2020-01-16T09:51:20.5062033Z Add-PnPFile : Local file was not found.
2020-01-16T09:51:20.5062546Z At D:\_work\_temp\6d682160-e8a7-4c56-ab30-7ff8c40f2958.ps1:51 char:7
2020-01-16T09:51:20.5062832Z + $te = Add-PnPFile -Path $path"\some_sidebar.js" -Folder $SPFolderName ...
我假設 azure 管道中的構建工件路徑是虛擬機中的某個路徑...但我不知道如何在 shell 腳本中指定該路徑,或者該路徑是什么...?
Azure DevOps:如何從 Release Pipeline 中的 PowerShell 腳本構建 Azure Pipeline 檢索構建工件?
您的帖子中有三個問題導致了此問題。
首先,由於您選擇的工件發布位置是Azure Pipeline
,因此您無法設置targetPath
。 您可以檢查文檔發布構建工件任務:
我假設您所說的應該將pathtoPublish
設置為$(Build.ArtifactStagingDirectory)/dro
with artifactName " some_sidebar
",例如:
但是這個pathtoPublish
是用來設置要發布的文件夾或者文件路徑的,也就是說,它是工件源位置,而不是目標。
因此,我們不需要在 powershell 腳本中使用\\drop
來獲取工件。
其次,MS提供了一系列Release變量,方便我們直接使用。
您可以使用System.DefaultWorkingDirectory
、 System.ArtifactsDirectory
或Agent.ReleaseDirectory
:
因此,我們可以在 powershell 腳本中使用上述三個變量之一來獲取工件,但該變量不是文件的完整路徑,它是發布管道中工件的路徑,我們需要再做一步。
第三,當您使用發布管道獲取工件時,這會將工件設置為包含Source alias
的文件夾:
作為測試,我使用以下 powershell 腳本創建了一個示例:
$path = "$(System.DefaultWorkingDirectory)\<SourceAliasVlaue>\<AartifactName>"
#$path = $(Build.Repository.LocalPath)
$SPFolderName = "Style Library/_some_sidebar";
# Upload template list
$status = "Uploading template list to Location: " + $SPFolderName
Write-Host $status
Get-ChildItem -Path $path
我使用 powershell 腳本Get-ChildItem -Path $path
列出工件中的文件:
現在,我可以在 powershell 任務中獲取工件文件some_sidebar.js
。
注意:您可以嘗試使用通配符來獲取工件,例如:
$te = Add-PnPFile -Path "$(System.DefaultWorkingDirectory)\**\some_sidebar.js"
希望這可以幫助。
您應該能夠使用 System.ArtifactsDirectory。
這是我的管道示例,我如何使用上一步中的工件。 應該可以在 powershell 腳本中使用相同的變量。 (此示例來自用於構建和發布的 yaml 管道。)
stages:
- stage: build
displayName: 'Build and package solution'
jobs:
- job: buildsteps
displayName: 'Steps to build and package'
pool: 'PrivateVS2017'
steps:
- task: ArchiveFiles@2
inputs:
rootFolderOrFile: '$(Build.SourcesDirectory)/Web'
includeRootFolder: true
archiveType: 'zip'
archiveFile: '$(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip'
replaceExistingArchive: true
- task: PublishBuildArtifacts@1
inputs:
PathtoPublish: '$(Build.ArtifactStagingDirectory)'
ArtifactName: 'it-service-wiki-build'
publishLocation: 'Container'
- stage: deploy_to_development
displayName: 'Deploy to development Environment'
dependsOn: build
jobs:
- deployment: deploy
displayName: 'Deploy the solution to Dev'
pool: 'PrivateVS2017'
environment: 'ITServiceWiki-Dev'
strategy:
runOnce:
deploy:
steps:
- task: DownloadBuildArtifacts@0
inputs:
buildType: 'current'
buildVersionToDownload: 'latest'
downloadType: 'single'
ArtifactName: 'it-service-wiki-build'
downloadPath: '$(System.ArtifactsDirectory)'
- task: ExtractFiles@1
inputs:
archiveFilePatterns: '../a/**/$(Build.BuildId).zip'
destinationFolder: '$(Build.DefaultWorkingDirectory)/$(Build.BuildId)'
cleanDestinationFolder: true
下載工件后搜索 zip 時請注意../a/**/
。 不確定 /a/ 是否相同,所有構建代理也可以在這里使用 system.artifactsDirectory。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.