-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): hotswap deployments for CodeBuild projects (#18161)
This extends the `cdk deploy --hotswap` command to support CodeBuild projects. This supports all changes to the `Source`, `SourceVersion`, and `Environment` attributes of the AWS::CodeBuild::Project cloudformation resource. The possible changes supported on the [Project](https://docs.aws.amazon.com/cdk/api/v1/docs/@aws-cdk_aws-codebuild.Project.html) L2 Construct will be changes to the [buildSpec](https://docs.aws.amazon.com/cdk/api/v1/docs/@aws-cdk_aws-codebuild.Project.html#buildspec), [environment](https://docs.aws.amazon.com/cdk/api/v1/docs/@aws-cdk_aws-codebuild.Project.html#environment), [environmentVariables](https://docs.aws.amazon.com/cdk/api/v1/docs/@aws-cdk_aws-codebuild.Project.html#environmentvariables), and [source](https://docs.aws.amazon.com/cdk/api/v1/docs/@aws-cdk_aws-codebuild.Project.html#source) constructor props. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
- Loading branch information
1 parent
caa5178
commit 4ae4df8
Showing
9 changed files
with
731 additions
and
20 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import * as AWS from 'aws-sdk'; | ||
import { ISDK } from '../aws-auth'; | ||
import { ChangeHotswapImpact, ChangeHotswapResult, establishResourcePhysicalName, HotswapOperation, HotswappableChangeCandidate, lowerCaseFirstCharacter, transformObjectKeys } from './common'; | ||
import { EvaluateCloudFormationTemplate } from './evaluate-cloudformation-template'; | ||
|
||
export async function isHotswappableCodeBuildProjectChange( | ||
logicalId: string, change: HotswappableChangeCandidate, evaluateCfnTemplate: EvaluateCloudFormationTemplate, | ||
): Promise<ChangeHotswapResult> { | ||
if (change.newValue.Type !== 'AWS::CodeBuild::Project') { | ||
return ChangeHotswapImpact.REQUIRES_FULL_DEPLOYMENT; | ||
} | ||
|
||
const updateProjectInput: AWS.CodeBuild.UpdateProjectInput = { | ||
name: '', | ||
}; | ||
for (const updatedPropName in change.propertyUpdates) { | ||
const updatedProp = change.propertyUpdates[updatedPropName]; | ||
switch (updatedPropName) { | ||
case 'Source': | ||
updateProjectInput.source = transformObjectKeys( | ||
await evaluateCfnTemplate.evaluateCfnExpression(updatedProp.newValue), | ||
convertSourceCloudformationKeyToSdkKey, | ||
); | ||
break; | ||
case 'Environment': | ||
updateProjectInput.environment = await transformObjectKeys( | ||
await evaluateCfnTemplate.evaluateCfnExpression(updatedProp.newValue), | ||
lowerCaseFirstCharacter, | ||
); | ||
break; | ||
case 'SourceVersion': | ||
updateProjectInput.sourceVersion = await evaluateCfnTemplate.evaluateCfnExpression(updatedProp.newValue); | ||
break; | ||
default: | ||
return ChangeHotswapImpact.REQUIRES_FULL_DEPLOYMENT; | ||
} | ||
} | ||
|
||
const projectName = await establishResourcePhysicalName(logicalId, change.newValue.Properties?.Name, evaluateCfnTemplate); | ||
if (!projectName) { | ||
return ChangeHotswapImpact.REQUIRES_FULL_DEPLOYMENT; | ||
} | ||
updateProjectInput.name = projectName; | ||
return new ProjectHotswapOperation(updateProjectInput); | ||
} | ||
|
||
class ProjectHotswapOperation implements HotswapOperation { | ||
public readonly service = 'codebuild' | ||
public readonly resourceNames: string[]; | ||
|
||
constructor( | ||
private readonly updateProjectInput: AWS.CodeBuild.UpdateProjectInput, | ||
) { | ||
this.resourceNames = [updateProjectInput.name]; | ||
} | ||
|
||
public async apply(sdk: ISDK): Promise<any> { | ||
return sdk.codeBuild().updateProject(this.updateProjectInput).promise(); | ||
} | ||
} | ||
|
||
function convertSourceCloudformationKeyToSdkKey(key: string): string { | ||
if (key.toLowerCase() === 'buildspec') { | ||
return key.toLowerCase(); | ||
} | ||
return lowerCaseFirstCharacter(key); | ||
} |
This file contains 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
This file contains 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
Oops, something went wrong.