Deploying over SFTP with Azure DevOps pipelines

I recently needed to deploy some files to a webserver over SFTP using Azure DevOps pipelines. Despite the name SFTP is not related to FTP or FTPS as its SSH-based, so the FtpUpload@2 task won’t work for us. What we’re actually looking for is the CopyFilesOverSSH task. Let’s see how to configure it properly.

First we need an SSH service connection:

Search for SSH as service connection type
Add the SFTP hostname and credentials for the service connection

Afterwards we create our Azure Pipline as YAML file. Be aware that if you want to clean your target folder before deploying the service user needs full SSH permissions, not just SFTP.

trigger:
branches:
include:
main
pool:
vmImage: 'ubuntu-latest'
stages:
stage: Deploy_Preview
jobs:
job: Deploy
steps:
task: CopyFilesOverSSH@0
inputs:
sshEndpoint: 'My SFTP Service Connection'
sourceFolder: '$(Pipeline.Workspace)/s/myrepo'
contents: '**'
targetFolder: 'mytargetfolder'
cleanTargetFolder: true # requires SSH permissions in addition to SFTP
overwrite: true

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.