Skip to content

Latest commit

 

History

History
57 lines (43 loc) · 2.8 KB

making-modules.mdx

File metadata and controls

57 lines (43 loc) · 2.8 KB
title description
How to make a custom module
If you want to extend your image with custom functionality that requires configuration, you should create a module.

If you want to extend your image with custom functionality that requires configuration, you should create a module.

Creating a new module

  1. Open your repository and create a new directory inside the modules/ directory. The name of this directory should be the name of your module.
    • This name will be used as the type: when launching your module.
    • If this name has multiple parts, they should be separated with a dash -.
    • As a guideline for (to be) official modules, the name should be a short and sweet noun or modifier-noun combination, without a -installer or -setup suffix being recommended.
  2. Inside your newly created directory, create a file called <name-of-your-module>.sh and paste the following code into it:
    #!/usr/bin/env bash
    set -euo pipefail
    • This makes sure the correct shell is used and errors in your module cause the build to fail.
  3. You have now created an empty module and can proceed to coding it.

Coding your module

This guide only includes the bare minimum information to get you started on your coding adventure. Check out the module reference for more technical information about modules.

When being launched, your module receives its configuration as a JSON string as the first argument. It can be read from in bash using jq or yq like this:

#!/usr/bin/env bash
set -euo pipefail

# read a single variable from the configuration
VAR=$(echo "$1" | yq -I=0 ".var") # `-I=0` makes sure the output isn't indented
echo "$VAR"

# read an array from the configuration
get_yaml_array ARRAY '.array[]' "$1"
# loop over the array
for THING in "${ARRAY[@]}"; do
    echo "$THING"
done

In addition to the module's own configuration, each module has access to a set of environment variables. Check out the module reference's run environment section for a list.

Though bash is the recommended language to write modules, they can technically be written in any language as long as the .sh is used to launch them while passing the configuration.

Using your module

Your custom modules are available by default in custom images built in the same repository. There is no need to specify the source, you can just use them the same way you can use default modules. If the name of your custom module is the same as a default module's name, the custom module will always be used instead.

# recipe.yml
modules:
    - type: <name-of-your-module>
      option: true