azure-devopsazure-pipelines-yaml

How to deploy files to a server


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'

Solution

  • The CopyFiles task can do the following things on the agent machine:


    You can set the target machine as an environment resource in the Azure DevOps project where you run the pipeline:

    1. Go to Pipelines > Environments to create an environment. You can select "None: You can add resources later" when creating the environment.

      enter image description here

    2. Create a PAT for use. The PAT should have the scope "Environment (Read & manage)" at least.

      enter image description here

    3. 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.

    4. 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.

      • Enter Y (Yes) to allow the deploy agent to run as service.
      • Provide the user account and password that can logon the agent machine. The agent service will use this user account to logon the machine to run the the pipeline tasks. Ensure the logon user account has the permissions to access the files and folders on the agent machine. If you need to access file share in pipeline, the logon user account also should has the permissions to access the file share.

      enter image description here

    5. After successfully installing the deploy agent, you can see it is listed as resource on the environment created previously.

    enter image description here

    1. In the YAML pipeline, you can directly use the resource on the deployment job.
      # 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: