How to copy specific folders/files to artifact? - azure-devops

I have the following build pipeline:
pool:
name: Azure Pipelines
demands:
- npm
- msbuild
steps:
- task: Npm#1
displayName: 'npm install'
inputs:
workingDir: Project123/Angular
verbose: false
steps:
- task: Npm#1
displayName: 'npm custom: angular build'
inputs:
command: custom
workingDir: Project123/Angular
verbose: false
customCommand: 'run-script build --prod --extractCss'
steps:
- task: NuGetCommand#2
displayName: 'NuGet restore'
steps:
- task: MSBuild#1
displayName: '.Net build'
inputs:
solution: 'Project123/*.csproj'
msbuildArchitecture: x64
configuration: Release
msbuildArguments: '/p:OutputPath=$(Build.ArtifactStagingDirectory)'
- task: CopyFiles#2
displayName: 'Copy Files to: $(Build.ArtifactStagingDirectory)'
inputs:
SourceFolder: Project123/Bundles
TargetFolder: '$(Build.ArtifactStagingDirectory)'
- task: PublishBuildArtifacts#1
displayName: 'Publish Artifact: Release'
inputs:
ArtifactName: Release
When we build the project from VS manually, this is the (expected) artifact we deploy:
However, the YAML I have generates this artifact instead:
How do I accomplish the expected artifact?
I am actually surprised that AngularOutput was copied in the root of the artifact, and not Bundles...I specified in the copy task to copy the Bundles folder , which would contain the AngularOutput...

Since I do not know the .csproj configuration, just provide a workaround based on the screenshot.
Don't need this Angular folder in this artifact
These Web.Debug and Web.Release are unneeded in the artifact
Add task power shell and delete the folder and files via below script
#Delete Angular folder and files
- task: PowerShell#2
inputs:
targetType: 'inline'
script: |
Remove-Item ''$(Build.ArtifactStagingDirectory)/_PublishedWebsites/Project123/Angular''
Remove-Item ''$(Build.ArtifactStagingDirectory)/_PublishedWebsites/Project123/Web.Debug.configure''
Remove-Item ''$(Build.ArtifactStagingDirectory)/_PublishedWebsites/Project123/Web.Release.configure''
Those dlls, .pdb, .xml and .configure files are not needed here.
There needs to be a 'Bundles' directory here which is generated by the angular build task
Check this Copy files task, these files in the folder Project123/Bundles, right?
- task: CopyFiles#2
displayName: 'Copy Files to: $(Build.ArtifactStagingDirectory)'
inputs:
SourceFolder: Project123/Bundles
TargetFolder: '$(Build.ArtifactStagingDirectory)'
We need to change the Copy file task as below:
- task: CopyFiles#2
displayName: 'Copy Files to: $(Build.ArtifactStagingDirectory)'
inputs:
SourceFolder: Project123/Bundles
TargetFolder: '$(Build.ArtifactStagingDirectory)/_PublishedWebsites/Project123/Bundles'
It will save these files in the folder Bundles instead of root.
This AngularOutput folder needs to be located under 'Bundles' directory inside 'Project123' folder above
We could copy the folder to project123 folder and then publish the artifact.
- task: CopyFiles#2
inputs:
SourceFolder: '$(Build.ArtifactStagingDirectory)/AngularOutput'
Contents: '**'
TargetFolder: '$(Build.ArtifactStagingDirectory)/_PublishedWebsites/Project123/AngularOutput'
You could run below YAML build and check the result.
pool:
name: Azure Pipelines
demands:
- npm
- msbuild
steps:
- task: Npm#1
displayName: 'npm install'
inputs:
workingDir: Project123/Angular
verbose: false
- task: Npm#1
displayName: 'npm custom: angular build'
inputs:
command: custom
workingDir: Project123/Angular
verbose: false
customCommand: 'run-script build --prod --extractCss'
- task: NuGetCommand#2
displayName: 'NuGet restore'
- task: MSBuild#1
displayName: '.Net build'
inputs:
solution: 'Project123/*.csproj'
msbuildArchitecture: x64
configuration: Release
msbuildArguments: '/p:OutputPath=$(Build.ArtifactStagingDirectory)'
- task: CopyFiles#2
displayName: 'Copy Files to: $(Build.ArtifactStagingDirectory)'
inputs:
SourceFolder: Project123/Bundles
TargetFolder: '$(Build.ArtifactStagingDirectory)/_PublishedWebsites/Project123/Bundles'
#Delete Angular folder and files
- task: PowerShell#2
inputs:
targetType: 'inline'
script: |
Remove-Item ''$(Build.ArtifactStagingDirectory)/_PublishedWebsites/Project123/Angular''
Remove-Item ''$(Build.ArtifactStagingDirectory)/_PublishedWebsites/Project123/Web.Debug.configure''
Remove-Item ''$(Build.ArtifactStagingDirectory)/_PublishedWebsites/Project123/Web.Release.configure''
- task: CopyFiles#2
inputs:
SourceFolder: '$(Build.ArtifactStagingDirectory)/AngularOutput'
Contents: '**'
TargetFolder: '$(Build.ArtifactStagingDirectory)/_PublishedWebsites/Project123/AngularOutput'
- task: PublishBuildArtifacts#1
displayName: 'Publish Artifact: Release'
inputs:
ArtifactName: Release
Update1
If the issue is delete the folder and files, please update the power shell script as below and try it again.
#Delete Angular folder and files
- task: PowerShell#2
inputs:
targetType: 'inline'
script: |
Remove-Item -Path $(Build.ArtifactStagingDirectory)/_PublishedWebsites/Project123/Angular -Recurse -Force
Remove-Item -Path $(Build.ArtifactStagingDirectory)/_PublishedWebsites/Project123/Web.Debug.configure -Recurse -Force
Remove-Item -Path $(Build.ArtifactStagingDirectory)/_PublishedWebsites/Project123/Web.Release.configure -Recurse -Force
And the test result:

