I am trying to run a Newman runner on CI/CD. For that i need some environment variables to be updated during runtime like firebase_tokens and all.
In the CI/CD:
- I am generating the required secrets.
- Then updating the postman environment variables.
- Then i run Newman runner.
The steps 1 and 3 are working as intended.
But the problem, is that the secrets that I am generating in the step 1 is not reflecting on the Postman UI as well as on the runner.
From what I can see in the Postman API docs,
The code to update the environment variables goes like this:
///////////////////////////
// POSTMAN
///////////////////////////
const postmanAPIKey = process.env.POSTMAN_API_KEY;
const environmentId = process.env.POSTMAN_ENVIRONMENT_ID;
if (!postmanAPIKey || !environmentId) {
console.error("Missing POSTMAN_API_KEY or POSTMAN_ENVIRONMENT_ID");
return;
}
const response = await fetch(
`https://api.getpostman.com/environments/${environmentId}`,
{
method: "GET",
headers: {
"Content-Type": "application/json",
"X-Api-Key": postmanAPIKey,
},
}
).catch((err) => {
console.error("Failed to get environment", err.message);
process.exit(1);
});
const data = await response.json();
const updatedEnvs = data.environment.values;
const updates = [{ key: "firebase_auth_token", value: token, enabled: true }];
for (const update of updates) {
const existingEnv = updatedEnvs.find((env) => env.key === update.key);
if (existingEnv) {
existingEnv.value = update.value;
existingEnv.enabled = true;
existingEnv.type = existingEnv.type || "default"; // ensure type stays valid
} else {
updatedEnvs.push({
key: update.key,
value: update.value,
enabled: true,
type: "default",
});
}
}
console.log("updatedEnvs:", JSON.stringify(updatedEnvs, null, 2));
await fetch(`https://api.getpostman.com/environments/${environmentId}`, {
method: "PUT",
headers: {
"Content-Type": "application/json",
"X-Api-Key": postmanAPIKey,
},
body: JSON.stringify({
environment: {
id: data.environment.id,
name: data.environment.name,
values: updatedEnvs,
},
}),
}).catch((err) => {
console.error("Failed to update environment", err.message);
process.exit(1);
});
console.log("Environment updated successfully");
I also read the following post. I could also confirm i have all the env’s are shared in my environment file.