Skip to content

Instantly share code, notes, and snippets.

@symroe
Created January 5, 2023 10:23
Show Gist options
  • Save symroe/715222f4bab5fd1c49e48b2127c89a19 to your computer and use it in GitHub Desktop.
Save symroe/715222f4bab5fd1c49e48b2127c89a19 to your computer and use it in GitHub Desktop.
Dumps all the context keys to a TSV on STDOUT
"""
Use:
Pass CIRCLECI_TOKEN and CIRCLECI_ORG_ID in as environment vars.
CIRCLECI_TOKEN is a personal access token from https://app.circleci.com/settings/user/tokens
CIRCLECI_ORG_ID is the Organisation ID from your CircleCI Organization Settings
Run the file and it'll output a TSV for you.
"""
import csv
import os
import sys
import requests
TOKEN = os.environ.get("CIRCLECI_TOKEN")
OWNER_ID = os.environ.get("CIRCLECI_ORG_ID")
HEADERS = {"Circle-Token": TOKEN}
def list_contexts():
next_token = ""
all_contexts = []
while next_token is not None:
req = requests.get(
f"https://circleci.com/api/v2/context?owner-id={OWNER_ID}&page-token={next_token}",
headers=HEADERS,
)
data = req.json()
all_contexts += data["items"]
next_token = data["next_page_token"]
return all_contexts
def get_single_context(context_id):
req = requests.get(
f"https://circleci.com/api/v2/context/{context_id}/environment-variable",
headers=HEADERS,
)
data = req.json()
return data["items"]
output = csv.writer(sys.stdout, delimiter="\t")
for context in list_contexts():
for env_var in get_single_context(context["id"]):
row = [
context["name"],
env_var["variable"]
]
output.writerow(row)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment