Skip to content

Commit

Permalink
Add upload to Netlify feature
Browse files Browse the repository at this point in the history
Add functionality to allow user to upload generate files to Netlify

Closes thanethomson#82
  • Loading branch information
kx-chen committed Dec 5, 2018
1 parent 42ee1c5 commit 52e0da5
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 2 deletions.
10 changes: 10 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ language: python
apt:
packages:
- libgnutls-dev

stages:
- test
- name: deploy
if: type != pull_request

jobs:
include:
- python: 2.7
Expand All @@ -17,6 +23,10 @@ jobs:
- <<: *xenial-mixin
python: 3.7

- stage: deploy
python: 3.6
script: "python -m statik.cmdline --upload=netlify --netlify-site-id=$NETLIFY_SITE_ID -p examples/blog"

install:
- "pip install -r requirements.txt"
script:
Expand Down
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ repository. Watch that space!

## Remote upload

**Statik** can publish your generated site for you through SFTP or through Netlify.

### SFTP
To publish your website on a server via SFTP, you can use the remote upload command
line option. The connection parameters must be added to your project's `config.yml`.

Expand All @@ -118,6 +121,32 @@ remote:
password: 'SSH password'
```

### Netlify
To publish your website via Netlify, you will need 2 things:
your Netlify access token and your Netlify site id.

First, specify your Netlify access token as an environment variable:

Linux:

```bash
> export NETLIFY_TOKEN=<netlify_token>
```

Windows

```bash
> set NETLIFY_TOKEN=<netlify_token>
```

Then, run **Statik** by passing in `--upload=netlify` and your Netlify site id
`--netlify-site-id=<netlify_site_id>`.

```bash
statik --upload=netlify --netlify-site-id=<netlify_site_id>
```
**Statik** will upload the static site that it outputs.

## License
**The MIT License (MIT)**

Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ SQLAlchemy==1.2.12
Unidecode==1.0.22
watchdog==0.9.0
paramiko==2.4.2
git+https://github.com/kx-chen/netlify_deployer.git
25 changes: 23 additions & 2 deletions statik/cmdline.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from statik.watcher import watch
from statik.project import StatikProject
from statik.errors import StatikError, StatikErrorContext
from statik.upload import upload_sftp
from statik.upload import upload_sftp, upload_netlify

import logging
logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -113,8 +113,15 @@ def main():
group_remote.add_argument(
'-u', '--upload',
action='store',
help="Upload project to remote location (supported: SFTP)"
help="Upload project to remote location (supported: SFTP, netlify)",
)

group_remote.add_argument(
'--netlify-site-id',
action='store',
help="Netlify site id to upload to. (--upload=netlify must be specified too)",
)

group_remote.add_argument(
'-c', '--clear-remote',
action='store_true',
Expand Down Expand Up @@ -217,10 +224,24 @@ def main():
output_path,
rm_remote=args.clear_remote
)
elif args.netlify_site_id and args.upload == 'netlify':
if args.clear_remote:
logger.warning("--clear-remote is not supported when uploading to Netlify")

upload_netlify(
output_path,
args.netlify_site_id
)
else:
if args.clear_remote:
logger.warning("Ignoring --clear-remote switch because --upload is not specified")

if args.netlify_site_id and args.upload != 'netlify' or args.netlify_site_id:
logger.warning("Ignoring --netlify-site-id: --upload=netlify not specified")

if args.upload:
logger.warning("Upload format '{}' not supported".format(args.upload))

except StatikError as e:
sys.exit(e.exit_code)

Expand Down
6 changes: 6 additions & 0 deletions statik/upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import os

import paramiko
from netlify_uploader import deploy

from statik.config import StatikConfig

Expand All @@ -13,6 +14,7 @@

__all__ = [
'upload_sftp',
'upload_netlify',
]


Expand Down Expand Up @@ -73,3 +75,7 @@ def upload_sftp(input_path, output_path, rm_remote=False):

sftp.close()
logger.info('Connection closed.')


def upload_netlify(output_path, netlify_site_id):
deploy(netlify_site_id, output_path)

0 comments on commit 52e0da5

Please sign in to comment.