Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1284,7 +1284,7 @@
"ProjectName": {
"Ref": "MyProject2B52B17CC"
},
"EnvironmentVariables": "[{\"name\":\"CommitId\",\"type\":\"PLAINTEXT\",\"value\":\"#{Source_CodeStarConnectionsSourceAction2_NS.CommitId}\"}]"
"EnvironmentVariables": "[{\"name\":\"CommitId\",\"type\":\"PLAINTEXT\",\"value\":\"#{Source_CodeStarConnectionsSourceAction2_NS.CommitId}\"},{\"name\":\"SourceBranchName\",\"type\":\"PLAINTEXT\",\"value\":\"#{Source_CodeStarConnectionsSourceAction2_NS.SourceBranchName}\"},{\"name\":\"DestinationBranchName\",\"type\":\"PLAINTEXT\",\"value\":\"#{Source_CodeStarConnectionsSourceAction2_NS.DestinationBranchName}\"},{\"name\":\"PullRequestId\",\"type\":\"PLAINTEXT\",\"value\":\"#{Source_CodeStarConnectionsSourceAction2_NS.PullRequestId}\"},{\"name\":\"PullRequestTitle\",\"type\":\"PLAINTEXT\",\"value\":\"#{Source_CodeStarConnectionsSourceAction2_NS.PullRequestTitle}\"}]"
},
"InputArtifacts": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ new codepipeline.Pipeline(stack, 'Pipeline2', {
outputs: [new codepipeline.Artifact()],
environmentVariables: {
CommitId: { value: sourceAction2.variables.commitId },
SourceBranchName: { value: sourceAction2.variables.sourceBranchName },
DestinationBranchName: { value: sourceAction2.variables.destinationBranchName },
PullRequestId: { value: sourceAction2.variables.pullRequestId },
PullRequestTitle: { value: sourceAction2.variables.pullRequestTitle },
},
}),
],
Expand Down
37 changes: 37 additions & 0 deletions packages/aws-cdk-lib/aws-codepipeline-actions/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,43 @@ new codepipeline_actions.CodeBuildAction({
});
```

When a pipeline is triggered by a pull request event, additional PR-specific variables are available:

```ts
declare const project: codebuild.Project;

const sourceOutput = new codepipeline.Artifact();
const sourceAction = new codepipeline_actions.CodeStarConnectionsSourceAction({
actionName: 'BitBucket_Source',
owner: 'aws',
repo: 'aws-cdk',
output: sourceOutput,
connectionArn: 'arn:aws:codestar-connections:us-east-1:123456789012:connection/12345678-abcd-12ab-34cdef5678gh',
});

// later:

new codepipeline_actions.CodeBuildAction({
actionName: 'CodeBuild',
project,
input: sourceOutput,
environmentVariables: {
SOURCE_BRANCH: {
value: sourceAction.variables.sourceBranchName,
},
DESTINATION_BRANCH: {
value: sourceAction.variables.destinationBranchName,
},
PULL_REQUEST_ID: {
value: sourceAction.variables.pullRequestId,
},
PULL_REQUEST_TITLE: {
value: sourceAction.variables.pullRequestTitle,
},
},
});
```

### AWS S3 Source

To use an S3 Bucket as a source in CodePipeline:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ export interface CodeStarSourceVariables {
readonly commitMessage: string;
/** The connection ARN this source uses. */
readonly connectionArn: string;
/** The source branch name of the PR that triggered the pipeline execution. Only available for PR-triggered executions. */
readonly sourceBranchName: string;
/** The destination branch name (target) of the PR that triggered the pipeline execution. Only available for PR-triggered executions. */
readonly destinationBranchName: string;
/** The ID of the PR that triggered the pipeline execution. Only available for PR-triggered executions. */
readonly pullRequestId: string;
/** The title of the PR that triggered the pipeline execution. Only available for PR-triggered executions. */
readonly pullRequestTitle: string;
}

/**
Expand Down Expand Up @@ -124,6 +132,10 @@ export class CodeStarConnectionsSourceAction extends Action {
commitId: this.variableExpression('CommitId'),
commitMessage: this.variableExpression('CommitMessage'),
connectionArn: this.variableExpression('ConnectionArn'),
sourceBranchName: this.variableExpression('SourceBranchName'),
destinationBranchName: this.variableExpression('DestinationBranchName'),
pullRequestId: this.variableExpression('PullRequestId'),
pullRequestTitle: this.variableExpression('PullRequestTitle'),
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,62 @@ describe('CodeStar Connections source Action', () => {
});
});

test('exposes PR-specific variables', () => {
const stack = new Stack();
const sourceOutput = new codepipeline.Artifact();
const sourceAction = new cpactions.CodeStarConnectionsSourceAction({
actionName: 'BitBucket',
owner: 'aws',
repo: 'aws-cdk',
output: sourceOutput,
connectionArn: 'arn:aws:codestar-connections:us-east-1:123456789012:connection/12345678-abcd-12ab-34cdef5678gh',
});

new codepipeline.Pipeline(stack, 'Pipeline', {
stages: [
{
stageName: 'Source',
actions: [sourceAction],
},
{
stageName: 'Build',
actions: [
new cpactions.CodeBuildAction({
actionName: 'CodeBuild',
project: new codebuild.PipelineProject(stack, 'MyProject'),
input: sourceOutput,
environmentVariables: {
SourceBranchName: { value: sourceAction.variables.sourceBranchName },
DestinationBranchName: { value: sourceAction.variables.destinationBranchName },
PullRequestId: { value: sourceAction.variables.pullRequestId },
PullRequestTitle: { value: sourceAction.variables.pullRequestTitle },
},
}),
],
},
],
});

Template.fromStack(stack).hasResourceProperties('AWS::CodePipeline::Pipeline', {
'Stages': [
{
'Name': 'Source',
},
{
'Name': 'Build',
'Actions': [
{
'Name': 'CodeBuild',
'Configuration': {
'EnvironmentVariables': '[{"name":"SourceBranchName","type":"PLAINTEXT","value":"#{Source_BitBucket_NS.SourceBranchName}"},{"name":"DestinationBranchName","type":"PLAINTEXT","value":"#{Source_BitBucket_NS.DestinationBranchName}"},{"name":"PullRequestId","type":"PLAINTEXT","value":"#{Source_BitBucket_NS.PullRequestId}"},{"name":"PullRequestTitle","type":"PLAINTEXT","value":"#{Source_BitBucket_NS.PullRequestTitle}"}]',
},
},
],
},
],
});
});

test('fail if variable from unused action is referenced', () => {
const app = new App();
const stack = new Stack(app);
Expand Down
Loading