Created
August 1, 2024 21:16
-
-
Save dleavitt/7950f5073bb0ebe2f3fa5049a2f44ab8 to your computer and use it in GitHub Desktop.
Construct for aws/aws-cdk#31000 reproduction
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as codebuild from "aws-cdk-lib/aws-codebuild"; | |
import * as codepipeline from "aws-cdk-lib/aws-codepipeline"; | |
import * as codepipeline_actions from "aws-cdk-lib/aws-codepipeline-actions"; | |
import * as ec2 from "aws-cdk-lib/aws-ec2"; | |
import { Construct } from "constructs"; | |
export interface PipelineReproProps { | |
owner: string; | |
repo: string; | |
connectionArn: string; | |
vpc: ec2.IVpc; | |
} | |
export class PipelineRepro extends Construct { | |
pipeline: codepipeline.Pipeline; | |
constructor( | |
scope: Construct, | |
id: string, | |
{ owner, repo, connectionArn, vpc }: PipelineReproProps, | |
) { | |
super(scope, id); | |
const sourceOutput = new codepipeline.Artifact(); | |
this.pipeline = new codepipeline.Pipeline(this, "Pipeline"); | |
// Source Stage | |
// | |
const pullSourceAction = | |
new codepipeline_actions.CodeStarConnectionsSourceAction({ | |
actionName: "PullSource", | |
connectionArn, | |
output: sourceOutput, | |
owner, | |
repo, | |
codeBuildCloneOutput: true, | |
}); | |
this.pipeline.addTrigger({ | |
providerType: codepipeline.ProviderType.CODE_STAR_SOURCE_CONNECTION, | |
gitConfiguration: { | |
sourceAction: pullSourceAction, | |
pullRequestFilter: [ | |
{ | |
events: [ | |
codepipeline.GitPullRequestEvent.OPEN, | |
codepipeline.GitPullRequestEvent.UPDATED, | |
], | |
branchesIncludes: ["*"], | |
}, | |
], | |
}, | |
}); | |
this.pipeline.addStage({ | |
stageName: "Source", | |
actions: [pullSourceAction], | |
}); | |
// Build Stage | |
// | |
const project = new codebuild.PipelineProject(this, "Project", { | |
vpc, | |
buildSpec: codebuild.BuildSpec.fromObject({ | |
version: 0.2, | |
env: { "exported-variables": ["GIT_BRANCH_NAME"] }, | |
phases: { | |
build: { commands: ["env"] }, | |
}, | |
}), | |
}); | |
// - will succeed when pipeline first created and when a release is manually | |
// created | |
// - will fail when a release is created through a PR | |
const branchNameAction = new codepipeline_actions.CodeBuildAction({ | |
actionName: "UsesBranchName", | |
input: sourceOutput, | |
project, | |
environmentVariables: { | |
GIT_BRANCH_NAME: { value: pullSourceAction.variables.branchName }, | |
}, | |
}); | |
// - will succeed when a release is created through a PR | |
// - will fail when pipeline first created and when a release is manually | |
// created | |
const destBranchNameAction = new codepipeline_actions.CodeBuildAction({ | |
actionName: "UsesDestinationBranchName", | |
input: sourceOutput, | |
project, | |
environmentVariables: { | |
GIT_BRANCH_NAME: { | |
// workaround for accessing variable | |
value: pullSourceAction["variableExpression"]( | |
"DestinationBranchName", | |
), | |
}, | |
}, | |
}); | |
this.pipeline.addStage({ | |
stageName: "Build", | |
actions: [branchNameAction, destBranchNameAction], | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment