Copy a selection of files to a server using Azure Devops - azure-devops

I am trying to copy a selection of files to a destination folder on a target machine.
In my first version, I can already copy all files to the destination. Therefore, I use the following task to build an artifact.
steps:
- task: CopyFiles#2
displayName: 'copy files'
inputs:
SourceFolder: $(workingDirectory)
Contents: '**/files/*'
flattenFolders: true
targetFolder: $(Build.ArtifactStagingDirectory)
- task: PublishBuildArtifacts#1
inputs:
pathToPublish: $(Build.ArtifactStagingDirectory)
artifactName: files
Later I try to use that artifact for a deployment
stage: Deploy
displayName: 'Deploy files to destination'
jobs:
- deployment: VMDeploy
displayName: 'download artifacts'
pool:
vmImage: 'ubuntu-latest'
environment:
name: local_env
resourceType: VirtualMachine
strategy:
runOnce:
deploy:
steps:
- task: DownloadPipelineArtifact#2
displayName: 'download files'
inputs:
artifact: dags
downloadPath: /opt/myfolder/files
This works perfectly fine for all files.
But what I need is the following:
The 'local_env' environment contains multiple servers. The first three letters of each server would be the perfect wild card for the files I needed.
Or in other words, if the environment contains names such as 'Capricorn', 'Aries', 'Pisces', I would like to copy 'cap*.* ', ari*.* ' or 'pis*.*' on the corresponding server.

The way I fixed it for now was
- task: Bash#3
inputs:
targetType: 'inline'
script: "HN=$(hostname | head -c 3) \n cd /opt/myfolder/files/ \n rm -r $(ls -I \"$HN*.*\")"
It does its job, but I am open to mark a better solution as resolution.

Related

Azure Devops - Can not retrieve artifact

I am publishing a artifact in a job (which completes successfully). In a separate job, I am trying to retrieve the artifact. However, nothings shows in the ls $(Build.ArtifactStagingDirectory). Why is this?
- task: PublishPipelineArtifact#1
displayName: 'Publish Artifact'
inputs:
targetpath: 'anatomy/dist'
artifact: 'anatomy-artifact'
publishLocation: 'pipeline'
- job: deployBucket
displayName: 'Deploy Bucket'
dependsOn: buildAnatomy
condition: eq(dependencies.buildAnatomy.result,'Succeeded')
pool:
vmImage: "ubuntu-20.04"
steps:
- task: Bash#3
displayName: "Upload contents to bucket"
inputs:
targetType: "inline"
script: |
ls -al '$(Build.ArtifactStagingDirectory)'
Each job runs in its own space and the agent cleans up after itself by default. If you want the contents of the artifact to be available in the 2nd job, you need to fetch it again.
Use the DownloadPipelineArtifact#1 task at the start of your 2nd job.
Or move the deploy command into the same job.

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 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'

Azure pipeline - unzip artefact, copy one directory into Azure blob store YAML file

I am getting stuck with Azure pipelines.
I have an existing node SPA project that needs built for each environment (TEST and PRODUCTION). This i can do, but need to have a manual step when pushing to PROD. I am using Azure Dev-op pipeline environments with Approval and Checks to mandate this.
The issue is using a 'deploy job' to take an artefact from a previous step I am unable to find the right directory. This is my YAML file have so far:
variables:
# Agent VM image name
vmImageName: 'ubuntu-latest'
trigger:
- master
# Don't run against PRs
pr: none
stages:
- stage: Development
displayName: Devlopment stage
jobs:
- job: install
displayName: Install and test
pool:
vmImage: $(vmImageName)
steps:
- task: NodeTool#0
inputs:
versionSpec: '12.x'
displayName: 'Install Node.js'
- script: |
npm install
displayName: Install node modules
- script: |
npm run build
displayName: 'Build it'
# Build creates a ./dist folder. The contents will need to be copied to blob store
- task: ArchiveFiles#2
inputs:
rootFolderOrFile: '$(Build.BinariesDirectory)'
includeRootFolder: true
archiveType: 'zip'
archiveFile: '$(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip'
replaceExistingArchive: true
verbose: true
- deployment: ToDev
environment: development
dependsOn: install
strategy:
runOnce:
deploy:
steps:
- task: DownloadPipelineArtifact#2
inputs:
buildType: 'current'
targetPath: '$(Pipeline.Workspace)'
- task: ExtractFiles#1
inputs:
archiveFilePatterns: '**/*.zip'
cleanDestinationFolder: true
destinationFolder: './cpDist/'
# Somehow within a deploy job retrieve the .zip artefact, unzip, copy the ./dist folder into the blob store
- task: AzureCLI#2
inputs:
azureSubscription: MYTEST-Development
scriptLocation: "inlineScript"
scriptType: "bash"
inlineScript: |
az storage blob upload-batch -d \$web --account-name davey -s dist --connection-string 'DefaultEndpointsProtocol=https;AccountName=davey;AccountKey=xxxxxxx.yyyyyyyyy.zzzzzzzzzz;EndpointSuffix=core.windows.net'
displayName: "Copy build files to Development blob storage davey"
- script: |
pwd
ls
cd cpDist/
pwd
ls -al
displayName: 'list'
- bash: echo "Done"
If you are confused with the folder path, you could add few debug steps to check the location of know system variables to understand what was going on using a powershell script as below:
- task: PowerShell#2
displayName: 'Degug parameters'
inputs:
targetType: Inline
script: |
Write-Host "$(Build.ArtifactStagingDirectory)"
Write-Host "$(System.DefaultWorkingDirectory)"
Write-Host "$(System.ArtifactsDirectory)"
Write-Host "$(Pipeline.Workspace)"
Write-Host "$(System.ArtifactsDirectory)"
You should simply publish the build generated artifacts to drop folder.
Kindly check this official doc -- Artifact selection , in there is explaining that you can define the path which to download the artifacts to with the following task:
steps:
- download: none
- task: DownloadPipelineArtifact#2
displayName: 'Download Build Artifacts'
inputs:
patterns: '**/*.zip'
path: '$(Build.ArtifactStagingDirectory)'
Please be aware that the download happens automatically to $(Pipeline.Workspace), so if you don’t want you deployment to download the files twice, you need to specify the “download: none” in your steps.