Drone Plugin for Curl Command

Hello there,
I am completely newbie to Drone, so any help/directions would be useful.
I am trying a POC to create a drone plugin and my requirements for plugin is as follows

  1. The plugin should accept a json file
  2. The json file should be posted using curl command to another service api
  3. capture the response and read json value
  4. based on the result, pipeline succeed or fails
  5. The plugin should be reusable across drone pipelines

I was going through this page Example Bash Plugin and have some questions.

Should i first create a docker image first and push to artifactory and then reuse it? or can it be created on the fly and push to artifactory when running the pipeline?

Thanks

It is possible to build a docker image in one step and use that image in a later step, but that probably isn’t what you want to do, if you are working on a plugin.

Usually plugins are built and published to a public docker registry, then pulled as the image: field in pipeline steps.

Is there a reason you want to use a plugin, rather than the official curl docker image?

You could add your own scripting in commands: rather than introduce the extra complexity involved with writing a Drone plugin.

@jimsheldon ,

Thanks for your response. Agree that we need repository and our org have the one to pull and push.
First time seeing curl image and will go through that, but does it allow to pass file as input parameter and additional parameters if any?
The reason is I need is that file to be passed to a service api as form file parameter(may be additional parameters as well)

I may be having little knowledge on this but what is the best way to pass the values that are generated in previous steps?

Hello @guhan

The drone workspace is shared with every step in your pipeline. If you write a file in one step, it will be available in the next step.

From what you are describing, I don’t think a plugin is necessary. You should be able to do what you need with regular docker images. For example:

steps:
- name: write file
  image: alpine:3
  commands:
  - echo "writing to file" > my_file

- name: curl
  image: curlimages/curl
  commands:
  - curl -d @my_file https://example.com/api

Does that make sense?

@jimsheldon ,

Sure thanks, will give a try. I too tried to run command without any docker image. My sample code below and it works fine(status 200) but I need to capture the response of the curl command.

steps:

  • name: curl

    commands:

    • curl --request --location POST <my_api_url> --form “inputFile="<file_path>"”

but when I tried to capture the response with something like below, i get pipeline exited with error code 26( I could not find any additional details)
http_response = $(curl command as above)

am I missing anything? If not using a complex way to create docker image/plugin if I would be able to read the response / json for a particular value and do a If condition it would be great.

Else, if I can use those shell scripts inside a script file and execute it would be also nice

You can certainly create a shell script and execute it in your commands.

I am not a curl expert, can you run this curl command yourself on your computer, and does it work?

This might help you read the status code returned How to Get HTTP status code with curl post - Stack Overflow

@jimsheldon ,

Yes it does work locally as well as in the pipeline, but I need to receive the http response and check some value from the returned json, that is where I need to use with curl command for now.

Ah! So your step needs to receive traffic from a service running outside of your Drone infrastructure?

@jimsheldon ,
sorry for giving late reply. Yes the service is running outside of the drone infra.

As an update,
I was able to call shell script from drone pipeline and handle all the business logic inside it.
Next is to convert/move the logic inside a docker image and publish it if needed.

Thanks @jimsheldon

1 Like