compass/sass remote themeing via sftp/scp with alternate port

房东的猫 提交于 2019-12-07 10:57:32

I needed a solution for this too but did not find any satisfying answer anywhere. After reading the Ruby Net::ssh documentation and some source of Compass, this is my solution to upload CSS and sourcemap to a remote SSH server with non-standard port and forced public-key authorisation:

First make sure you have the required gems installed

sudo gem install net-ssh net-sftp

then add this to your config.rb

# Add this to the first lines of your config.rb
require 'net/ssh'
require 'net/sftp'

...
# Your normal compass config comes here
...

# At the end of your config.rb add the config for the upload code
remote_theme_dir_absolute = '/path/to/my/remote/stylesheets'

sftp_host = 'ssh_host' # Can be an IP
sftp_user = 'ssh_user' # SFTP Username

on_stylesheet_saved do |filename|

  # You can use the ssh-agent for authorisation. 
  # In this case you can remove the :passphrase from the config and set :use_agent => true.

  Net::SFTP.start(

    sftp_host, 
    sftp_user , 
    :port => 10022, 
    :keys_only => true, 
    :keys => ['/path/to/my/private/id_rsa'], 
    :auth_methods => ['publickey'], 
    :passphrase => 'my_secret_passphrase', 
    :use_agent => false, 
    :verbose => :warn

  ) do |sftp|

      puts sftp.upload! css_dir + '/app.css', remote_theme_dir_absolute + '/' + 'app.css'

  end

end

on_sourcemap_saved do |filename|

  # You can use the ssh-agent for authorisation. 
  # In this case you can remove the :passphrase from the config and set :use_agent true.

  Net::SFTP.start(
    sftp_host, 
    sftp_user , 
    :port => 10022, 
    :keys_only => true, 
    :keys => ['/path/to/my/private/id_rsa'], 
    :auth_methods => ['publickey'], 
    :passphrase => 'my_secret_passphrase', 
    :use_agent => false, 
    :verbose => :warn
  ) do |sftp|

    puts sftp.upload! css_dir + '/app.css.map', remote_theme_dir_absolute + '/' + 'app.css.map'

  end

end

It was quite some trial and error until this worked for me. Some points of failure were:

  • If no ssh-agent is available connection will fail until you set :ssh_agent => false explicitly
  • If you do not limit the available keys with :keys all available keys will be tried one after another. If you use the ssh-agent and have more than 3 keys installed chanches are high that the remote server will close the connection if you try too much keys that are not valid for the server you currently connect.
  • On any connection issue set verbosity level to :verbose => :debug to see what is going on. Remember to stop the compass watch and restart to ensure configuration changes apply.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!