Skip to content

Instantly share code, notes, and snippets.

@robert-stuttaford
Last active September 21, 2016 03:22
Show Gist options
  • Save robert-stuttaford/7fe8eff8617c49dac7cf45802bef0eb3 to your computer and use it in GitHub Desktop.
Save robert-stuttaford/7fe8eff8617c49dac7cf45802bef0eb3 to your computer and use it in GitHub Desktop.
Set a bunch of env vars on many CircleCI projects via API

Usage

Update the yml files with the vars you want to set and which projects you want them set for.

If you haven't already done so, visit https://circleci.com/account/api and generate yourself an API token.

Edit the python script to replace YOUR_USER_NAME_HERE on line 11 with your actual Github org name.

python update-circleci-env.py YOUR_CIRCLE_API_TOKEN_HERE
AWS_ACCESS_KEY_ID: ABCDEFGHIJKLMNOPQRSTUVWXYZ
AWS_SECRET_ACCESS_KEY: supercalifragilisticexpialidocious
# more: values
# go: here
projects:
- Your
- Projects
- Here
#!/usr/bin/env python
# NOTE
# this will overwrite env vars on those projects!
import requests
import argparse
import json
import yaml
address = "https://circleci.com/api/v1.1/project/github/YOUR_USER_NAME_HERE"
def set_env_var_for_project(token, project, name, value):
api_request = "{}/{}/envvar?circle-token={}".format(address, project, token)
var = {'name': name, 'value': value}
print("-> {}\n{}".format(api_request, json.dumps(var)))
r = requests.post(api_request, data=json.dumps(var))
r.raise_for_status()
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('circleci_token', help='CircleCI API token.')
args = parser.parse_args()
with open('circleci.env.yml', "r") as f:
vars = yaml.load(f)
with open('circleci.projects.yml', "r") as f:
projects = yaml.load(f).get('projects')
for project in projects:
for key in vars:
set_env_var_for_project(args.circleci_token, project, key, vars.get(key))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment