Last active
July 31, 2018 06:39
-
-
Save johnmarcou/7e648d93e64e6500b21ecd8aee668d5d to your computer and use it in GitHub Desktop.
Drone CI setup repo
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/env python3 | |
# .drone.yml is used to configure your pipeline at the repo level | |
# let's write a .drone.setup.yml to setup our repo, configure some settings, and push some secrets | |
# | |
# Configure your Drone repo with a git-tracked file, then apply your conf | |
# Use a bit of sops (https://github.com/mozilla/sops) to protect your secrets | |
# First, export your Drone env var (DRONE_SERVER, DRONE_TOKEN) | |
# Run: ./drone-setup.py | |
# | |
# Example of .drone.setup.yml file: | |
# | |
# owner: MYORG | |
# repo: MYREPO | |
# settings: | |
# allow_pr: false | |
# allow_push: true | |
# allow_deploys: false | |
# allow_tags: false | |
# trusted: true | |
# secrets: | |
# slack_webhook: https://hooks.slack.com/services/************************ | |
# | |
import os | |
import yaml | |
import click | |
from os.path import abspath, expanduser | |
import requests | |
import logging | |
logging.basicConfig(level=logging.INFO) | |
logger = logging.getLogger(__name__) | |
def drone(method, path, **kwargs): | |
return requests.request(method, f'{server}{path}', params=dict(access_token=token), **kwargs) | |
def load_config(config_file): | |
with open(expanduser(config_file), 'r') as stream: | |
try: | |
config = yaml.load(stream) | |
except yaml.YAMLError as exc: | |
print(exc) | |
return config | |
@click.command() | |
@click.argument('config_file', default=".drone.setup.yml") | |
def main(config_file): | |
global owner | |
global repo | |
global server | |
global token | |
config = load_config(config_file) | |
server = os.getenv("DRONE_SERVER") | |
token = os.getenv("DRONE_TOKEN") | |
owner = config["owner"] | |
repo = config["repo"] | |
secrets = config.get("secrets", {}) | |
drone("PATCH", f"/api/repos/{owner}/{repo}", json=config.get("settings")) | |
#Flush all secrets | |
for secret in drone("GET", f"/api/repos/{owner}/{repo}/secrets").json(): | |
logger.info(f"delete secret {secret['name']}") | |
drone("DELETE", f"/api/repos/{owner}/{repo}/secrets/{secret['name']}") | |
# Inject secrets | |
for name, value in secrets.items(): | |
logger.info(f"create secret {name}") | |
result = drone("POST", f"/api/repos/{owner}/{repo}/secrets", json=dict(name=name, value=value)) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment