Skip to content
v2.274.0

Variable Class ​

Pipeline-Level variable.

Example ​

ts
declare const sourceAction: codepipeline_actions.S3SourceAction;
declare const sourceOutput: codepipeline.Artifact;
declare const deployBucket: s3.Bucket;

// Pipeline-level variable
const variable = new codepipeline.Variable({
  variableName: 'bucket-var',
  description: 'description',
  defaultValue: 'sample',
});

new codepipeline.Pipeline(this, 'Pipeline', {
  pipelineType: codepipeline.PipelineType.V2,
  variables: [variable],
  stages: [
    {
      stageName: 'Source',
      actions: [sourceAction],
    },
    {
      stageName: 'Deploy',
      actions: [
        new codepipeline_actions.S3DeployAction({
          actionName: 'DeployAction',
          // can reference the variables
          objectKey: `${variable.reference()}.txt`,
          input: sourceOutput,
          bucket: deployBucket,
        }),
      ],
    },
  ],
});
python
# source_action: codepipeline_actions.S3SourceAction
# source_output: codepipeline.Artifact
# deploy_bucket: s3.Bucket


# Pipeline-level variable
variable = codepipeline.Variable(
    variable_name="bucket-var",
    description="description",
    default_value="sample"
)

codepipeline.Pipeline(self, "Pipeline",
    pipeline_type=codepipeline.PipelineType.V2,
    variables=[variable],
    stages=[codepipeline.StageProps(
        stage_name="Source",
        actions=[source_action]
    ), codepipeline.StageProps(
        stage_name="Deploy",
        actions=[
            codepipeline_actions.S3DeployAction(
                action_name="DeployAction",
                # can reference the variables
                object_key=f"{variable.reference()}.txt",
                input=source_output,
                bucket=deploy_bucket
            )
        ]
    )
    ]
)
java
S3SourceAction sourceAction;
Artifact sourceOutput;
Bucket deployBucket;


// Pipeline-level variable
Variable variable = Variable.Builder.create()
        .variableName("bucket-var")
        .description("description")
        .defaultValue("sample")
        .build();

Pipeline.Builder.create(this, "Pipeline")
        .pipelineType(PipelineType.V2)
        .variables(List.of(variable))
        .stages(List.of(StageProps.builder()
                .stageName("Source")
                .actions(List.of(sourceAction))
                .build(), StageProps.builder()
                .stageName("Deploy")
                .actions(List.of(
                    S3DeployAction.Builder.create()
                            .actionName("DeployAction")
                            // can reference the variables
                            .objectKey(String.format("%s.txt", variable.reference()))
                            .input(sourceOutput)
                            .bucket(deployBucket)
                            .build()))
                .build()))
        .build();
csharp
// Install: dotnet add package Amazon.CDK.Lib
S3SourceAction sourceAction;
Artifact sourceOutput;
Bucket deployBucket;


// Pipeline-level variable
var variable = new Variable(new VariableProps {
    VariableName = "bucket-var",
    Description = "description",
    DefaultValue = "sample"
});

new Pipeline(this, "Pipeline", new PipelineProps {
    PipelineType = PipelineType.V2,
    Variables = new [] { variable },
    Stages = new [] { new StageProps {
        StageName = "Source",
        Actions = new [] { sourceAction }
    }, new StageProps {
        StageName = "Deploy",
        Actions = new [] {
            new S3DeployAction(new S3DeployActionProps {
                ActionName = "DeployAction",
                // can reference the variables
                ObjectKey = $"{variable.reference()}.txt",
                Input = sourceOutput,
                Bucket = deployBucket
            }) }
    } }
});
go
// Install: go get github.com/aws/aws-cdk-go/awscdk/v2
var sourceAction S3SourceAction
var sourceOutput Artifact
var deployBucket Bucket


// Pipeline-level variable
variable := codepipeline.NewVariable(&VariableProps{
	VariableName: jsii.String("bucket-var"),
	Description: jsii.String("description"),
	DefaultValue: jsii.String("sample"),
})

codepipeline.NewPipeline(this, jsii.String("Pipeline"), &PipelineProps{
	PipelineType: codepipeline.PipelineType_V2,
	Variables: []Variable{
		variable,
	},
	Stages: []StageProps{
		&StageProps{
			StageName: jsii.String("Source"),
			Actions: []IAction{
				sourceAction,
			},
		},
		&StageProps{
			StageName: jsii.String("Deploy"),
			Actions: []IAction{
				codepipeline_actions.NewS3DeployAction(&S3DeployActionProps{
					ActionName: jsii.String("DeployAction"),
					// can reference the variables
					ObjectKey: fmt.Sprintf("%v.txt", variable.Reference()),
					Input: sourceOutput,
					Bucket: deployBucket,
				}),
			},
		},
	},
})

Initializer ​

ts
new Variable(props: VariableProps)
python
Variable(props: VariableProps)
java
new Variable(VariableProps props)
csharp
new Variable(VariableProps props)
go
NewVariable(Props VariableProps)

Properties ​

variableNamestringvariable_namestrvariableNameStringVariableNamestringVariableName*stringrequired#
The name of a pipeline-level variable.

Methods ​

reference(): stringdef reference(self, ) -> strpublic String reference()public string Reference()func Reference() *string#
Reference the variable name at Pipeline actions.
returns

Returns: The variable name in a format that can be referenced at Pipeline actions