Related

How can I include and use a powershell file from another repo

I want to use a generic powershell file in multiple build pipelines from a specific repo. This file I want to use it in my release stages to set our servers to maintenance mode an back to running.
At the moment I checkout the second repo in my build stage. But the DotNetCoreCLI#2 restore task does also a checkout and my powershell file disappears.
steps:
- checkout: templates
- checkout: self
- task: CopyFiles#2
displayName: 'Copy ADC Powershell script to: $(Build.ArtifactStagingDirectory)'
inputs:
SourceFolder: '$(Build.SourcesDirectory)/MYREPONAME/scripts/'
Contents: changestate.ps1
TargetFolder: $(Build.ArtifactsDirectory)
And then my task that runs the script doesn't find my powershell file
- task: PowerShell#2
displayName: 'Set $(Agent.MachineName) to maintenance'
inputs:
targetType: filePath
filePath: '$(System.ArtifactsDirectory)/${{ parameters.artifactName }}/changestate.ps1'
arguments: '-server $(AGENT.MACHINENAME) -state "maintenance"'
Has anyone maybe a better idea how can I do that?
Thanks for help.
Update: Solved in this way
I used a seperate stage and the publish task
- checkout: templates
- task: CopyFiles#2
displayName: 'Copy ADC Powershell script to: $(Build.ArtifactStagingDirectory)'
inputs:
SourceFolder: '$(Build.SourcesDirectory)/scripts/'
Contents: '*.ps1'
TargetFolder: $(Build.ArtifactStagingDirectory)
- task: PublishBuildArtifacts#1
displayName: "Publish artifact"
inputs:
pathtoPublish: '$(Build.ArtifactStagingDirectory)'
artifactName: '${{ parameters.artifactName }}'

Azure DevOps: Publishing a file created on a previous task wont work

My pipeline looks like this:
- task: PythonScript#0
displayName: "Create Excel sheet"
inputs:
scriptSource: 'filePath'
failOnStderr: false
scriptPath: /create_excel.py
- task: CopyFiles#2
inputs:
contents: '_buildOutput/**'
targetFolder: $(Build.ArtifactStagingDirectory)
- task: PublishBuildArtifacts#1
inputs:
pathToPublish: $(Build.ArtifactStagingDirectory)
artifactName: MyBuildOutputs
And the last line of the python code that it runs looks like this, it returns an excel sheet:
def create_excel():
...
return rn_df.to_excel(fr'rn{rel_ver}.xlsx', sheet_name=f"RN {rel_ver}", index=False)
But I see nothing is copied nor uploaded. What is the problem?
From the YAML sample, the default working directory of the python task is $(build.sourcesdirectory).
When the task runs the python file, it will create the excel sheet in the working directory.
To solve this issue, you can change the $(Build.ArtifactStagingDirectory) to $(build.sourcesdirectory) in the next tasks.
For example:
- task: PythonScript#0
displayName: "Create Excel sheet"
inputs:
scriptSource: 'filePath'
failOnStderr: false
scriptPath: /create_excel.py
- task: CopyFiles#2
inputs:
contents: '_buildOutput/**'
targetFolder: $(build.sourcesdirectory)
- task: PublishBuildArtifacts#1
inputs:
pathToPublish: $(build.sourcesdirectory)
artifactName: MyBuildOutputs

