I have a very basic requirement: deploy files (could be .NET binaries, configuration, assets...) to an on-premise server, using an Azure DevOps YAML pipeline.
The files are saved as artifacts from the build pipeline with the publish
step.
They can be retrieved with the download
step.
The target machine is registered in an environment.
Using a deployment
job should be the solution but I'm missing the last step:
trigger: none
resources:
pipelines:
- pipeline: tp
source: Test_Publish
jobs:
- deployment:
environment: Test
strategy:
runOnce:
deploy:
steps:
- script: cat $(Pipeline.Workspace)/tp/output1/test.txt
???
The artifacts are retrieved on the agent: the cat
command displays the file's content as expected.
But what to put for ???
.
I've naively tried some CopyFiles
but of course as the source and target folders are not on the same machine it does not work (though oddly the copy command is "successful"):
CopyFiles@2
inputs:
SourceFolder: '$(Pipeline.Workspace)/tp/output1'
Contents: '**'
TargetFolder: 'C:\TestAzureDevOps\Deployment'
The CopyFiles
task can do the following things on the agent machine:
Copy files from a local directory of the agent machine to another local directory of the agent machine.
Copy files from a local directory of the agent machine to a file share which can be accessible from the agent machine. The file share also can be one that has been mapped as a drive on the agent machine.
Copy files from an accessible file share to a local directory of the agent machine.
Copy files from an accessible file share to another accessible file share on the agent machine.
You can set the target machine as an environment resource in the Azure DevOps project where you run the pipeline:
Go to Pipelines > Environments to create an environment. You can select "None: You can add resources later
" when creating the environment.
Create a PAT for use. The PAT should have the scope "Environment (Read & manage)
" at least.
Download the latest available installation package of self-hosted agents based on the OS of your target machine. Unpack the package to a specified installation directory on the target machine.
In the installation directory, open the command prompt as admin, for example, open PowerShell as admin on Windows machine. Run the command ".\config.cmd --environment
" to start installing the deploy agent to the environment. Provide the required information step by step.
Yes
) to allow the deploy agent to run as service.After successfully installing the deploy agent, you can see it is listed as resource on the environment created previously.
# azure-pipelines.yml
jobs:
- deployment: deploy
environment:
name: 'EnvVM'
resourceType: virtualMachine
resourceName: 'Win11-01'
strategy:
runOnce:
deploy:
steps:
. . .
With this configuration, all the tasks in the deployment job will run on the agent machine (target machine). So, the download
task runs in the deployment job also will download the artifact files into the working directory on the agent machine. Then you can use the CopyFiles
task to copy the artifact files to any of other directories or the accessible file share on the agent machine.
Related documentation: