appleboy / drone-scp

Copy files and artifacts via SSH using a binary, docker or Drone CI.
MIT License
142 stars 30 forks source link

how to use with private ssh key? #74

Closed scippio closed 6 years ago

scippio commented 7 years ago

I'm trying this setup:

  deploy:
    image: appleboy/drone-scp
    host: example.com
    username: root
    port: 922
    key: $scp_key
    secrets:
      - source: DEPLOY_KEY
        target: scp_key
    source:
      - public
    target:
      - /srv/public
    when:
      branch: prod
      status: success

but still habe some errors like: ERROR: Error response from daemon: invalid environment variable:.. etc...

or drone-scp error: error copy file to dest: example.com, error message: ssh: handshake failed: ssh: unable to authenticate, attempted methods [none], no supported methods remain

EDIT: I paste my private key via drone web admin: DEPLOY_KEY + and paste output of cat /.ssh/mykey | tr -d '\n'

EDIT2: I trying this too:

deploy:
    image: appleboy/drone-scp
    host: example.com
    username: root
    port: 922
    key: >
      -----BEGIN RSA PRIVATE KEY-----
      MI...
      -----END RSA PRIVATE KEY-----
    source:
      - public
    target:
      - /srv/public
    when:
      branch: prod
      status: success

but get: error copy file to dest: example.com, error message: ssh: handshake failed: ssh: unable to authenticate, attempted methods [none], no supported methods remain

scippio commented 7 years ago

Ok... it's functional now... I must add secret key via cli drone secret add --repository my/repo --name DEPLOY_KEY --value @/home/....rsa.key