Azure Pipeline Copy Secure file into build folder

I've a vite/svelte project which uses .env files for environment settings. I also have an Azure Pipeline which contains a secure file .env.staging this is on the .gitignore list of the associated repo. I'd like to download this secure file, copy it to my build directory and then have it's contents read when I run vite build --mode staging (well, npm run build:staging which includes vite build...)
When run locally from my machine npm run build:staging works as expected and reads the .env.staging file, however it seems to get ignored when used in the pipeline, am I doing anything wrong?
Here's my yml.
trigger:
- main
pool:
vmImage: 'ubuntu-latest'
steps:
- task: DownloadSecureFile#1
name: "dotenvStaging"
inputs:
secureFile: '.env.staging'
displayName: "Download .env.staging"
- task: NodeTool#0
inputs:
versionSpec: 14.15.4
displayName: "Install Node.JS"
- task: CopyFiles#2
inputs:
contents: "$(Agent.TempDirectory)/.env.staging"
targetFolder: "$(Agent.BuildDirectory)"
displayName: "Import .env.staging"
- script: npm install
displayName: "npm install"
- script: npm run build:staging
displayName: "npm run build:staging"
- task: ArchiveFiles#2
inputs:
rootFolderOrFile: 'dist'
archiveType: 'zip'
archiveFile: '$(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip'
#replaceExistingArchive: true
#verbose: # Optional
#quiet: # Optional
displayName: "Create archive"
- task: PublishBuildArtifacts#1
inputs:
PathtoPublish: '$(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip'
ArtifactName: 'drop'
publishLocation: 'Container'
displayName: "Publish archive"
I'm not sure if CopyFiles#2 is doing what I expect or not as it just matches the content parameter to copy whatever files match, which could be 0 if I'm writing it wrong...
Another note, I also tried using $(dotenvStaging.secureFilePath) as the content parameter, but it doesn't seem to do anything either.
Naturally I figured it out as soon as I posted, I needed to update the CopyFiles part to specify sourceFolder, clearly it didn't like my absolute file path for content.
- task: CopyFiles#2
inputs:
sourceFolder: "$(Agent.TempDirectory)"
contents: ".env.staging"
targetFolder: "$(Agent.BuildDirectory)"
displayName: "Import .env.staging"

Adding artifacts at one level up in root folder

I have an artifact called onnxruntime.dll which are downloaded from pipeline and its folder structure is like this MyProject_x64_windows/bin/Nodes/onnxruntime.dll . I would like this artifact to be downloaded at one level up i.e. MyProject_x64_windows/bin/onnxruntime.dll
I am not sure how it is getting downloaded at that level and how can I fix this. I cant copy the complete YAML but am providing the one which I think is required:
variables:
IppRoot: $(Build.SourcesDirectory)/packages/IPP
ONNXXRoot: $(Build.SourcesDirectory)/packages/ONNXRuntime
- stage: MyProject
jobs:
- job: MyProject_Build
strategy:
matrix:
win:
imageName: 'windows-2019'
OrzRootSuffix: 'x64-windows-staticlib'
osSuffix: 'windows'
LibFT4222Suffix: 'windows'
matlabVersion: '9.6.0-2'
extraCmakeOptions: '-D MyProject_ONNX_SUPPORT=On
-D ONNX_RUNTIME_ROOT:PATH=$(ONNXRoot)'
pool:
vmImage: $(imageName)
steps:
- checkout: self
lfs: true
- task: UniversalPackages#0
displayName: 'Download pre-build ONXX Runtime headers and libraries'
inputs:
command: 'download'
vstsFeed: 'MyProjectPackages'
vstsFeedPackage: 'microsoft.ml.onxxruntime'
vstsPackageVersion: '*' # use the latest
downloadDirectory: '$(ONNXRoot)'
- download: SCMockPipeline
displayName: Download SCMock
artifact: scmock
condition: eq(variables['Agent.OS'], 'Windows_NT')
- script: python -m pip install jinja2
displayName: Install python jinja2 template engine
- task: CMake#1
displayName: CMake configure
inputs:
workingDirectory: '$(Build.BinariesDirectory)'
cmakeArgs: '-G Ninja
$(extraCmakeOptions)
-DLibFT4222_ROOT=$(LibFT4222Root)
-DIPP_ROOT=$(IppRoot)
-DCMAKE_BUILD_TYPE=Release
-DCMAKE_INSTALL_PREFIX=$(Build.ArtifactStagingDirectory)
$(Build.SourcesDirectory)'
- task: CMake#1
displayName: CMake build
inputs:
workingDirectory: '$(Build.BinariesDirectory)'
cmakeArgs: '--build . --target install'
- task: PublishPipelineArtifact#1
displayName: 'Publish MyProject'
inputs:
targetPath: $(Build.ArtifactStagingDirectory)
artifactName: 'MyProject_x64-$(osSuffix)'
- task: DownloadPipelineArtifact#2
displayName: Download MyProject artifact
inputs:
artifact: 'MyProject_x64-$(osSuffix)'
path: '$(Build.SourcesDirectory)/MyProject_x64-$(osSuffix)'
You can try to set the destination directory in the DownloadPipelineArtifact task to download the artifact to the bin folder.
- task: PublishPipelineArtifact#1
displayName: 'Publish MyProject'
inputs:
targetPath: $(Build.ArtifactStagingDirectory)/bin/Nodes/onnxruntime.dll
artifactName: 'MyProject_x64-$(osSuffix)'
- task: DownloadPipelineArtifact#2
displayName: Download MyProject artifact
inputs:
artifact: 'MyProject_x64-$(osSuffix)'
path: '$(Build.SourcesDirectory)/MyProject_x64-$(osSuffix)/bin'

