Created
July 1, 2019 08:19
-
-
Save cnicodeme/a400a752e08906d98fcc6504e5713a59 to your computer and use it in GitHub Desktop.
Deploy a ZIP archive to Netlify
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# -*- coding:utf-8 -*- | |
import zipfile, urllib2, argparse, os | |
try: | |
from BytesIO import BytesIO | |
except ImportError: | |
from io import BytesIO | |
def deploy(archive): | |
data = open(archive, 'rb') | |
req = urllib2.Request('https://api.netlify.com/api/v1/sites/{SITE_ID}.netlify.com/deploys', data.read()) | |
req.get_method = lambda: 'POST' | |
req.add_header('Content-Length', len(data.getvalue())) | |
req.add_header('Content-Type', 'application/zip') | |
req.add_header('Authorization', 'Bearer {YOUR_API_KEY}') | |
res = urllib2.urlopen(req) | |
if res.getcode() == 200: | |
print("Deploy successful") | |
exit(0) | |
print("An error occured!") | |
print(res.read()) | |
exit(1) | |
def is_valid_file(parser, arg): | |
if not os.path.exists(arg): | |
parser.error("The file %s does not exist!" % arg) | |
else: | |
return arg | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser() | |
parser.add_argument("-f", dest="file", required=True, help="Path to the ZIP archive", metavar="FILE", type=lambda x: is_valid_file(parser, x)) | |
args = parser.parse_args() | |
deploy(args.file) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment