Publish artifacts build failed in Azure DevOps - azure-devops

I am setting publish build artifacts in Azure DevOps but following error occurred.
'D:\a\1\a' is empty. Nothing will be added to build artifact 'drop'.
I am doing this to exchange dynamics 365 solution from one instance to another instance using Azure DevOps.
Following is my YAML document for publish build artifacts.
- task: PublishBuildArtifacts#1
inputs:
pathtoPublish: $(Build.ArtifactStagingDirectory)
artifactName: drop
displayName: 'Publish build artifacts'
Following this link.
I want that publish build artifacts successfully.

You have to copy something into your $(Build.ArtifactStagingDirectory) directory before the publish task.
This is example for the .net project:
- task: CopyFiles#2
displayName: 'Copy Files to: $(build.artifactstagingdirectory)'
inputs:
SourceFolder: '$(build.sourcesdirectory)'
Contents: '**\bin\$(BuildConfiguration)\**'
TargetFolder: '$(build.artifactstagingdirectory)'
condition: succeededOrFailed()
As I understand from your link, the powershell task will create a zip file in the $(Build.ArtifactStagingDirectory) directory:
- powershell: |
Start-Process tools/CoreTools/SolutionPackager.exe `
-ArgumentList `
"/action: Pack", `
"/zipfile: $(Build.ArtifactStagingDirectory)\packedSolution\$env:SolutionName.zip", `
"/folder: $env:SolutionPath", `
"/packagetype: Both" `
-Wait `
-NoNewWindow
env:
SolutionPath: $(solution.path)
SolutionName: $(solution.name)
displayName: 'Solution Packager: pack solution'

Related

How can I check that an artifact is built in ADO

I'm building an ADO pipeline and want to check that an artifact has been created before continuing with the build stage. I'm trying to create a variable to tell me if the file exists but when I run the pipeline I get an error saying the file path is invalid.
This is the relevant ymal code:
- task: PublishBuildArtifacts#1
inputs:
PathtoPublish: $(Build.ArtifactStagingDirectory) # dist or build files
ArtifactName: 'www' # output artifact named www
- task: PowerShell#2
inputs:
script: |
$fileExists = Test-Path -Path "$(Build.ArtifactStagingDirectory)/www/dist/apps/poc/index.html"
Write-Output "##vso[task.setvariable variable=FileExists]$fileExists"
- stage: deploy
condition: and(succeeded(), eq(variables['FileExists'], True))
This is the error that I get when I run the pipeline:
##[error]Invalid file path '/home/vsts/work/1/s'. A path to a .ps1 file is required.
##[error]Invalid file path '/home/vsts/work/1/s'. A path to a .ps1 file is required.
From your YAML sample, the Powershell task definition format is not correct if you are using inline script type.
You need to define the targetType field.
For example: targetType: 'inline'
- task: PowerShell#2
inputs:
targetType: 'inline'
script: |
$fileExists = Test-Path -Path "$(Build.ArtifactStagingDirectory)/www/dist/apps/poc/index.html"
Write-Output "##vso[task.setvariable variable=FileExists]$fileExists"

Bicep Deployment error : An error occurred reading file. Could not find a part of the path '/home/vsts/work/1/s/bicep/storageaccount.bicep'

I'm trying to deploy a bicep template using a powershell task in a devOps yml pipeline.
I have the following task:
- task: AzurePowerShell#4
displayName: "4.3) Deploy xxxxxx Synapse Infra"
enabled: true
inputs:
azureSubscription: ${{parameters.azureServiceConnection}}
ScriptType: "InlineScript"
azurePowerShellVersion: "LatestVersion"
continueOnError: true
errorActionPreference : "continue"
Inline: |
echo "Deploy Bicep template"
$deployment = New-AzResourceGroupDeployment `
-ResourceGroupName "rg-emdi-data-${{parameters.environment}}" `
-TemplateFile "$env:BUILD_SOURCESDIRECTORY\bicep\storageaccount.bicep" `
-envName "${{parameters.environment}}" `
-location "${{parameters.location}}" `
-storageId "$(storageID)" `
However, when I run it, I get the following error message :
ERROR: An error occurred reading file. Could not find a part of the path '/home/vsts/work/1/s/bicep/storageaccount.bicep'.
I can't understand why the file path is not resolved. My file structure is:
It works if I deploy main.bicep but fails when deploying the storage account module.
Any help would be great.
A deployment job does not automatically clone the source repo, so you can either use the artifact approach as mentioned by Thomas or you can add a checkout: self step.
From the documentation: Deployment Jobs
A deployment job doesn't automatically clone the source repo. You can
checkout the source repo within your job with checkout: self.
Deployment jobs only support one checkout step.
This would look like something like this inside the YAML pipeline:
- stage: Deployment
jobs:
- deployment: DeployBicep
environment: $(Environment)
strategy:
runOnce:
deploy:
steps:
- checkout: self
- task: AzurePowerShell#4
displayName: "4.3) Deploy xxxxxx Synapse Infra"
enabled: true
inputs:
azureSubscription: ${{parameters.azureServiceConnection}}
ScriptType: "InlineScript"
azurePowerShellVersion: "LatestVersion"
continueOnError: true
errorActionPreference : "continue"
Inline: |
echo "Deploy Bicep template"
$deployment = New-AzResourceGroupDeployment `
-ResourceGroupName "rg-emdi-data-${{parameters.environment}}" `
-TemplateFile "$env:BUILD_SOURCESDIRECTORY\bicep\storageaccount.bicep" `
-envName "${{parameters.environment}}" `
-location "${{parameters.location}}" `
-storageId "$(storageID)" `

Download and publish artifact only if exist in azure pipeline

Using task of azure pipeline is it possible to download and publish artifact only if exists?
-> My first task is a build - but that build is a bit special, it will check if there is something that have changed since the last commit. If nothing has changed, nothing will be build.
-> The second task is the download artifact, but in the case nothing was build, there will be nothing to download.
Because no folder has been created by the build, my CI failed.
Is there a way to "tell" the download task: "download only if exist".
Or continue to task 3 only if folder created otherwise finish.
Thank you
You can use for this purpose variables and set them dynamically based on the fact if you are going to create your artifact or not
steps:
- bash: |
echo "Test here if you have folder which will you use to create an artifact and then set Yes or No"
echo "##vso[task.setvariable variable=doThing]Yes" #set variable doThing to Yes
name: DetermineResult
- script: echo "Job Foo ran and doThing is Yes."
condition: eq(variables['doThing'], 'Yes')
- script: echo "Skip this one"
condition: ne(variables['doThing'], 'Yes')
Based on your requirement, you can add a task before downloading artifacts task to determine whether there is a folder, and then decide whether to run the download artifacts task.
Here are the examples:
Check if the folder exists:
steps:
- powershell: |
$Folder = '$(Build.ArtifactStagingDirectory)'
"Test to see if folder [$Folder] exists"
if (Test-Path -Path $Folder) {
echo "##vso[task.setvariable variable=test]Yes"
} else {
echo "##vso[task.setvariable variable=test]No"
}
displayName: 'PowerShell Script'
- task: DownloadBuildArtifacts#1
displayName: 'Download Build Artifacts'
inputs:
downloadType: specific
condition: ne(variables['test'], 'Yes')
Check if the folder is empty:
steps:
- powershell: |
$directoryInfo = Get-ChildItem $(Build.ArtifactStagingDirectory) | Measure-Object
$directoryInfo.count
echo $directoryInfo.count
if ( $directoryInfo.count -eq 0 )
{
echo "##vso[task.setvariable variable=test]Yes"
}
else
{
echo "##vso[task.setvariable variable=test]No"
}
displayName: 'PowerShell Script'
- task: DownloadBuildArtifacts#1
displayName: 'Download Build Artifacts'
inputs:
downloadType: specific
condition: ne(variables['test'], 'Yes')

##[error]Required: 'ConnectedServiceNameARM' input --- Azure DevOps

##[error]Required: 'ConnectedServiceNameARM' input
This is in Azure DevOps using YAML inline script.
Need help with what to enter to fix this error? I am really new at YAML. This is a inline YAML and what tried seems to break the YAML script. The ConnectedServiceNameARM is just the Azure Subscription name? My service connection in azure devops has a working azure subscription name so I am wondering what is wrong?
Also need this YAML code to run so that the output file is placed in agent/_work/_tasks folder and not the artifacts folder. How would I move the file from the _tasks/Powershell folder to something that can be copied to share?
trigger:
- main
pool:
name: 'CloudUiPath001'
demands:
- agent.name -equals UiPathAgent01
steps:
- task: AzurePowerShell#3
displayName: 'Azure PowerShell script: InlineScript'
inputs:
ScriptType: InlineScript
Inline: |
$filePath='C:\Program Files (x86)\UiPath\Studio'
$dir=(New-Object -com scripting.filesystemobject).getFolder($filePath).ShortPath
$ProjectFilePath= "$(System.DefaultWorkingDirectory)/_TESTREPO7/project.json"
$ExecutableFilePath=$dir+"\UiPath.Studio.CommandLine.exe"
$OutputFilePath=".\$(Get-Date -Format 'yyyy-MM-dd-HH-mm-ss')-Workflow-Analysis.json"
#This was an attempt to write the filename to a pipeline variable: Feel free to continue on this path if possible
Write-Host "##vso[task.setvariable variable=jsonFilePath]$OutputFilePath"
Write-Output "$(Get-Date -Format 'HH:mm:ss') - STARTED - Workflow Analyzer CLI Script"
$Command = "$ExecutableFilePath analyze -p $ProjectFilePath"
Invoke-Expression $Command | Out-File -FilePath $OutputFilePath
Write-Output "$(Get-Date -Format 'HH:mm:ss') - COMPLETED - Workflow Analyzer CLI Script"
azurePowerShellVersion: LatestVersion
How do I fix his error within a INLINE YAML script. I am new to YAML and when I tried to enter a input I got errors.
##[error]Required: 'ConnectedServiceNameARM' input
According to your AzurePowerShell task definition, you don’t seem to specify the azureSubscription field.
steps:
- task: AzurePowerShell#5
displayName: 'Azure PowerShell script: InlineScript'
inputs:
azureSubscription: 'xxx'
ScriptType: InlineScript
Inline: xxx
azurePowerShellVersion: 'LatestVersion'
You can click the Settings shown in the figure below to specify the subscription.
About Azure PowerShell task, please refer to this document for details.
to go around this error change from task: AzurePowerShell#5 to pwsh:
- pwsh: |
InlineScript
displayName: 'Azure PowerShell script: InlineScript'

Access YAML variable in inline PowerShell script

If I have a YAML Pipeline;
variables:
- name : myVariable
value : 'abcd'
and if I want to run some inline powershell - how can i access that value (abcd) in the powershell script;
I expected this to work - but it didnt;
- task: PowerShell#2
displayName: "Do the thing"
inputs:
targetType: 'inline'
script:
write-host $(myVariable)
we could refer to this doc to specify variables at the pipeline, stage, or job level.
YAML build definition:
pool:
vmImage: 'vs2017-win2016'
variables:
- name : myVariable
value : 'abcd'
steps:
- task: PowerShell#2
displayName: "Do the thing"
inputs:
targetType: 'inline'
script:
write-host $(myVariable)
Result:
Not sure if this works in Azure DevOps, but in GitLab it's:
$VAR_NAME
or
$env:VAR_NAME
I'm using these two line in existing yaml and they work fine:
- $PKG_VERSION = (Get-ChildItem -Path . -Filter *.version).basename
- Write-Host $PKG_VERSION