Copy a selection of files to a server using Azure Devops

Viewed 71

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.
1 Answers

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