Skip to content

Instantly share code, notes, and snippets.

@varqasim
Last active November 22, 2020 18:12
Show Gist options
  • Save varqasim/82addb154997327ecece6261cff49142 to your computer and use it in GitHub Desktop.
Save varqasim/82addb154997327ecece6261cff49142 to your computer and use it in GitHub Desktop.
Create a Pulumi stack on PR creation
var path = require("path");
var { LocalWorkspace } = require("@pulumi/pulumi/x/automation");
// get node command arguments, if the user
// passed `destroy`.
var args = process.argv.slice(2);
var destroy = false;
if (args.length > 0 && args[0]) {
destroy = args[0] === "destroy";
}
async function run() {
var stackArgs = {
stackName: process.env.PR_STACK_NAME, // the PR stackname
workDir: path.join(__dirname, "../"), // the directory where our Pulumi.yaml exists
};
// select `development` stack
var devWorkspace = await LocalWorkspace.selectStack({
stackName: "development",
workDir: stackArgs.workDir,
});
// create a new stack for the PR
console.log(`Creating stack ${stackArgs.stackName}`);
var stack = await LocalWorkspace.createOrSelectStack(stackArgs);
if (destroy) {
// destroy the stack resources
console.info(`Destroying stack ${stackArgs.stackName}`);
await stack.destroy({ onOutput: console.info });
await LocalWorkspace.removeStack(stackArgs.stackName);
process.exit(0);
}
// set the config of the PR stack to the development stack config such as aws:region etc.
console.log(
`Setting ${stackArgs.stackName} stack config from development stack`,
);
await stack.setAllConfig(await devWorkspace.getAllConfig());
// change aws:region config value to be different than the development config
await stack.setConfig("aws:region", { value: "eu-central-1" });
console.log("Running pulumi up");
var upOutputs = await stack.up({ onOutput: console.info });
console.log(
`Up Summary: \n${JSON.stringify(
upOutputs.summary.resourceChanges,
null,
4,
)}`,
);
}
run().catch(function onRunError(error) {
console.log(error);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment