Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(aws-codebuild): allow github sourceversion branch #5890

Merged
merged 9 commits into from
Feb 4, 2020
34 changes: 28 additions & 6 deletions packages/@aws-cdk/aws-codebuild/lib/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,13 @@ export interface ProjectProps extends CommonProjectProps {
*/
readonly secondarySources?: ISource[];

/**
* The secondary source versions for the Project. *
* @default - No secondary source versions (they default to latest).
* @see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-codebuild-project.html#cfn-codebuild-project-sourceversion
*/
readonly secondarySourceVersions?: CfnProject.ProjectSourceVersionProperty[];
knorms101 marked this conversation as resolved.
Show resolved Hide resolved

/**
* The secondary artifacts for the Project.
* Can also be added after the Project has been created by using the {@link Project#addSecondaryArtifact} method.
Expand Down Expand Up @@ -626,7 +633,7 @@ export class Project extends ProjectBase {
* @returns an array of {@link CfnProject.EnvironmentVariableProperty} instances
*/
public static serializeEnvVariables(environmentVariables: { [name: string]: BuildEnvironmentVariable }):
CfnProject.EnvironmentVariableProperty[] {
CfnProject.EnvironmentVariableProperty[] {
knorms101 marked this conversation as resolved.
Show resolved Hide resolved
return Object.keys(environmentVariables).map(name => ({
name,
type: environmentVariables[name].type || BuildEnvironmentVariableType.PLAINTEXT,
Expand Down Expand Up @@ -654,6 +661,7 @@ export class Project extends ProjectBase {
private readonly source: ISource;
private readonly buildImage: IBuildImage;
private readonly _secondarySources: CfnProject.SourceProperty[];
private readonly _secondarySourceVersions: CfnProject.ProjectSourceVersionProperty[];
skinny85 marked this conversation as resolved.
Show resolved Hide resolved
private readonly _secondaryArtifacts: CfnProject.ArtifactsProperty[];
private _encryptionKey?: kms.IKey;

Expand Down Expand Up @@ -698,6 +706,7 @@ export class Project extends ProjectBase {
}

this._secondarySources = [];
this._secondarySourceVersions = [];
for (const secondarySource of props.secondarySources || []) {
this.addSecondarySource(secondarySource);
}
Expand Down Expand Up @@ -725,8 +734,10 @@ export class Project extends ProjectBase {
name: this.physicalName,
timeoutInMinutes: props.timeout && props.timeout.toMinutes(),
secondarySources: Lazy.anyValue({ produce: () => this.renderSecondarySources() }),
secondarySourceVersions: Lazy.anyValue({ produce: () => this.renderSecondarySourceVersions() }),
secondaryArtifacts: Lazy.anyValue({ produce: () => this.renderSecondaryArtifacts() }),
triggers: sourceConfig.buildTriggers,
sourceVersion: sourceConfig.sourceVersion,
vpcConfig: this.configureVpc(props),
});

Expand Down Expand Up @@ -756,7 +767,12 @@ export class Project extends ProjectBase {
if (!secondarySource.identifier) {
throw new Error('The identifier attribute is mandatory for secondary sources');
}
this._secondarySources.push(secondarySource.bind(this, this).sourceProperty);
const secondarySourceConfig = secondarySource.bind(this, this);
this._secondarySources.push(secondarySourceConfig.sourceProperty);
this._secondarySourceVersions.push({
sourceIdentifier: secondarySource.identifier,
sourceVersion: secondarySourceConfig.sourceVersion,
});
}

/**
Expand Down Expand Up @@ -788,7 +804,7 @@ export class Project extends ProjectBase {
const keyStack = Stack.of(options.artifactBucket.encryptionKey);
const projectStack = Stack.of(this);
if (!(options.artifactBucket.encryptionKey instanceof kms.Key &&
(keyStack.account !== projectStack.account || keyStack.region !== projectStack.region))) {
(keyStack.account !== projectStack.account || keyStack.region !== projectStack.region))) {
knorms101 marked this conversation as resolved.
Show resolved Hide resolved
this.encryptionKey = options.artifactBucket.encryptionKey;
}
}
Expand Down Expand Up @@ -893,6 +909,12 @@ export class Project extends ProjectBase {
: this._secondarySources;
}

private renderSecondarySourceVersions(): CfnProject.ProjectSourceVersionProperty[] | undefined {
return this._secondarySourceVersions.length === 0
? undefined
knorms101 marked this conversation as resolved.
Show resolved Hide resolved
: this._secondarySourceVersions;
}

private renderSecondaryArtifacts(): CfnProject.ArtifactsProperty[] | undefined {
return this._secondaryArtifacts.length === 0
? undefined
Expand Down Expand Up @@ -983,8 +1005,8 @@ export class Project extends ProjectBase {
const artifactsType = artifacts.type;

if ((sourceType === CODEPIPELINE_SOURCE_ARTIFACTS_TYPE ||
artifactsType === CODEPIPELINE_SOURCE_ARTIFACTS_TYPE) &&
(sourceType !== artifactsType)) {
artifactsType === CODEPIPELINE_SOURCE_ARTIFACTS_TYPE) &&
knorms101 marked this conversation as resolved.
Show resolved Hide resolved
(sourceType !== artifactsType)) {
throw new Error('Both source and artifacts must be set to CodePipeline');
}
}
Expand Down Expand Up @@ -1124,7 +1146,7 @@ class ArmBuildImage implements IBuildImage {
public validate(buildEnvironment: BuildEnvironment): string[] {
const ret = [];
if (buildEnvironment.computeType &&
buildEnvironment.computeType !== ComputeType.LARGE) {
buildEnvironment.computeType !== ComputeType.LARGE) {
knorms101 marked this conversation as resolved.
Show resolved Hide resolved
ret.push(`ARM images only support ComputeType '${ComputeType.LARGE}' - ` +
`'${buildEnvironment.computeType}' was given`);
}
Expand Down
63 changes: 63 additions & 0 deletions packages/@aws-cdk/aws-codebuild/lib/source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ export interface SourceConfig {
readonly sourceProperty: CfnProject.SourceProperty;

readonly buildTriggers?: CfnProject.ProjectTriggersProperty;

/**
* `AWS::CodeBuild::Project.SourceVersion`
* @see http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-codebuild-project.html#cfn-codebuild-project-sourceversion
* @default the latest version
*/
readonly sourceVersion?: string;
}

/**
Expand Down Expand Up @@ -471,6 +478,14 @@ abstract class ThirdPartyGitSource extends GitSource {
*/
export interface CodeCommitSourceProps extends GitSourceProps {
readonly repository: codecommit.IRepository;

/**
* The commit ID, branch, or Git tag to use.
*
* @example 'mybranch'
* @default the default branch's HEAD commit ID is used
*/
readonly sourceVersion?: string;
knorms101 marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand All @@ -479,10 +494,12 @@ export interface CodeCommitSourceProps extends GitSourceProps {
class CodeCommitSource extends GitSource {
public readonly type = CODECOMMIT_SOURCE_TYPE;
private readonly repo: codecommit.IRepository;
private readonly sourceVersion?: string;

constructor(props: CodeCommitSourceProps) {
super(props);
this.repo = props.repository;
this.sourceVersion = props.sourceVersion;
}

public bind(_scope: Construct, project: IProject): SourceConfig {
Expand All @@ -498,6 +515,7 @@ class CodeCommitSource extends GitSource {
...superConfig.sourceProperty,
location: this.repo.repositoryCloneUrlHttp,
},
sourceVersion: this.sourceVersion,
};
}
}
Expand All @@ -508,6 +526,13 @@ class CodeCommitSource extends GitSource {
export interface S3SourceProps extends SourceProps {
readonly bucket: s3.IBucket;
readonly path: string;

/**
* The version ID of the object that represents the build input ZIP file to use.
*
* @default latest
*/
readonly sourceVersion?: string;
knorms101 marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand All @@ -517,11 +542,13 @@ class S3Source extends Source {
public readonly type = S3_SOURCE_TYPE;
private readonly bucket: s3.IBucket;
private readonly path: string;
private readonly sourceVersion?: string;

constructor(props: S3SourceProps) {
super(props);
this.bucket = props.bucket;
this.path = props.path;
this.sourceVersion = props.sourceVersion;
}

public bind(_scope: Construct, project: IProject): SourceConfig {
Expand All @@ -533,6 +560,7 @@ class S3Source extends Source {
...superConfig.sourceProperty,
location: `${this.bucket.bucketName}/${this.path}`,
},
sourceVersion: this.sourceVersion,
};
}
}
Expand All @@ -554,18 +582,29 @@ export interface GitHubSourceProps extends ThirdPartyGitSourceProps {
* @example 'aws-cdk'
*/
readonly repo: string;

/**
* The commit ID, pull request ID, branch name, or tag name that corresponds to
* the version of the source code you want to build
*
* @example 'pr/25'
* @default the default branch's HEAD commit ID is used
*/
readonly sourceVersion?: string;
knorms101 marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* GitHub Source definition for a CodeBuild project.
*/
class GitHubSource extends ThirdPartyGitSource {
public readonly type = GITHUB_SOURCE_TYPE;
private readonly sourceVersion?: string;
knorms101 marked this conversation as resolved.
Show resolved Hide resolved
private readonly httpsCloneUrl: string;

constructor(props: GitHubSourceProps) {
super(props);
this.httpsCloneUrl = `https://github.com/${props.owner}/${props.repo}.git`;
this.sourceVersion = props.sourceVersion;
}

public bind(_scope: Construct, project: IProject): SourceConfig {
Expand All @@ -575,6 +614,7 @@ class GitHubSource extends ThirdPartyGitSource {
...superConfig.sourceProperty,
location: this.httpsCloneUrl,
},
sourceVersion: this.sourceVersion,
buildTriggers: superConfig.buildTriggers,
};
}
Expand All @@ -595,6 +635,15 @@ export interface GitHubEnterpriseSourceProps extends ThirdPartyGitSourceProps {
* @default false
*/
readonly ignoreSslErrors?: boolean;

/**
* The commit ID, pull request ID, branch name, or tag name that corresponds to
* the version of the source code you want to build
*
* @example 'pr/25'
* @default the default branch's HEAD commit ID is used
*/
readonly sourceVersion?: string;
knorms101 marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand All @@ -604,11 +653,13 @@ class GitHubEnterpriseSource extends ThirdPartyGitSource {
public readonly type = GITHUB_ENTERPRISE_SOURCE_TYPE;
private readonly httpsCloneUrl: string;
private readonly ignoreSslErrors?: boolean;
private readonly sourceVersion?: string;

constructor(props: GitHubEnterpriseSourceProps) {
super(props);
this.httpsCloneUrl = props.httpsCloneUrl;
this.ignoreSslErrors = props.ignoreSslErrors;
this.sourceVersion = props.sourceVersion;
}

public bind(_scope: Construct, _project: IProject): SourceConfig {
Expand All @@ -619,6 +670,7 @@ class GitHubEnterpriseSource extends ThirdPartyGitSource {
location: this.httpsCloneUrl,
insecureSsl: this.ignoreSslErrors,
},
sourceVersion: this.sourceVersion,
buildTriggers: superConfig.buildTriggers,
};
}
Expand All @@ -641,6 +693,14 @@ export interface BitBucketSourceProps extends ThirdPartyGitSourceProps {
* @example 'aws-cdk'
*/
readonly repo: string;

/**
* The commit ID, branch name, or tag name that corresponds to the version of the source code you want to build
*
* @example 'mybranch'
* @default the default branch's HEAD commit ID is used
*/
readonly sourceVersion?: string;
knorms101 marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand All @@ -649,10 +709,12 @@ export interface BitBucketSourceProps extends ThirdPartyGitSourceProps {
class BitBucketSource extends ThirdPartyGitSource {
public readonly type = BITBUCKET_SOURCE_TYPE;
private readonly httpsCloneUrl: any;
private readonly sourceVersion?: string;

constructor(props: BitBucketSourceProps) {
super(props);
this.httpsCloneUrl = `https://bitbucket.org/${props.owner}/${props.repo}.git`;
this.sourceVersion = props.sourceVersion;
}

public bind(_scope: Construct, _project: IProject): SourceConfig {
Expand All @@ -672,6 +734,7 @@ class BitBucketSource extends ThirdPartyGitSource {
...superConfig.sourceProperty,
location: this.httpsCloneUrl,
},
sourceVersion: this.sourceVersion,
buildTriggers: superConfig.buildTriggers,
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,11 @@
"SourceIdentifier": "AddSource1",
"Type": "S3"
}
],
"SecondarySourceVersions": [
{
"SourceIdentifier": "AddSource1"
}
knorms101 marked this conversation as resolved.
Show resolved Hide resolved
]
}
}
Expand Down
70 changes: 70 additions & 0 deletions packages/@aws-cdk/aws-codebuild/test/test.codebuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -865,6 +865,76 @@ export = {
},
},

'secondary source versions': {
'allow secondary source versions'(test: Test) {
const stack = new cdk.Stack();
const bucket = new s3.Bucket(stack, 'MyBucket');
const project = new codebuild.Project(stack, 'MyProject', {
source: codebuild.Source.s3({
bucket,
path: 'some/path',
}),
});

project.addSecondarySource(codebuild.Source.s3({
bucket,
path: 'another/path',
identifier: 'source1',
sourceVersion: 'someversion'
}));

expect(stack).to(haveResourceLike('AWS::CodeBuild::Project', {
"SecondarySources": [
{
"SourceIdentifier": "source1",
"Type": "S3",
},
],
"SecondarySourceVersions": [
{
"SourceIdentifier": "source1",
"SourceVersion": "someversion"
}
]
}));

test.done();
},

'allow not to specify secondary source versions'(test: Test) {
const stack = new cdk.Stack();
const bucket = new s3.Bucket(stack, 'MyBucket');
const project = new codebuild.Project(stack, 'MyProject', {
source: codebuild.Source.s3({
bucket,
path: 'some/path',
}),
});

project.addSecondarySource(codebuild.Source.s3({
bucket,
path: 'another/path',
identifier: 'source1',
}));

expect(stack).to(haveResourceLike('AWS::CodeBuild::Project', {
"SecondarySources": [
{
"SourceIdentifier": "source1",
"Type": "S3",
},
],
"SecondarySourceVersions": [
{
"SourceIdentifier": "source1",
}
]
}));

test.done();
},
},

'secondary artifacts': {
'require providing an identifier when creating a Project'(test: Test) {
const stack = new cdk.Stack();
Expand Down
Loading