[Solved] Set variables to plugin params?

Hello i have successfully setup and integrated one of my projects with drone… its amazing… however i found one issue that might be the key factor if i will be able to keep using this CI along with my other projects…

  deploy:
    image: appleboy/drone-scp
    host: example.com
    target: /home/ec2-user/public/
    secrets: [ ssh_username, ssh_key ]
    source:
      - dist/**

i have several other projects hosted in different servers, which uses different username + different ssh_keys (which i have no control over) what i am trying to do is have something like this

  deploy:
    image: appleboy/drone-scp
    host: $PROJECT_A_HOST
    target: $PROJECT_A_TARGET
    key: $PROJECT_A_KEY
    user: $PROJECT_A_USER
    source:
      - dist/**

but according with this resolution of this issue: https://stackoverflow.com/questions/46350675/access-environment-variables-of-drone-in-drone-plugins?noredirect=1&lq=1

GO can not evaluate bash expressions… is there a work around for this kind of situation?

@milewski not sure I totally get your question but :

I would advice to check the documentation on subsitution : http://docs.drone.io/substitution/

But ultimately I think you’re looking for secret mapping :

Later in the build you can use the followin trick to import variable from secrets into plugin :

  deploy:
    image: appleboy/drone-scp
    secrets:
      - source: project_a_host
        target: plugin_host
      - source: project_a_target
        target: plugin_target
      - source: project_a_key
        target: key
      - source: project_a_user
        target: user
    source:
      - dist/**


  deploy:
    image: appleboy/drone-scp
    secrets:
      - source: project_b_host
        target: plugin_host
      - source: project_b_target
        target: plugin_target
      - source: project_b_key
        target: key
      - source: project_b_user
        target: user
    source:
      - dist/**

PS: prefixing with plugin_ allows for providing plugin parameters from secrets.

1 Like

Exactly what i was looking for!!.. i couldn’t find this on the documentation… thanks i will give ie a try

Well i have just tried… and i get this error:

missing ssh config (Host, Username)

any idea what could be wrong?

 deploy:
    image: appleboy/drone-scp
    secrets:
      - source: project_b_host
        target: plugin_host
      - source: project_b_target
        target: plugin_target
      - source: project_b_key
        target: plugin_key
      - source: project_b_user
        target: plugin_user // have prefixed this with plugin like the other options
    source:
      - dist/**

do you have the secret project_b_host configured as a drone secret variable?

yes, but technically not with that names… here is the real names i am using within my tests

  deploy:
    image: appleboy/drone-scp
    secrets:
      - source: host
        target: plugin_host
      - source: deployment_path
        target: plugin_target
      - source: private_key
        target: plugin_key
      - source: user
        target: plugin_user
    source:
      - distribution/**

maybe the variable name HOST is reserved? or might being overridden by some system variables?

edit. Just changed the names to something else but i still get the same error

@milewski

You have to be careful about the target name.

When the target is a real secret, you shouldn’t prefix it with plugin_

So effectively :

 deploy:
    image: appleboy/drone-scp
    secrets:
      - source: project_b_host
        target: plugin_host
      - source: project_b_target
        target: plugin_target
      - source: project_b_key
        target: key
      - source: project_b_user
        target: user // SOULDN'T have prefixed this with plugin like the other options
    source:
      - dist/**

would work better

also if I’m correct. for the SCP plugin the secret name for user is username

according with the SCP plugin docs the only options that can be passed from secrets are:

Secret Reference

ssh_username
ssh_password
ssh_key
proxy_ssh_username
proxy_ssh_password
proxy_ssh_key

and the other options are listed as Parameter Reference

host
target
user
key

i have also tried without the plugin_ prefix only in the key and user but got the same error message: missing ssh config (Host, Username) which i believe is caused because the HOST is not available as a secret, so back to my original question, what i wanted to do is give the value to this HOST param dynamically.

that’s why i didn’t understand why prefixing host with plugin_ was allowed in your example but not user and key as all of them are listed as Parameter in the doc…

Cool thanks! this lead me to the solution.

Following config is working now:

    secrets:
      - source: project_b_host
        target: plugin_host
      - source: project_b_target
        target: plugin_target
      - source: project_b_key
        target: ssh_key
      - source: project_b_user
        target: ssh_username

but still don’t quite understand why host, user and key are listed as params but couldnt work with the plugin_ prefix, is this something to do with the way this docker image script was build? maybe didn’t take this in consideration? or this parsing and replacing happens on the drone side? and this is potentially a bug? i would like to assume that every “Parameter” could be set dynamically by this secret trick but for some reason i don’t yet understand it didn’t worked out…

The system is behaving as expected. Aside from passing secret environment variable names as uppercase, drone does not transform or alter the secret environment variable names in any way.

reference code:

environment[strings.ToUpper(requested.Target)] = secret.Value

Please note that secret names do not by default correspond to the yaml attribute names. To learn more about how yaml atrributes are passed to the plugin, you might find the following document helpful: http://docs.drone.io/creating-custom-plugins-bash/

You can reference the plugin source code to see which environment variables it reads:

		cli.StringFlag{
			Name:   "username, u",
			Usage:  "Server username",
			EnvVar: "PLUGIN_USERNAME,SCP_USERNAME,SSH_USERNAME",
		},
1 Like

Thanks! i got how it works now! and thanks for the source code! i can work out on building my own plugins now!