GitLab webhooks
How CodeHerder registers a GitLab project webhook for a repo, how to set one up by hand, and how to rotate, remove and troubleshoot it.
A GitLab webhook makes GitLab push merge request, comment, pipeline, job, push, tag and release events to CodeHerder. CodeHerder then does not need to poll GitLab for them. Each repo has its own hook and its own secret.
What you need
CodeHerder registers a hook through the GitLab API. The GitLab connection token must be able to do that. It needs the api scope and the Maintainer role on the project. A group access token or a project access token works.
The same connection token lets the server mint project tokens for your agents. The device holds no GitLab mint token. For each sandbox, the server creates a short-lived project access token with this connection, and revokes it when the sandbox ends. A connection token without the api scope and the Maintainer role cannot create project tokens. The device then uses its own key or token pool for GitLab. On GitLab.com, project access tokens need the Premium or Ultimate plan. On a Free namespace, GitLab refuses the create call. The server then treats the repository as not covered, and the device uses its own key or pool. See Git credentials on a device.
The same connection token also opens the GitLab package registry to a stage. That needs the api scope and the Maintainer role on the connection’s group. A Maintainer can create project deploy tokens, which the server uses. Only an Owner can create a group deploy token, so the server tries that first and then falls back. See Package downloads.
The connection’s Sandbox git tokens setting controls this. project_access_token is the default, and a connection without the setting uses it. An owner can set service_account. The server then mints a short-lived token of a project service account for each sandbox, with a Reporter account for read and social work and a Developer account for write work. This works on every GitLab tier, including Free. An owner can set group_service_account when the connection token belongs to a group service account with the Maintainer role or higher. The server then mints a short-lived token of that same account for each sandbox, limited to one project. Pushes and pipelines then run as that account. An owner can set off. The server then mints no project token for that connection, and each device uses its own GitLab credential, such as its token pool. Any other value is a failed mint. To turn minting off, open Admin → Integrations, then GitLab, and click Edit connection. Set Sandbox git tokens to off, and save. The server still revokes the tokens it minted before.
A token with only the read_api scope can still read merge state. It cannot create a hook. In that case CodeHerder shows manual setup steps. See Set up a hook by hand.
The api scope is optional. It gives the token write access to GitLab. Use it only if you want one-step setup. See Integrations to connect GitLab.
Automatic setup
- Connect GitLab: open Admin → Integrations, then GitLab. Use a token with the
apiscope. - Add a GitLab repo to your workspace. See Connecting repositories.
- CodeHerder creates the hook in the background. Adding the repo does not wait for it.
CodeHerder also creates the hook when you restore an archived repo. It moves the hook when you change the repo’s git URL.
Open the repo page to see the Webhook section. It shows the status and the last error. An admin can click Rotate webhook secret or Remove GitLab webhook there. When the repo has no hook yet, the section shows Register webhook. When a hook exists but is not registered, the button reads Register again, or Try automatic setup in manual mode. You can do the same from the CLI:
ch repo hook show <repo>
ch repo hook register <repo>
ch repo hook rotate <repo>
ch repo hook remove <repo>
Registering is safe to repeat. CodeHerder deletes any hook that already points at its receive URL, so GitLab never holds two copies.
Registration by default
CodeHerder registers hooks without a human. These events start it:
- You create a GitLab connection, turn it on, give it a new token, or change its host. CodeHerder registers the hook of every active repo on that host in the workspace and its sub-workspaces. It registers at most four at a time.
- You add a GitLab repo and the workspace has no usable token. CodeHerder stores a failed hook with the cause
no_token. The repo page shows the fix. - A repo exists on the connection’s host and has no hook. CodeHerder registers it within a few minutes.
An admin can also open Admin → Integrations, then GitLab. The page lists each repo on the host with its hook status, the fix and the retry time. Click Register all to register every repo now. From the CLI, run ch repo hook list and ch repo hook register-all.
Healing
CodeHerder reads each registered hook from GitLab every six hours. The CH_GITLAB_HOOK_CHECK_INTERVAL setting changes this period. It also reads the hook at once when the repo’s event delivery shows a gap.
- If GitLab deleted the hook, CodeHerder registers it again.
- If GitLab disabled the hook, CodeHerder sends one update with the same URL and secret. This turns the hook on. The hook state notes the time.
- If a hook failed, CodeHerder tries again at the retry time. After a GitLab rate limit, it waits until GitLab’s reset time. With no reset time, it waits 1 minute. After any other failure, it waits at least 5 minutes, then longer after each failure, up to 6 hours.
An alert rule on the metric gitlab_hook_unhealthy_repos can tell you when a hook stays failed. Set the threshold to 0 and the window to 3600 seconds. The rule then fires when a hook has been failed for over one hour.
Set up a hook by hand
When the token cannot create hooks, the hook shows as failed. Click Register again (or Register webhook when the repo has no hook yet). CodeHerder then shows the receive URL, a secret and a list of steps. The secret appears once. Copy it now. CodeHerder cannot show it again. If you lose it, click Rotate webhook secret for a new one. A later Try automatic setup keeps the current secret, so a hook you set up by hand keeps working. It shows the steps again, but not the secret.
- In GitLab, open the project. Go to Settings, then Webhooks.
- Choose Add new webhook. Paste the receive URL into the URL field.
- Paste the secret into the Secret token field.
- Turn on these triggers: Push events, Tag push events, Comments, Merge request events, Job events, Pipeline events, Releases events.
- Keep Enable SSL verification on. Save the webhook.
Rotate the secret
Click Rotate webhook secret or run ch repo hook rotate. CodeHerder makes a new secret and replaces the old one on both sides. In manual mode, CodeHerder shows the new secret once. Paste it into the GitLab webhook.
A delivery that is in flight during the change can fail verification. GitLab sends later events with the new secret.
Remove the hook
Click Remove GitLab webhook or run ch repo hook remove. CodeHerder deletes the GitLab hook, the stored secret and the hook state. Later deliveries to the receive URL get a 401 answer.
If GitLab refuses the delete, CodeHerder still removes its own side. The answer names the hook id. Delete that hook by hand in Settings → Webhooks.
Archiving a repo removes its hook. Deleting a workspace does not. The receive URL then answers 401, and GitLab disables the hook after repeated failures. Delete the hook by hand if you want it gone sooner.
Self-managed GitLab
Set the Host field of the GitLab connection to your GitLab hostname. Use the bare name, for example gitlab.example.com. The flow is the same as for gitlab.com.
Your GitLab instance must reach the server’s public base URL (CH_BASE_URL). If CodeHerder runs on a private network, an instance admin must turn on Allow requests to the local network from webhooks and integrations. The setting is under Admin → Settings → Network → Outbound requests.
CodeHerder also refuses to call a private GitLab host unless the server allows internal hosts. In that case it falls back to manual setup.
Request changes on a merge request
CodeHerder records a GitLab “request changes” review as repo.merge_request_changes_requested. GitLab 17 or later sends it in the merge request hook, as the reviewer’s state requested_changes.
- Open the merge request in GitLab as the reviewer.
- Select Your review, or Start review.
- Select Request changes.
- Select Submit review.
Use the web UI. A Developer-role token could not set this state through the REST or GraphQL API. A review summary note arrives as a separate comment event.
An approval adds the approver to the reviewer list. CodeHerder does not read that as a review request.
Troubleshooting
The Webhook section shows one of these last errors.
| Last error | What to do |
|---|---|
| No GitLab token that can create webhooks is connected for this host. | Connect GitLab for this host. Use a token with the api scope. Or set up the hook by hand. |
The GitLab token needs the Maintainer role on the project and the api scope. |
Give the token the api scope and the Maintainer role. Then click Register again. |
| GitLab has no project, or the token cannot see it. Check the repo URL. | Check the repo URL. Check that the token can open the project. |
| GitLab rate limit. Retrying at a time. | Wait for the time shown. CodeHerder tries again then. |
| GitLab returned a status for the webhook request. Retrying at a time. | Check the GitLab status. CodeHerder tries again at the time shown. You can also click Register again. |
| GitLab disabled the webhook after delivery failures. | If the text says CodeHerder re-enabled it, no action is needed. Otherwise open Settings → Webhooks in GitLab and enable the hook. |
| GitLab no longer has the webhook. CodeHerder registers it again. | No action is needed. If the hook stays failed, click Register again. |
| CodeHerder may not call this GitLab host. | The host is private or not allowed. Set up the hook by hand. |
| GitLab did not answer the webhook request. Check that the host is up. | Check that GitLab is up. Then click Register again. |
| The secret is out of sync. Rotate it again. | GitLab holds a new secret and CodeHerder holds the old one. Click Rotate webhook secret. |
| The server has no public base URL (CH_BASE_URL), so GitLab has no address to post to. | Ask the server operator to set CH_BASE_URL. |
The error text shows retry times as clock times in UTC. The Retry at row shows the same time in your time zone. ch repo hook show prints the same cause, fix and retry time (in UTC).
If GitLab shows failed deliveries with a 401 status, the secrets do not match. Click Rotate webhook secret.
Check the repo’s integration health
The repo page has an Integration health section. ch repo health <repo> shows the same data. It reads Healthy when deliveries arrive and nothing is missing. It reads Gap when GitLab shows merge request activity that no delivery announced. It reads Failing when deliveries arrive and fail. It reads Setup failed when the hook failed to register or waits for manual setup. It reads No push when the repo has no webhook. CodeHerder then polls the repo. It reads Not connected when no GitLab connection covers the host. For a gap, open Settings → Webhooks in GitLab, edit the CodeHerder webhook, open Recent events, and resend the missed delivery. The section links to the hook when CodeHerder knows its id. About once an hour, CodeHerder also reads each repo from GitLab, whatever its health. This repairs events that GitLab did not send, so task state stays correct.
Last updated