Functions
Functions are bash functions that can be called inside the remainder of devspace.yaml
. A common use case is to define reusable functionality that is called from within the scripts you specify within the pipelines
section.
There are two types of functions:
- Custom Functions defined in
devspace.yaml
(often added viaimports
) - Built-In Functions such as
build_images
orcreate_deployments
Custom Functions
DevSpace provides support for custom functions. To use them, you need to:
- Declare your custom functions within the
functions
section ofdevspace.yaml
- Call custom functions from any scripted field in
devspace.yaml
such as inpipelines
or in any value defined using an inline script such as$(command)
1. Declare Functions
Declaring a function means adding a key with the function name to the functions
section in devspace.yaml
as shown in the example below:
version: v2beta1
functions:
hello_world: |-
echo "Hello World"
get_tag: |-
echo $(git rev-parse --short HEAD)
2. Call Functions
To call a custom function
version: v2beta1
pipelines:
deploy: |-
build_images --tag $(get_git_commit)
create_deployments --all
dev: |-
hello_world
run_default_pipeline dev
Please note that custom functions can only be called from pipelines
and other functions
as these run in a pipeline
context.
Built-In Functions
DevSpace provides a set of built-in functions. There are two types of functions:
Pipeline-Only Functions
Pipeline-only functions can only be used inside the scripts within the pipelines
section of devspace.yaml
. They can be overridden through the functions section to avoid changing a default pipeline.
Example:
functions:
create_deployments: |-
echo "create_deployments was called with $@"
__create_deployments "$@"
build_images
[image-1][image-2] ... pipeline only
Builds all images passed as arguments in parallel
build_images
[image-1][image-2] ... pipeline onlyensure_pull_secrets
[image-1][image-2] ... pipeline only
Creates pull secrets for all images passed as arguments
ensure_pull_secrets
[image-1][image-2] ... pipeline onlyget_image
[image] string pipeline only
Returns the most recently built image and/or tag for a given image name
get_image
[image] string pipeline onlycreate_deployments
[deployment-1][deployment-2] ... pipeline only
Creates all deployments passed as arguments in parallel
create_deployments
[deployment-1][deployment-2] ... pipeline onlypurge_deployments
[deployment-1][deployment-2] ... pipeline only
Purges all deployments passed as arguments
purge_deployments
[deployment-1][deployment-2] ... pipeline onlystart_dev
[dev-1][dev-2] ... pipeline only
Starts all dev modes passed as arguments
start_dev
[dev-1][dev-2] ... pipeline onlystop_dev
[dev-1][dev-2] ... pipeline only
Stops all dev modes passed as arguments
stop_dev
[dev-1][dev-2] ... pipeline onlyrun_pipelines
[pipeline-1][pipeline-2] ... pipeline only
Runs all pipelines passed as arguments
run_pipelines
[pipeline-1][pipeline-2] ... pipeline onlyrun_default_pipeline
[pipeline] pipeline only
Runs the default pipeline passed as arguments
run_default_pipeline
[pipeline] pipeline onlyrun_dependency_pipelines
[dependency-1][dependency-2] ... pipeline only
Runs a pipeline of each dependency passed as arguments
run_dependency_pipelines
[dependency-1][dependency-2] ... pipeline onlyis_dependency
int pipeline only
Returns exit code 0 if the pipeline currently being executed is run because the project is a dependency of another project
is_dependency
int pipeline onlyselect_pod
string pipeline only
Returns the name of a Kubernetes pod
select_pod
string pipeline onlywait_pod
[command] pipeline only
Waits for a pod to become running
wait_pod
[command] pipeline onlyexec_container
[command] pipeline only
Executes the command provided as argument inside a container
exec_container
[command] pipeline onlyget_config_value
[json.path] string pipeline only
Returns the value of the config loaded from devspace.yaml
get_config_value
[json.path] string pipeline onlyGlobal Functions
Global functions can be used anywhere in devspace.yaml
, either in config fields that expect a bash script such as within functions
or using $(command)
vars in any other config field.
is_empty
[value] int pipeline only
Returns exit code 0 if the value of the argument is empty string
is_empty
[value] int pipeline onlyis_equal
[value-1][value-2] int pipeline only
Returns exit code 0 if the values of both arguments provided are equal
is_equal
[value-1][value-2] int pipeline onlyis_in
[value-1][value-2] int pipeline only
Returns exit code 0 if the value of the first argument can be found in the second argument (second argument being a blank-separated list of strings e.g "bananas apples peaches"
)
is_in
[value-1][value-2] int pipeline only"bananas apples peaches"
)is_os
[os] darwin linux windows aix android dragonfly freebsd hurd illumos ios js nacl netbsd openbsd plan9 solaris zos int pipeline only
Returns exit code 0 if the current operating system equals the value provided as argument
is_os
[os] darwin linux windows aix android dragonfly freebsd hurd illumos ios js nacl netbsd openbsd plan9 solaris zos int pipeline onlyis_true
[value] int pipeline only
Returns exit code 0 if the value of the argument is "true"
is_true
[value] int pipeline onlycat
[file-path] string pipeline only
Returns the content of a file
cat
[file-path] string pipeline onlyget_flag
[flag-name] string pipeline only
Returns the value of the flag that is provided as argument
get_flag
[flag-name] string pipeline onlyrun_watch
[command] pipeline only
Executes the command provided as argument and watches for conditions to restart the command
run_watch
[command] pipeline onlysleep
[seconds] pipeline only
Pauses the script execution for the number of seconds provided as argument
sleep
[seconds] pipeline onlyxargs
[command] pipeline only
Reads from stdin, splits input by blanks and executes the command provided as argument for each blank-separated input value (often used in pipes, e.g. echo 'image-1 image-2' | xargs build_images
)
xargs
[command] pipeline onlyecho 'image-1 image-2' | xargs build_images
)Config Reference
functions
required <function_name>:string
Functions are POSIX functions that can be used within pipelines. Those functions can also be imported by
imports.
functions
required <function_name>:string