How do I add additional files to the Azure DevOps artifacts?

My Azure DevOps pipeline is successfully packing up my web service into a zip file with the following task:
- task: DotNetCoreCLI#2
displayName: Pack Artifacts
inputs:
command: 'publish'
publishWebProjects: false
arguments: '"Web Service" --configuration $(BuildConfiguration) --output $(Build.ArtifactStagingDirectory)'
This deposits the entire contents of the project in a zip file in 'drop\a.zip'. Now, I have a script (setup.ps1) that I want to run to open up the firewall and an SSL certificate that needs to be installed. I want to add them to the same zip file that was the target for the 'publish' operation. I tried this:
- task: CopyFiles#2
inputs:
SourceFolder: 'Additional Files'
Contents: '*'
TargetFolder: '$(Build.ArtifactStagingDirectory)'
flattenFolders: true
But it just added the files in the 'Additional Files' directory to the "\drop" directory alongside the a.zip file:
drop
a.zip
setup.ps1
www_mysite_com.pfx
How would I go about adding the additional files to the zip file that's created as part of the build and publish steps. This must be a common problem. What's the common solution?
You have three options:
unpack archive, copy file and pack again
add file to archive - like it is shown here
add your file to solution and make dotnet publish responsible for putting file in archive like it is shown here
Your artifacts directory should be
drop
a.zip
And now, you want to add setup.ps1 and www_mysite_com.pfx to the folder a, right?
YAML sample:
- task: DotNetCoreCLI#2
displayName: Pack Artifacts
inputs:
command: 'publish'
publishWebProjects: false
arguments: '"Web Service" --configuration $(BuildConfiguration) --output $(Build.ArtifactStagingDirectory)'
#Extract a.zip file to $(Build.ArtifactStagingDirectory)/drop/a folder
- task: ExtractFiles#1
inputs:
archiveFilePatterns: '$(Build.ArtifactStagingDirectory)/drop/a.zip'
destinationFolder: '$(Build.ArtifactStagingDirectory)/drop/a'
cleanDestinationFolder: false
overwriteExistingFiles: false
#Delete PrescQIPPWebApp.zip file
- task: PowerShell#2
inputs:
targetType: 'inline'
script: 'Remove-Item ''$(Build.ArtifactStagingDirectory)/drop/a.zip'''
#copy these files to the folder a
- task: CopyFiles#2
inputs:
SourceFolder: 'Additional Files'
Contents: |
setup.ps1
www_mysite_com.pfx
TargetFolder: '$(Build.ArtifactStagingDirectory)/drop/a'
OverWrite: true
flattenFolders: true
#Archive folder a to a.zip
- task: ArchiveFiles#2
inputs:
rootFolderOrFile: '$(Build.ArtifactStagingDirectory)/drop/a'
includeRootFolder: true
archiveType: 'zip'
archiveFile: '$(Build.ArtifactStagingDirectory)/drop/a.zip'
replaceExistingArchive: true
#Delete a folder
- task: PowerShell#2
inputs:
targetType: 'inline'
script: 'Remove-Item -path ''$(Build.ArtifactStagingDirectory)/drop/a'' -Recurse -Force -EA SilentlyContinue -Verbose'
#And now, we could the a.zip contain the file setup.ps1 and www_mysite_com.pfx.
We could also check this Thread Adding additional files to Azure Build Pipeline