Trigger jobs automatically

Anvil jobs can be automatically triggered whenever commits are made to the repository, or on other repository events. This is done using hooks, which are custom scripts that run when certain actions in the version control process occur. They can be used for many purposes, but here we will just discuss how they can be utilised with Anvil.

A 'post-commit hook' runs client-side whenever a commit is made. A 'post-receive hook' runs server-side after the server repository receives a push. Both of these hooks can be used to trigger job builds in Anvil.

Many repository-hosting websites, such as GitHub, offer an interface for setting up post-receive hooks - these are known as webhooks. We recommend using webhooks where possible.

Webhook setup

Both the Anvil job and the repository must be configured to allow a webhook to automatically trigger the job.

Anvil setup

For a hook to be able to trigger a job in Anvil, the job must have Poll SCM activated in the Build Triggers section of the job configuration. You do not need to set a polling schedule, the box can be left blank (though setting a schedule will not prevent hooks from working).

Enabling post-commit hooks in Anvil
Enabling post-commit hooks in Anvil job

Note: While the settings page references only post-commit hooks, any type of hook can be used to trigger a build.

Repository setup

Webhook setup can typically be found in the repository settings (the setup page for GitHub is shown below).

For a Git repository, the required URL for the webhook is https://anvil.softeng-support.ac.uk/jenkins/git/notifyCommit?url=<repo-url> . Make sure the webhook is configured to trigger on push events, or any other event you wish to trigger builds on.

Additional setup for GitHub

When using GitHub, you should activate both the Poll SCM and the GitHub hook trigger for GITScm polling options in the Build Triggers section of the Anvil job configuration. Hooks will not work if only GitHub hook trigger for GITScm polling is activated.

In your repository settings, select Webhooks, and click New Webhook. Paste the URL above (replacing <repo-url> with the URL of your GitHub repository) into the Payload URL box, and click Add webhook to activate the hook.

GitHub webhook setup
GitHub webhook setup

Manual setup

If you are self-hosting your repository and wish to manually set up a post-receive hook to trigger a Jenkins build, please contact us.