How do I define global variables for multi-stage YAML pipelines?
When working with multi-stage YAML pipelines, defining global variables helps maintain consistency across different stages and facilitates easier management and updates. These global variables can be set at the pipeline level and then accessed by any stage within the pipeline.
Below is an example of how to define global variables for a multi-stage pipeline:
- First, we’ll specify the global variables at the top of the pipeline definition.
- Then, we will access these variables in the stages.
Example Multi-Stage YAML Pipeline with Global Variables
This example defines global variables that can be referenced across different stages in the pipeline. The variables include common values such as environment details, project names, and version numbers.
import * as pulumi from "@pulumi/pulumi";
import * as _null from "@pulumi/null";
import * as command from "@pulumi/command";
const config = new pulumi.Config();
const pipelineVariables = config.getObject<Record<string, string>>("pipelineVariables") || {
environment: "production",
project_name: "MyProject",
version: "1.0.0",
};
const pipeline = new _null.Resource("pipeline", {});
const pipelineProvisioner0 = new command.local.Command("pipelineProvisioner0", {create: ` echo "Starting pipeline for ${pipelineVariables.project_name} in ${pipelineVariables.environment} environment with version ${pipelineVariables.version}"
`}, {
dependsOn: [pipeline],
});
const deployStage = new _null.Resource("deploy_stage", {}, {
dependsOn: [pipeline],
});
const deployStageProvisioner0 = new command.local.Command("deployStageProvisioner0", {create: ` echo "Deploying ${pipelineVariables.project_name} (${pipelineVariables.version}) to ${pipelineVariables.environment}"
`}, {
dependsOn: [deployStage],
});
const testStage = new _null.Resource("test_stage", {}, {
dependsOn: [deployStage],
});
const testStageProvisioner0 = new command.local.Command("testStageProvisioner0", {create: ` echo "Testing ${pipelineVariables.project_name} in ${pipelineVariables.environment} environment"
`}, {
dependsOn: [testStage],
});
export const globalVariables = pipelineVariables;
Concluding Summary
In this example, we set global variables at the beginning of our pipeline to hold common values such as the project name, environment, and version number. These variables are then accessed in different stages like deploy_stage
and test_stage
, creating a consistent and maintainable structure for the pipeline. This approach simplifies managing the pipeline as updates can be made in a single place rather than multiple stages.
Deploy this code
Want to deploy this code? Sign up for a free Pulumi account to deploy in a few clicks.
Sign upNew to Pulumi?
Want to deploy this code? Sign up with Pulumi to deploy in a few clicks.
Sign upThank you for your feedback!
If you have a question about how to use Pulumi, reach out in Community Slack.
Open an issue on GitHub to report a problem or suggest an improvement.