Creating custom actions in Buddy
Tags: cicd, buddy, ci, devops
Published: 2023-01-02 15:57:48

While Buddy was always my favorite CI/CD app, it was missing one feature - shareable custom actions. Luckily for us - it has changed lately and in this article, I will show you how to create them.

I always saw the lack of Custom Actions as the biggest downside of Buddy. There was an action **Custom Build**, but it didn't provide us with any UI options, and it was pretty tedious to share and keep updated.

Luckily, Custom Actions change all of this. From now, you can:

*   create a UI to make it easier to use
    
*   share it with others
    
*   import someone's actions
    

How to store Custom Actions
---------------------------

There are three ways to do so:

*   create a separate repository for all your custom actions
    
*   create a separate repository for each action
    
*   add actions to the project's repositories
    

Each is useful in some cases. A separate repository for all your actions is excellent when you work in a company and want to have all your actions in one place.

If you are considering an **Open Source approach**, having separate actions in separate repositories seems best.

But, if you want to create related actions with a project, then adding them to that project's repo is the best solution.

For more information, check out the [official Buddy documentation](https://buddy.works/docs/pipelines/custom-actions).

How to create a Custom Action
-----------------------------

Again, you have a choice. You can either create everything manually based on the documentation or use this [small tool](https://www.npmjs.com/package/buddy-custom-action) I made.

If you decide to use **Buddy Custom Action CLI,** you only need to run `npx buddy-custom-action@latest` and answer a few questions. When you are done, you should have a ready-to-use boilerplate.

![](https://maciekpalmowski.dev/img/containers/assets/arts/custom-actions/control-v-%289%29.png/2f4bbebc93cfcf9b6764bcf974f9701e.webp)

Custom actions

Example #1 - No deployment on Friday action
-------------------------------------------

Some time ago, I created this simple action that [prevents deploying on Fridays](https://maciekpalmowski.dev/no-deploy-friday-action/). Let's try to convert it into a Custom Action.

First, I used the `npx buddy-custom-action@latest`. I only provided it with a name and a short description, and I said yes to creating an execute commands section.
```
    name: "no_deploys_on_friday"
    execute_commands:
      - 
    docker_image_name: "ubuntu"
    docker_image_tag: "latest"
```
This is my boilerplate. Of course, right now, it won't do anything. It's time to add the missing command itself:
```
    name: "no_deploys_on_friday"
    execute_commands:
      - if [[ $(date +%u) == 5 ]]; then echo 'Do not deploy on Firday.' && exit 1; fi;
    docker_image_name: "ubuntu"
    docker_image_tag: "latest"
```
Is this all? No - we should also add an icon and name it `action.png`

Now we are done. It was simple. Now it's time to create something more complicated.

You can download the code from **[this repository](https://github.com/palmiak/custom-actions-article)**.

Example #2 - API Parser
-----------------------

This time, we'll create something more complicated. This action will get data from a given API then it will be parsed with `jq` and in the end, we will pass the result to another action.

Again, let's get started with running the `npx buddy-custom-action@latest`. We'll also need inputs and outputs this time.

Let's start with the inputs. Inputs are the fields that you can populate with some data. In our case, we will need the following:

*   the API URL
    
*   the `jq` command to filter out the required value
    
```
    inputs:
      url:
        name: "URL you want to test"
        required: true
      jq_command:
        name: "JQ parse command"
        required: true
```
I will use the Star Wars API and filter out the character's name.

![](https://maciekpalmowski.dev/img/containers/assets/arts/custom-actions/control-v-%287%29.png/b6ee2bc528deca117e806ec8cf272799.webp)

Now it's time to add the `exectute_commands`:
```
    execute_commands:
      - apt-get update && apt-get install -y curl jq
      - api=`curl -s $url`
      - `jq $jq_command <<< $api`
```
It works - we will get **Luke Skywalker** as a result. But, while it works, this action is a bit useless. It would be much better to pass the result to other actions. Let's do it, then.

This is when outputs come in handy:
```
    output:
      variables:
        result:
          info: "Result of JQ on API"
```
This will result in a tiny change in how your action looks:

![](https://maciekpalmowski.dev/img/containers/assets/arts/custom-actions/control-v-%288%29.png/3490337ff6ba8feb8ecc3ba601797e7a.webp)

Now, we have to change the `execute_commends` too:
```
    execute_commands:
      - apt-get update && apt-get install -y curl jq
      - api=`curl -s $url`
      - result=`jq $jq_command <<< $api`
      - export result
```
From this point, we can use the `result` variable in other actions.

The whole action should look like this:
```
    name: "API_Parser"
    inputs:
      url:
        name: "URL you want to test"
        required: true
      jq_command:
        name: "JQ parse command"
        required: true
    output:
      variables:
        result:
          example: "Variable with the results of the JQ on API"
    execute_commands:
      - apt-get update && apt-get install -y curl jq
      - api=`curl -s $url`
      - result=`jq $jq_command <<< $api`
      - export result
    docker_image_name: "ubuntu"
    docker_image_tag: "latest"
```
You can download the code from **[this repository](https://github.com/palmiak/custom-actions-article)**.

What else can you do?
---------------------

The sky is the limit here. I see **Custom Actions** as one of the most groundbreaking features in Buddy, and it gives everyone the potential to create and share their own actions. We don't have to wait until developers have time to implement something - now, we can do it independently.

I can't wait to see what actions you will create.