Hooks
This section describes how to use hooks.
There are three hooks available:
| Hook | When it runs |
|---|---|
| pre-hook | Before the certificate is created or renewed (only if a change will actually happen) |
| deploy-hook | After the certificate is successfully created or renewed |
| post-hook | After the operation completes, regardless of outcome |
Don’t hesitate to share your hook scripts with the community.
Pre-Hook
This hook is executed, before the creation or the renewal, in cases where a certificate will be effectively created/renewed.
Execute the following command:
Define the following section in your .lego.yaml file:
Deploy-Hook
This hook is executed after the successful creation or the renewal of a certificate.
Execute the following command:
Define the following section in your .lego.yaml file:
Post-Hook
This hook is executed, after the creation or the renewal, in cases where a certificate is created/renewed, regardless of whether any errors occurred.
Execute the following command:
Define the following section in your .lego.yaml file:
Environment Variables
Some details are passed through environment variables to help you with your hooks:
| Environment Variable | Description |
|---|---|
LEGO_HOOK_ACCOUNT_ID |
The account ID. |
LEGO_HOOK_ACCOUNT_EMAIL |
The account email (if available). |
LEGO_HOOK_ACCOUNT_SERVER |
The server related to the account. |
LEGO_HOOK_CERT_NAME |
The name/ID of the certificate. |
LEGO_HOOK_CERT_NAME_SANITIZED |
The sanitized name/ID of the certificate. |
LEGO_HOOK_CERT_KEY_TYPE |
The type of the certificate key. |
LEGO_HOOK_CERT_DOMAINS |
The domains of the certificate. |
LEGO_HOOK_CERT_PATH |
The path of the certificate. |
LEGO_HOOK_CERT_KEY_PATH |
The path of the certificate key. |
LEGO_HOOK_ISSUER_CERT_PATH |
The path of the issuer certificate. |
LEGO_HOOK_CERT_PEM_PATH |
(only with --pem) The path to the PEM certificate. |
LEGO_HOOK_CERT_PFX_PATH |
(only with --pfx) The path to the PFX certificate. |
Use Case
A typical use case is distributing the certificate for other services and reload them if necessary. Since many programs understand PEM-formatted TLS certificates, it is relatively simple to use certificates for more than a web server.
This example script installs the new certificate for a mail server and reloads it. Beware: this is just a starting point, error checking is omitted for brevity.
Don’t hesitate to share your hook scripts with the community.