Thursday, 28 June 2018

use ssh for drone to deploy app on instance.

We can instruct drone to deploy the application on an instance/VM with providing the access.
->
==
Add hostname, user and password as drone secrets.
drone secret add --image appleboy/drone-ssh --repository <git repo name> --name ssh_user --value <username>
drone secret add --image appleboy/drone-ssh --repository <git repo name> --name ssh_host --value 'IP/HostName'
drone secret add --image appleboy/drone-ssh --repository <git repo name> --name ssh_key --value @/<Key path>
==

.drone.yaml
---
workspace: /* help to clone the repo to drone container and same repo can scp to an instance/VM */
  base: /go
  path: </tmp>
pipeline:
  ssh-connection:
    image: appleboy/drone-ssh
    secrets: [ ssh_host, ssh_user, ssh_key ]
    commands:
      - echo $${SSH_HOST}
      - echo $${SSH_USER}
      - . ./ssh_setup.sh
      - . ./test_script.sh $${SSH_USER} $${SSH_HOST}
    when:
      event: [ push ]

ssh_setup.sh
#!/bin/bash
eval $(ssh-agent -s)
printf '%b%s' "$SSH_KEY" | ssh-add -
#echo $${SSH_KEY} > /tmp/SSH_KEY && chmod 600 /tmp/SSH_KEY

test_script.sh
#!/bin/bash
SSH_USER=$1
SSH_HOST=$2
#scp the repo from drone workspace
scp -r -q -o StrictHostKeyChecking=no </tmp>  ${SSH_USER}@${SSH_HOST}:/tmp/
#scp the script to /tmp
scp -q -o StrictHostKeyChecking=no <any thing> ${SSH_USER}@${SSH_HOST}:/tmp/
#Excute the script.sh from the server
ssh -q -tt -o StrictHostKeyChecking=no ${SSH_USER}@${SSH_HOST} "sudo /bin/sh <anything>"

No comments:

Prasanta Kumar Behera. Mob- 8147470101