Hooks
DevSpace allows you to define certain actions that should be executed during the pipeline. This makes it possible to customize the deployment and development process with DevSpace. The following actions can be executed with hooks:
- Execute a command on the local machine (in a golang shell or directly on the system)
- Execute a command in a container
- Upload a file or folder into a container
- Download a file or folder from a container
- Wait for a container to start or terminate
- Print the logs of container
Hooks can be defined in the hooks
section of devspace.yaml
:
This tells DevSpace to execute the command echo before image building
before any image will be built.
#
Lifecycle EventsYou are able to define hooks for the following lifecycle events:
before:deploy
,after:deploy
,before:deploy:[name]
,after:deploy:[name]
,error:deploy:[name]
,skip:deploy:[name]
: executed while DevSpace deploysdeployments
.[name]
can be replaced with the config name of a deployment or*
to match all.before:render
,after:render
,before:render:[name]
,after:render:[name]
,error:render:[name]
: executed while DevSpace rendersdeployments
duringdevspace render
.[name]
can be replaced with the config name of a deployment or*
to match all.before:purge
,after:purge
,before:purge:[name]
,after:purge:[name]
,error:purge:[name]
: executed while DevSpace purgesdeployments
duringdevspace purge
.[name]
can be replaced with the config name of a deployment or*
to match all.before:build
,after:build
,before:build:[name]
,after:build:[name]
,error:build:[name]
,skip:build:[name]
: executed while DevSpace buildsimages
.[name]
can be replaced with the config name of an image or*
to match all.start:sync:[name]
,stop:sync:[name]
,error:sync:[name]
,restart:sync:[name]
,before:initialSync:[name]
,after:initialSync:[name]
,error:initialSync:[name]
: executed while DevSpace syncs files withdev.sync
.[name]
can be replaced with the config name of a sync configuration or*
to match all.start:portForwarding:[name]
,restart:portForwarding:[name]
,error:portForwarding:[name]
,stop:portForwarding:[name]
: executed while DevSpace port forwards withdev.ports
.[name]
can be replaced with the config name of a port forwarding configuration or*
to match all.start:reversePortForwarding:[name]
,restart:reversePortForwarding:[name]
,error:reversePortForwarding:[name]
,stop:reversePortForwarding:[name]
: executed while DevSpace reverse port forwards withdev.ports
.[name]
can be replaced with the config name of a port forwarding configuration or*
to match all.before:createPullSecrets
,after:createPullSecrets
,error:createPullSecrets
: executed while DevSpace createspullSecrets
devCommand:before:execute
,devCommand:after:execute
,devCommand:error
,devCommand:interrupt
,devCommand:before:sync
,devCommand:after:sync
,devCommand:before:portForwarding
,devCommand:after:portForwarding
,devCommand:before:replacePods
,devCommand:after:replacePods
,devCommand:before:runPipeline
,devCommand:after:runPipeline
,devCommand:before:deployDependencies
,devCommand:after:deployDependencies
,devCommand:before:build
,devCommand:after:build
,devCommand:before:deploy
,devCommand:after:deploy
,devCommand:before:openTerminal
,devCommand:before:streamLogs
: executed at certain lifecycle events during thedevspace dev
commanddeployCommand:before:execute
,deployCommand:after:execute
,deployCommand:error
,deployCommand:interrupt
executed at different checkpoints whendevspace deploy
is executedpurgeCommand:before:execute
,purgeCommand:after:execute
,purgeCommand:error
,purgeCommand:interrupt
executed at different checkpoints whendevspace purge
is executedbuildCommand:before:execute
,buildCommand:after:execute
,buildCommand:error
,buildCommand:interrupt
executed at different checkpoints whendevspace build
is executedbefore:resolveDependencies
,after:resolveDependencies
,error:resolveDependencies
,before:buildDependencies
,after:buildDependencies
,error:buildDependencies
,before:deployDependencies
,after:deployDependencies
,error:deployDependencies
,before:purgeDependencies
,after:purgeDependencies
,error:purgeDependencies
executed at different checkpoints when dependencies are resolved, deployed, built or purged
Errors in Hooks
If any hook returns a non zero exit code, DevSpace will abort and print an error message.
For error:
events the actual error will be passed to the hook via the environment variable DEVSPACE_HOOK_ERROR
. For example:
#
Execute hooks in a containerDevSpace allows you to execute commands directly in a container instead of the local system. You can specify this in the container
section of the hook:
By default, DevSpace will wait for all pods / containers that were selected with the given selector to come up. As soon as all targets are running, DevSpace will execute the hook and wait for it to finish. You can define if DevSpace should wait and how long it should wait with wait
and timeout
:
#
Upload or Download files from a containerHooks can be used to upload or download files from a container. In the background, DevSpace will basically do a kubectl cp
to the specified container. Example:
#
Wait for a pod to be runningThis hook action can be useful if you want to ensure a certain pod is running before you continue with the pipeline. An example configuration could look like this:
#
Print the logs of a containerThis action can be useful to print logs of jobs or print the logs of init containers that would not be printed otherwise during devspace dev
. An example configuration could look like this:
job.yaml
:
devspace.yaml
:
#
Execute hooks in the backgroundBy default, DevSpace will wait for a hook to finish and then move on with the pipeline. However, in some cases it might be desired that a hook is executed in the background to speed up a process. You can specify a background hook with the background
option:
If you do not want to stream the output of the hook to the console, you can also mark the hook as silent, which will prevent any hook output:
#
Execute hooks only on certain operating systemsHooks can be executed only on certain operating systems:
#
Execute hooks onceHooks can be executed only once for each targeted container. This means as long as the container where the hook was executed stays running, the hook will not be executed for this container again until it restarts. This can be useful for running one-time development tasks without using init containers. In the following example, the command would only run once for the newest container running with the image nginx:1.21
.
If you run devspace dev
or devspace deploy
now multiple times and the container is not replaced or restarted, the hook is only executed once.
#
Hook Context InformationDevSpace passes certain environment variables to the hook execution:
- DEVSPACE_HOOK_KUBE_CONTEXT: the name of the kube context that was used
- DEVSPACE_HOOK_KUBE_NAMESPACE: the name of the kube namespace that was used
- DEVSPACE_HOOK_OS_ARGS: json encoded os.Args that were used to call devspace
- DEVSPACE_HOOK_ERROR: if an error has occured contains the error string (only for onError hooks)
- DEVSPACE_HOOK_EVENT: the event that has triggered the hook
Depending on the hook there will be other context variables set that are prefixed with DEVSPACE_HOOK_
.