and in my .drone.yml: ` secrets:

please update documentation :+1:

lucischen commented 6 years ago

Hi @scippio

I add

drone secret add --repository my/repo --name deploy_key --value @/home/machine01/.ssh/id_rsa

and in my .drone.yml:

secrets: 
    - source: deploy_key 
      target: scp_key

but it throw

Error response from daemon: invalid environment variable: =/home/machine01/.ssh/id_rsa

I'm confused. what's scp_key here?

appleboy commented 6 years ago
secrets: 
  - source: deploy_key 
    target: ssh_key
fenixwu commented 6 years ago

@lucischen I only set deploy_key like this:

drone secret add --repository MY_REPOSITORY --name deploy_key --value @/PATH/TO/.ssh/id_rsa

And .drone.yml like this:

secrets: 
  - source: deploy_key 
    target: scp_key

It works!!

But I don't understand how it works by setting target to "scp_key" or "ssh_key".

appleboy commented 6 years ago

Drone have the same result if you use scp_key or ssh_key

see: https://github.com/appleboy/drone-scp/blob/master/main.go#L65

appleboy commented 6 years ago

@scippio any updates on this?

mighty-raj commented 6 years ago

@appleboy how to pass envrionment variables for "appleboy/drone-scp" image when using in drone.yml file ?

appleboy commented 6 years ago

@rajWT see http://docs.drone.io/manage-secrets/

mebibou commented 6 years ago

Is it possible to have a full example of this? I can not make this work, I've tried 100 combinations of things for the past hours but I always have the message

error message: ssh: handshake failed: ssh: unable to authenticate, attempted methods [none], no supported methods remain

my config is

publish:
    image: appleboy/drone-scp
    host: <host>
    username: deploy
    secrets:
      - source: deploy_key
        target: scp_key
    target: /var/www/
    source: site
fnsne commented 6 years ago

Hello, I also got the same error "ERROR: Error response from daemon: invalid environment variable". I have tried to add secrets by drone Web UI and drone secret add (like below) drone -t MY_TOKEN -s DRONE_SERVER secret add MY_REPO --name dev_key --value SSH_KEY But got the same error.

here is my .drone.yml.

scp_dev:
         image: appleboy/drone-scp
         host:
             - MY_HOST_IP
         username: ubuntu
         secrets:
             - source: dev_key
             - target: scp_key
         target: /home/ubuntu/scp_test
         source:
             - django_database
fnsne commented 6 years ago

I found what is wrong! I add a '-' before the word target.

secrets:
    - source: dev_key
       target: scp_key

work well.

ozywuli commented 5 years ago

Is it possible to have a full example of this? I can not make this work, I've tried 100 combinations of things for the past hours but I always have the message

error message: ssh: handshake failed: ssh: unable to authenticate, attempted methods [none], no supported methods remain

my config is

publish:
    image: appleboy/drone-scp
    host: <host>
    username: deploy
    secrets:
      - source: deploy_key
        target: scp_key
    target: /var/www/
    source: site

I'm in the same boat as you with no luck. Here's my config:

  scp:
    image: appleboy/drone-scp
    host: HOST_IP
    username: test
    source:
      - dist
    target:
      - /home/test/dist
    secrets:
      - source: DEPLOY_KEY
        target: ssh_key
appleboy commented 5 years ago

@ozywuli what is your drone version?

ozywuli commented 5 years ago

@ozywuli what is your drone version?

Hey @appleboy thanks for the prompt reply, but I actually just figured it out and it was my fault all along. I had added the secret key to Drone from my remote server when I was suppose to add the secret key from my local machine. @mebibou maybe this is your issue as well if you're still experiencing it.

r6m commented 5 years ago

@appleboy, I'm having the same issue. image: drone/drone:1.0.0-rc.5

drone-scp error: error copy file to dest: mydomain.com, error message: ssh: handshake failed: ssh: unable to authenticate, attempted methods [none], no supported methods remain

and this is the .drone.yml:

- name: scp
    image: appleboy/drone-scp
    settings:
      username: root
      host: mydomain.com
      port: 2222
      key:
        from_secret: ssh_key
      rm: false
      target: /root/docker_apps/${DRONE_REPO_NAME}/
      source:
        - ./bin/${DRONE_REPO_NAME}
        - ./Dockerfile
        - ./config.yml
        - ./docker-compose.yml
    when:
      event: [ push ]
      branch: [ master ]

I have a ssh_key named secret with my plain key as value. I'm able to connect to the VPS using the same key.

can you help?

yujiangshui commented 5 years ago

@appleboy I have the same issue with @rezam90 here.

As we all know, use ssh key to log in a server don't have to specify a username, but if you don't set a username the drone-scp will show an error message:

missing ssh config (Host, Username)

After I search the code and compare with drone-ssh's code, I find this line L193 may be the cause of this issue.

Compare to the drone-ssh L140.

I don't familiar with Go, but it seems like in drone-scp must set up a username otherwise that will not works. So I try to set a username root to detour this logic, but get the same error:

drone-scp error: error copy file to dest: ********, error message: ssh: handshake failed: ssh: unable to authenticate, attempted methods [none], no supported methods remain

After that, I made a test, I comment out the drone-scp configuration and the drone-ssh works, here is my configuration:

  - name: deploy-services
    image: appleboy/drone-scp
    settings:
      username: root
      host:
        from_secret: db-host
      ssh_key:
        from_secret: id-rsa
      target:
        - /root/apps/
      source:
        - ./services
    when:
      ref:
        include:
          - refs/tags/services/*
  - name: start-services
    image: appleboy/drone-ssh
    settings:
      host:
        from_secret: db-host
      ssh_key:
        from_secret: id-rsa
      script:
        - docker-compose stop
        - docker-compose up -d
    when:
      ref:
        include:
          - refs/tags/services/*

This test confirm that drone-scp has issues about the ssh_key login functions, and it's better keep the same logics with drone-ssh because the usage are very similar.

I want to make a pr, but I don't familiar with Go. So, hope you have some time to have a look. Thanks for your awesome work!

appleboy commented 5 years ago

@yujiangshui I will take it tomorrow or next week.

r6m commented 5 years ago

@appleboy, could you fix the issue?

appleboy commented 5 years ago

@yujiangshui @rezam90 I fixed the error missing ssh config (Host, Username) in #91

Sfinx commented 5 years ago

In short:

1) Poor plugin docs 2) Poor plugin examples for .drone.yml 3) Really stupid tar usage => man tar -C 3) No debug (!?)

It is nearly unusable, you have allways guess. This is what works with latest drone

- name: Web Dev Deploy
  image: appleboy/drone-scp
  settings:
    host: some_site
    port: 22
    user: webdeploy
    key:
      from_secret: webdeploy_ssh_key
    target: /opt/poc/web/root
    rm: true
    source: dist/spa