vinyl-ftp hide credentials issue

301 Views Asked by At

I decided to use vinyl-ftp for my deployment process in gulp. One thing I would like to do is to have a separate file with my ftp credentials:

  1. host

  2. user

  3. password

    and put that file in my .gitignore. And then I want those credentials in that file be read by my connection variable in my gulp file. My deploy code is the following:

    gulp.task( 'deploy', function () {
    var conn = ftp.create( {
    host:     'yourdomain.com',
    user:     'ftpusername',
    password: 'ftpuserpassword',
    parallel: 10,
    log:      gutil.log
    } );
    
    var globs = [
    'dist/**'
    ];
    
    return gulp.src( globs, { base: './dist/', buffer: false } )
    .pipe( conn.newer( 'yourdomain.com' ) )
    .pipe( conn.dest( 'yourdomain.com' ) );
    
    } );//end deploy
    

So I want the values of the variables yourdomain.com for the host, ftpusername for user and ftpuserpassword for password be read in from a separate file so my credentials show up when I do git push. How do I accomplish this?

Thanks

1

There are 1 best solutions below

0
On

You can pass them as run args:

var 
    gulp           = require('gulp'),
    args           = require('yargs').argv;
const distDir = "./dist";
gulp.task('deploy', function() {
    var conn = ftp.create({
        host:      args.host,
        user:      args.user,
        password:  args.password,
        parallel:  10,
        log: flog // .log
    });

    var globs = [
    distDir + '/**',
    distDir + '/.htaccess',
    ];
    return gulp.src(globs, {buffer: false})  
    .pipe(conn.dest(args.remotedir));

});

Then call it from command line or put the line in a batch file: npm run gulp deploy -- --host=hostname --user=username --password=password --remotedir=/path/to/folder/on/server. Use gulp instead of npm run gulp if gulp is installed global.

This is a good practice to pass credentials trough args at a program start.