Skip to content

Commit

Permalink
Auto deploy by hook (#24)
Browse files Browse the repository at this point in the history
* Support triggering builds on Netlify automatically

* Update version
  • Loading branch information
tomdyson committed Nov 10, 2020
1 parent fac3dc0 commit 0a22cd6
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 12 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.

...

## v0.7

### Added

- Support triggering builds on Netlify automatically

## v0.6

### Added
Expand Down
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Deploy your Wagtail site on Netlify. Features include:

1. Add `wagtailnetlify` to your `INSTALLED_APPS`.
2. Run the migrations: `./manage.py migrate wagtailnetlify`.
3. Add `NETLIFY_PATH` to your settings.
3. Add `NETLIFY_PATH` or `NETLIFY_BUILD_HOOK` to your settings.

Check the [Settings](#settings) section below for more customisation options.

Expand All @@ -30,7 +30,11 @@ If `NETLIFY_AUTO_DEPLOY` is set to `True`, Wagtail will automatically deploy you

*or*

To deploy changes manually, use `./manage.py netlify`. To generate redirects without deploying, use the `-n` or `--no-deploy` flag: `./manage.py netlify --no-deploy`. To trigger a build on Netlify's servers, configure `settings.NETLIFY_BUILD_HOOK` and use the `-t` or `--trigger-build` flag: `./manage.py netlify --trigger-build`.
To deploy changes manually, use `./manage.py netlify`.

To generate redirects without deploying, use the `-n` or `--no-deploy` flag: `./manage.py netlify --no-deploy`.

To trigger a build on Netlify's servers, configure `settings.NETLIFY_BUILD_HOOK` and use the `-t` or `--trigger-build` flag: `./manage.py netlify --trigger-build`.

## Settings

Expand All @@ -56,9 +60,9 @@ Connect to your Netlify account to [generate a token](https://app.netlify.com/ac

### `NETLIFY_AUTO_DEPLOY`

**Default: `True`**
**Default: `False`**

Whether to automatically deploy your site to Netlify every time you publish a page. This make take between a few seconds and a few minutes, depending on the size of your site, and the number of pages which are affected by your change.
Whether to automatically deploy your site to Netlify every time you publish a page. This make take between a few seconds and a few minutes, depending on the size of your site, and the number of pages which are affected by your change. If you have configured `settings.NETLIFY_BUILD_HOOK`, publishing a page will trigger a build on Netlify's servers.

### `NETLIFY_DEPLOY_FUNCTION`

Expand Down
2 changes: 1 addition & 1 deletion wagtailnetlify/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.6"
__version__ = "0.7"
25 changes: 18 additions & 7 deletions wagtailnetlify/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,43 @@ class Deployment(models.Model):
netlify_id = models.CharField(max_length=30, null=True)
url = models.URLField(null=True)
deployment_url = models.URLField(null=True)
datetime_started = models.DateTimeField(auto_now_add=True, help_text='deployment started')
datetime_finished = models.DateTimeField('deployment completed', null=True)
datetime_started = models.DateTimeField(
auto_now_add=True, help_text="deployment started"
)
datetime_finished = models.DateTimeField("deployment completed", null=True)


def postpone(function):
"""
Cheap aysnc, see https://stackoverflow.com/a/28913218
"""

def decorator(*args, **kwargs):
t = Thread(target=function, args=args, kwargs=kwargs)
t.daemon = True
t.start()

return decorator


@postpone
def deploy(sender, **kwargs):
"""
Build static pages, then send incremental changes to netlify.
Trigger a build on Netlify, if NETLIFY_BUILD_HOOK is supplied, or
build static pages, then upload incremental changes to Netlify.
"""
call_command('build')
call_command('netlify')
netlify_build_hook = getattr(settings, "NETLIFY_BUILD_HOOK", None)
if netlify_build_hook:
call_command("netlify", "--trigger-build")
else:
call_command("build")
call_command("netlify")
connection.close()


if getattr(settings, 'NETLIFY_AUTO_DEPLOY', False) == True:
function_path = getattr(settings, 'NETLIFY_DEPLOY_FUNCTION', 'wagtailnetlify.models.deploy')
if getattr(settings, "NETLIFY_AUTO_DEPLOY", False) == True:
function_path = getattr(
settings, "NETLIFY_DEPLOY_FUNCTION", "wagtailnetlify.models.deploy"
)
function = import_string(function_path)
page_published.connect(function)

0 comments on commit 0a22cd6

Please sign in to comment.