Skip to content

Instantly share code, notes, and snippets.

@azu
Last active January 6, 2023 06:28
Show Gist options
  • Save azu/533a1cfb83e1040d77b6963c9004856d to your computer and use it in GitHub Desktop.
Save azu/533a1cfb83e1040d77b6963c9004856d to your computer and use it in GitHub Desktop.
Get Circle CI Project envars! Require Node.js 16 + `npm install node-fetch`
import fetch from "node-fetch"; // Node.js 18 just remove line
const CIRCLECI_TOKEN = process.env.CIRCLECI_TOKEN
const ORG_NAME = process.env.ORG_NAME
const fetchProjectList = (orgName = ORG_NAME) => {
return fetch(`https://circleci.com/api/v2/insights/gh/${orgName}/summary`, {
headers: { "Circle-Token": `${CIRCLECI_TOKEN}` }
}).then(res => res.json()).then(json => json.all_projects);
};
const fetchProjectEnv = (orgName, project) => {
return fetch(`https://circleci.com/api/v2/project/gh/${orgName}/${project}/envvar`, {
headers: { "Circle-Token": `${CIRCLECI_TOKEN}` }
}).then(res => res.json());
}
const projectNames = await fetchProjectList();
const projectMap = new Map()
for(const projectName of projectNames){
const env = await fetchProjectEnv(ORG_NAME, projectName);
projectMap.set(projectName, env.items);
}
console.log(JSON.stringify(Array.from((projectMap.entries()))))
@azu
Copy link
Author

azu commented Jan 5, 2023

$ CIRCLECI_TOKEN=xxx ORG_NAME=your-org node all-env-circleci.mjs 

@azu
Copy link
Author

azu commented Jan 5, 2023

Note: insights API does not return all projects.
If you need to get all projecs, please copy these from Admin Console.

@azu
Copy link
Author

azu commented Jan 6, 2023

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment