• Stars
    star
    155
  • Rank 240,864 (Top 5 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 12 years ago
  • Updated almost 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Grunt task for code deployment over sftp

grunt-sftp-deploy

This is a grunt task for code deployment over the sftp protocol. It is mostly a copy of grunt-ftp-deploy, but uses ssh2 to provide sftp access instead of jsftp. And when I say "mostly a copy," I mean I stole it all and added sftp. Including most of this readme.

These days git is not only our goto code management tool but in many cases our deployment tool as well. But there are many cases where git is not really fit for deployment:

  • we deploy to servers with only sftp access
  • the production code is a result of a build process producing files that we do not necessarily track with git

This is why a grunt task like this would be very useful.

For simplicity purposes this task avoids deleting any files and it is not trying to do any size or time stamp comparison. It simply transfers all the files (and folder structure) from your dev / build location to a location on your server.

npm version dependency

NPM NPM

Usage

To use this task you will need to include the following configuration in your Gruntfile file:

'sftp-deploy': {
  build: {
    auth: {
      host: 'server.com',
      port: 22,
      authKey: 'key1'
    },
    cache: 'sftpCache.json',
    src: '/path/to/source/folder',
    dest: '/path/to/destination/folder',
    exclusions: ['/path/to/source/folder/**/.DS_Store', '/path/to/source/folder/**/Thumbs.db', 'dist/tmp'],
    serverSep: '/',
    localSep: '/',
    concurrency: 4,
    progress: true
  }
}

and load the task:

grunt.loadNpmTasks('grunt-sftp-deploy');

The parameters in our configuration are:

  • host - the name or the IP address of the server we are deploying to
  • port - the port that the sftp service is running on
  • authKey - a key for looking up the saved credentials
  • cache - boolean/string - put false or omit this parameter for no cache, otherwise provide path to cache file, so overwriten are only the files that has been changed (it works only for your local copy, so if someone else uploaded something meanwhile it will not be catched). Path is relative to your gruntfile, so you can easily clear cache by erasing this json file. This file should be omitted from source control.
  • src - the source location, the local folder that we are transferring to the server
  • dest - the destination location, the folder on the server we are deploying to
  • exclusions - an optional parameter allowing us to exclude files and folders by utilizing grunt's support for minimatch. Please note that the definitions should be relative to the project root
  • serverSep - an optional parameter allowing you to define the server separator in case it differs from your local environment. Useful if you deploy from Windows to Unix
  • localSep - an optional parameter allowing you to define your separator in case it differs from your local environment. Useful if you deploy from Windows git-bash to Unix - in this case set this to '\\'
  • concurrency - an optional parameter to define the number of concurrent file transfer, default to 4
  • progress - an optional parameter to display or not the progress bar

The progress bar is also disabled when --verbose switch is used.

Authentication parameters

Usernames, passwords, and private key references are stored as a JSON object either in a file named .ftppass or in an environment variable. The .ftppass file should be omitted from source control. The file or variable uses the following format:

{
  "key1": {
    "username": "username1",
    "password": "password1"
  },
  "key2": {
    "username": "username2",
    "password": "password2"
  },
  "privateKey": {
    "username": "username"
  },
  "privateKeyEncrypted": {
    "username": "username",
    "passphrase": "passphrase1"
  },
  "privateKeyCustom": {
    "username": "username",
    "passphrase": "passphrase1",
    "keyLocation": "/full/path/to/key"
  },
  "privateKeyCustomByFileName": {
    "username": "username",
    "passphrase": "passphrase2",
    "keyLocation": "filename-of-key"
  },
  "sshAgentSocket": {
    "username": "username",
    "agent": true
  },
  "pageant": {
    "username": "username",
    "agent": "pageant"
  }
}

If keyLocation is not specified, grunt-sftp-deploy looks for keys at ~/.ssh/id_dsa and /.ssh/id_rsa.

You can supply passwords for encrypted keys with the passphrase attribute.

This way we can save as many username / password combinations as we want and look them up by the authKey value defined in the grunt config file where the rest of the target parameters are defined.

If you use agent-based SSH authentication, you can set agent to true to use ssh-agent's UNIX socket. When on Windows, you can set it -- to "pageant" for authenticating with Pageant or to true to use a cygwin socket which is read from the SSH_AUTH_SOCK environment variable.

To use an environment variable instead of the .ftppass file, add the JSON string to your ~/.bashrc (or equivalent), for example:

export GRUNT_SFTP="{
                    \"username\": \"your-username\",
                    \"keyLocation\": \"path/to/your/ssh/key\",
                    \"passphrase\": \"the-key-password\",
                  }"

