Reputation: 1569
I am really new to the Powershell and want to copy a file from BUILD_SOURCESDIRECTORY environment variable to a zip folder inside BUILD_STAGINGDIRECTORY environment variable in my VSTS build definition.
if(Test-Path $Env:BUILD_SOURCESDIRECTORY/MyFolder/MyFIle.txt)
{
Write-Host "Source File: $Env:BUILD_SOURCESDIRECTORY/MyFolder/MyFIle.txt"
Write-Host "Target Location: $Env:BUILD_STAGINGDIRECTORY\StagingDirectoryFolder.zip\TestFolder"
}
Copy file from one path to another is quite straight forward but I really don't know how to move file into the zip folder structure.
Upvotes: 0
Views: 1208
Reputation: 38116
You can copy MyFIle.txt
to the subfolder TestFolder
under a zip file by Expand-Archive
and Compress-Archive
(as gvee mentions). The PowerShell script as below:
if(Test-Path $(Build.SourcesDirectory)\MyFolder/MyFIle.txt)
{
clear-host
[string]$zipF = '$(Build.ArtifactStagingDirectory)\StagingDirectoryFolder.zip'
[string]$fileToZip = '$(Build.SourcesDirectory)\MyFolder\MyFIle.txt'
[string]$tempEx= '$(Build.SourcesDirectory)\temp'
[string]$copyDes='$(Build.SourcesDirectory)\temp\TestFolder'
Expand-Archive -Path $zipF -DestinationPath $tempEx -Force
Copy-Item $fileToZip -Destination $copyDes -Force
Compress-Archive -Path $tempEx\* -Update -DestinationPath $zipF
Remove-Item -Recurse -Force $tempEx
}
Now the zip file $(Build.ArtifactStagingDirectory)\StagingDirectoryFolder.zip
contains MyFIle.txt
under $(Build.ArtifactStagingDirectory)\StagingDirectoryFolder.zip\TestFolder
.
Upvotes: 0
Reputation: 1400
If I could suggest a non power shell solution (although it is worth looking up the Expand-Archive and Compress-Archive cmdlets as recommended in the comments.)
I would use a Archive Files
build task to handle the zipping. In your power shell build script, copy your artifact(s) into $ENV:BUILD_BINARIESDIRECTORY
, and then leverage the VSTS build to do the archiving of all of the files.
This then lets you publish that zip file using the VSTS build which will allow it to be easily accessible through the VSTS web gui which imo offers a superior user experience (for troubleshooting your build, as well as other users who need access to those artifacts (either physical people, or automated processes)). If you need to do something else with the zip file, you could then add another powershell script after your archive files
that would be able to access the file from the $ENV:BUILD_ARTIFACTSTAGINGDIRECTORY
. This way your scripts stay simple, and you can offload some of your build maintenance onto Microsoft.
Upvotes: 1