Search code examples
azure-devopsazure-pipelinescicd

FilePatterns for Azure Pipelines are not working as expected when using FTP-Upload


I want to upload my project via FTP to my webserver. Therefore I added a Pipeline in AzureDevOps. I have some folders (which have files and again some folders in it), which should not be uploaded to the webserver. For example the folders .git, .idea, blog and node_modules. My pipeline scripts looks like that:

trigger:
- test-stage

pool:
  vmImage: 'ubuntu-latest'

steps:
- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: 'Copy-Item -Path $(System.DefaultWorkingDirectory)/.htaccess_test -Destination $(System.DefaultWorkingDirectory)/.htaccess -force'
- task: FtpUpload@2
  inputs:
    credentialsOption: 'inputs'
    serverUrl: '$(ftp-url)'
    username: '$(ftp-username)'
    password: '$(ftp-password)'
    rootDirectory: '$(System.DefaultWorkingDirectory)'
    remoteDirectory: '/test'
    trustSSL: true
    enableUtf8: false
    preservePaths: true
    filePatterns: |
      **
      !**/.git/**
      !**/.idea/**
      !**/blog/**
      !**/node_modules/**

Even tho the 4 folders are excluded, the content is being loaded on the webserver, for example here: enter image description here

So my question is: What is wrong with my pipeline script, especially with the filePatterns?


Solution

  • I can reproduce the issue with the FtpUpload task used. But the filePatterns works as expected in CopyFiles task.

    So, as a workaround we can copy the project to a temp folder using the CopyFiles task with the filePatterns set, then upload the files from the temp folder using the FtpUpload task.

    For example:

    - task: CopyFiles@2
      inputs:
        SourceFolder: '$(System.DefaultWorkingDirectory)'
        Contents: |
          **
          !**/.git/**
          !**/.idea/**
          !**/blog/**
          !**/node_modules/**
        TargetFolder: '$(Build.ArtifactStagingDirectory)\upload'
    
    - task: FtpUpload@2
      inputs:
        credentialsOption: 'inputs'
        serverUrl: '$(ftp-url)'
        username: '$(ftp-username)'
        password: '$(ftp-password)'
        rootDirectory: '$(Build.ArtifactStagingDirectory)\upload'
        filePatterns: '**'
        remoteDirectory: '/upload/$(Build.BuildId)/'
        clean: false
        cleanContents: false
        preservePaths: true
        trustSSL: false
        enableUtf8: false