Then you simply pass the variable name as the sftp-deploy tasks's authKey parameter inside your Gruntfile.js

If you are fetching credentials from different sources like internal grunt config objects or shell prompt there is another option. You can pass the object with key:value pairs like they are stored in .ftppass as authKey parameter.

Dependencies

This task is built by taking advantage of the great work of Brian White and his ssh2 NodeJS module.

More Repositories

1

dropbox-taskpaper-editor

The Web-based Dropbox-synced Taskpaper Document Editor
JavaScript
30
star
2

dokku-bower-grunt-build-plugin

Add build steps for bower and grunt
Shell
22
star
3

mathr

A reactive notes app that can do math for you using data tables and maybe some python-like functions
JavaScript
15
star
4

s3staticuploader

A fully S3-hosted file uploader app. Uses HTML5 CORS to accept uploads to S3.
JavaScript
13
star
5

Frontend-Code-Test

HTML
10
star
6

Bogart

Sinatra for PHP - FYI this is old and unmaintained!
PHP
10
star
7

QDOS

A web-based QDOS clone.
HTML
7
star
8

monkey-typescript

An interpreted language for the browser and NodeJS, based on Monkey
TypeScript
7
star
9

Anatomy

This project has been majorly one-upped. Use this instead: http://html5reset.org/ or http://html5boilerplate.com/
JavaScript
4
star
10

sentinel-simulator-docker

A Docker image for HashiCorp Sentinel
Dockerfile
4
star
11

terraform-kitchen-sink

A single Terraform config to cover most cases of address and output types.
HCL
3
star
12

marvel-mini

MARVEL MINI A showcase of Marvel comics and creators.
JavaScript
3
star
13

dom-tree-viewer

A modal that displays the DOM of the current page as a tree.
CSS
3
star
14

lunch

What's for lunch?
JavaScript
2
star
15

s3url

s3url copies files and streams to a public S3 file and copies the url to your clipboard.
PHP
2
star
16

pauls-code-snippets

This is my spot for random code snippets that I'd like to save.
PHP
2
star
17

iphone-christmas-tree

I'm trying to work out an iPhone app. Here lies my adventure.
Objective-C
2
star
18

jenngen

AI static site generator: $ npx jenngen .
JavaScript
2
star
19

textorize-server

Generates and serves up textorize images for loading via url in img src.
PHP
2
star
20

my-tfe

Builds my TFE environment.
HCL
2
star
21

monkey-playground

A browser playground for monkey-typescript
JavaScript
1
star
22

thrashr888.github.com

Paul's GitHub page
1
star
23

the-rogue-clicker

A rogue-like clicker game
JavaScript
1
star
24

terraform-monorepo-example

An example of using a monorepo with TFC.
HCL
1
star
25

dotfiles

Shell
1
star
26

Scour

Another abandoned project
Swift
1
star
27

terraform-gcp-example

A basic GCP-in-terraform setup
HCL
1
star
28

pear

Distributed Express.js Server WIP
JavaScript
1
star
29

terraform-cert-example

HCL
1
star
30

terraform-initial-d

An S3 bucket you can drift in terraform
HCL
1
star
31

no-readme

Just an example of a repo with no readme.
JavaScript
1
star
32

terraform-apply-fail

A terraform config that fails on apply but not plan.
HCL
1
star
33

the-reg

DEAD PROJECT - DO NOT USE
Go
1
star
34

classy-express

A simple class-based web framework for Express
JavaScript
1
star
35

terraform-provider-keychain

A Terraform provider for the macOS Keychain
Go
1
star
36

terraform-random

HCL
1
star
37

Bogart-sandbox

A sandbox app for Bogart. THIS IS PRE-ALPHA. HIGHLY UNRECOMMENDED FOR PRODUCTION USE.
PHP
1
star
38

terraform-outputs

A hopefully exhaustive example of the many forms and types of terraform outputs.
HCL
1
star
39

terraform_013_example

an example of a terraform 0.13 config
HCL
1
star
40

terraform-postconditions-example

HCL
1
star
41

terraform-azure-example

HCL
1
star
42

terraform-env

prints out your `env` values
HCL
1
star
43

cool-website

My Cool Website
HTML
1
star
44

angular-animation-test

just testing out angular animation options
CSS
1
star
45

terraform-empty

A terraform config with no resources.
HCL
1
star
46

Bogart-sandbox-shared

A sandbox app for Bogart that plays nice on shared servers. THIS IS PRE-ALPHA. HIGHLY UNRECOMMENDED FOR PRODUCTION USE.
PHP
1
star
47

hashidocs

Creates mdx files to upload to HashiBot, a GPT
Python
1
star