github.com/GoogleContainerTools/skaffold/v2@v2.13.2/docs-v2/content/en/docs/lifecycle-hooks.md (about) 1 --- 2 title: "Lifecycle Hooks" 3 linkTitle: "Lifecycle Hooks" 4 weight: 45 5 featureId: hooks 6 aliases: [/docs/how-tos/hooks, /docs/pipeline-stages/lifecycle-hooks] 7 --- 8 9 This page describes how to use the lifecycle hook framework to run code triggered by different events during the skaffold process lifecycle. 10 11 ## Overview 12 13 We identify three distinct phases in skaffold - `build`, `sync` and `deploy`. Skaffold can trigger a hook `before` or `after` executing each phase. There are two types of `hooks` that can be defined - `host` hooks and `container` hooks. 14 15 ## Host hooks 16 17 Host hooks are executed on the runner and can be defined for the following phases: 18 19 ### `before-build` and `after-build` 20 21 Build hooks are executed before and after each artifact is built. 22 If an artifact is not built, such as happens when the image was found in the Skaffold image cache, then the build hooks will not be executed. 23 To force the build hooks, run Skaffold with `--cache-artifacts=false` option. 24 25 Example: _skaffold.yaml_ snippet 26 ```yaml 27 build: 28 artifacts: 29 - image: hooks-example 30 hooks: 31 before: 32 - command: ["sh", "-c", "./hook.sh"] 33 os: [darwin, linux] 34 - command: ["cmd.exe", "/C", "hook.bat"] 35 os: [windows] 36 after: 37 - command: ["sh", "-c", "./hook.sh"] 38 os: [darwin, linux] 39 - command: ["cmd.exe", "/C", "hook.bat"] 40 os: [windows] 41 ``` 42 This config snippet defines that `hook.sh` (for `darwin` or `linux` OS) or `hook.bat` (for `windows` OS) will be executed `before` and `after` each build for artifact `hooks-example`. 43 44 ### `before-sync` and `after-sync` 45 46 Example: _skaffold.yaml_ snippet 47 ```yaml 48 build: 49 artifacts: 50 - image: hooks-example 51 sync: 52 auto: {} 53 hooks: 54 before: 55 - host: 56 command: ["sh", "-c", "./hook.sh"] 57 os: [darwin, linux] 58 - host: 59 command: ["cmd.exe", "/C", "hook.bat"] 60 os: [windows] 61 after: 62 - host: 63 command: ["sh", "-c", "./hook.sh"] 64 os: [darwin, linux] 65 - host: 66 command: ["cmd.exe", "/C", "hook.bat"] 67 os: [windows] 68 ``` 69 This config snippet defines that `hook.sh` (for `darwin` or `linux` OS) or `hook.bat` (for `windows` OS) will be executed `before` and `after` each file sync operation for artifact `hooks-example`. 70 71 ### `before-deploy` and `after-deploy` 72 73 Example: _skaffold.yaml_ snippet 74 ```yaml 75 deploy: 76 kubectl: 77 manifests: 78 - deployment.yaml 79 hooks: 80 before: 81 - host: 82 command: ["sh", "-c", "echo pre-deploy host hook running on $(hostname)!"] 83 os: [darwin, linux] 84 after: 85 - host: 86 command: ["sh", "-c", "echo post-deploy host hook running on $(hostname)!"] 87 ``` 88 This config snippet defines a simple `echo` command to run before and after each `kubectl` deploy. 89 90 ### Environment variables 91 92 The following environment variables will be available for the corresponding phase host hooks, that can be resolved in both inline commands or scripts. 93 94 Environment variable | Description | Availability 95 -- | -- | -- 96 $SKAFFOLD_IMAGE | The fully qualified image name. For example, “gcr.io/image1:tag” | Build, Sync 97 $SKAFFOLD_PUSH_IMAGE | Set to true if the image in $IMAGE is expected to exist in a remote registry. Set to false if the image is expected to exist locally. | Build 98 $SKAFFOLD_IMAGE_REPO | The image repo. For example, “gcr.io/image1” | Build 99 $SKAFFOLD_IMAGE_TAG | The image tag. For example, “tag” | Build 100 $SKAFFOLD_BUILD_CONTEXT | An absolute path to the directory this artifact is meant to be built from. Specified by artifact context in the skaffold.yaml. | Build 101 $SKAFFOLD_FILES_ADDED_OR_MODIFIED | Semi-colon delimited list of absolute path to all files synced or to be synced in current dev loop that have been added or modified | Sync 102 $SKAFFOLD_FILES_DELETED | Semi-colon delimited list of absolute path to all files synced or to be synced in current dev loop that have been deleted | Sync 103 $SKAFFOLD_RUN_ID | Run specific UUID label for deployed or to be deployed resources | Deploy 104 $SKAFFOLD_DEFAULT_REPO | The resolved default repository | All 105 $SKAFFOLD_RPC_PORT | TCP port to expose event API | All 106 $SKAFFOLD_HTTP_PORT | TCP port to expose event REST API over HTTP | All 107 $SKAFFOLD_KUBE_CONTEXT | The resolved Kubernetes context | Sync, Deploy 108 $SKAFFOLD_MULTI_LEVEL_REPO | The multi-level support of the repository | All 109 $SKAFFOLD_NAMESPACES | Comma separated list of Kubernetes namespaces | Sync, Deploy 110 $SKAFFOLD_WORK_DIR | The workspace root directory | All 111 Local environment variables | The current state of the local environment (e.g. $HOST, $PATH). Determined by the golang os.Environ function. | All 112 113 ## Container hooks 114 Container hooks are executed on a target container and can be defined on the following phases: 115 116 ### `before-sync` and `after-sync` 117 118 Example: _skaffold.yaml_ snippet 119 ```yaml 120 build: 121 artifacts: 122 - image: hooks-example 123 sync: 124 auto: {} 125 hooks: 126 before: 127 - container: 128 command: ["sh", "-c", "echo before sync hook"] 129 after: 130 - container: 131 command: ["sh", "-c", "echo after sync hook"] 132 ``` 133 This config snippet defines a command to run inside the container corresponding to the artifact `hooks-example` image, `before` and `after` each file sync operation. 134 135 ### `before-deploy` and `after-deploy` 136 137 Example: _skaffold.yaml_ snippet 138 ```yaml 139 deploy: 140 kubectl: 141 manifests: 142 - deployment.yaml 143 hooks: 144 before: 145 - container: 146 # this will only run when there's a matching container from a previous deploy iteration like in `skaffold dev` 147 command: ["sh", "-c", "echo pre-deploy container hook running on $(hostname)!"] 148 containerName: hooks-example* 149 podName: hooks-example-deployment* 150 after: 151 - container: 152 command: ["sh", "-c", "echo post-deploy container hook running on $(hostname)!"] 153 containerName: hooks-example* # use a glob pattern to prefix-match the container name and pod name for deployments, stateful-sets, etc. 154 podName: hooks-example-deployment* 155 ``` 156 This config snippet defines a simple `echo` command to run inside the containers that match `podName` and `containerName`, before and after each `kubectl` deploy. The `after` container commands are only run after the [deployment status checks]({{< relref "/docs/status-check" >}}) on the deployment are complete. Also, unlike the `sync` container hooks, skaffold cannot determine the target container from just the config definition, and needs the `podName` and `containerName`.