public CodeCoveragePipelineStack(Construct scope, string id, IStackProps props = null) : base(scope, id, props) { var buildProjectName = Constants.Project + Constants.BuildProject; var buildProject = new PipelineProject(this, buildProjectName, new PipelineProjectProps { ProjectName = buildProjectName, BuildSpec = BuildSpec.FromObject(new Dictionary <string, object> { ["version"] = "0.2", ["phases"] = new Dictionary <string, object> { ["install"] = new Dictionary <string, object> { ["runtime-versions"] = new Dictionary <string, object> { ["dotnet"] = "latest" }, ["commands"] = new string[] { "dotnet tool install -g dotnet-reportgenerator-globaltool", "export PATH=\"$PATH:/root/.dotnet/tools\"" } }, ["build"] = new Dictionary <string, object> { ["commands"] = new string[] { "dotnet build", "dotnet test --collect:\"XPlat Code Coverage\" --logger trx", "xmlPath=$(find $CODEBUILD_SRC_DIR/Calculator.Tests/TestResults -name \"*.xml\")", "reportgenerator -reports:$xmlPath -targetdir:$CODEBUILD_SRC_DIR/Calculator.Tests/TestResults/" } } }, ["reports"] = new Dictionary <string, object> { [Constants.TestReportGroup] = new Dictionary <string, object> { ["files"] = new string[] { "**/*.trx" }, ["base-directory"] = "$CODEBUILD_SRC_DIR/Calculator.Tests/TestResults", ["file-format"] = "VisualStudioTrx" }, [Constants.CodeCoverageReportGroup] = new Dictionary <string, object> { ["files"] = new string[] { "**/*.xml" }, ["base-directory"] = "$CODEBUILD_SRC_DIR/Calculator.Tests/TestResults", ["file-format"] = "CoberturaXml" } }, ["artifacts"] = new Dictionary <string, object> { ["files"] = new string[] { "**/*" }, ["name"] = "coverlet-$(date +%Y-%m-%d)", ["base-directory"] = "$CODEBUILD_SRC_DIR/Calculator.Tests/TestResults/" } }), Environment = new BuildEnvironment { BuildImage = LinuxBuildImage.AMAZON_LINUX_2_3 } }); var bucketName = "codepipeline-ap-southeast-2-822565979272"; var testReportGroupName = buildProjectName + "-" + Constants.TestReportGroup; var testReportGroup = new CfnReportGroup(this, "TestReportGroup", new CfnReportGroupProps { Name = testReportGroupName, Type = "TEST", ExportConfig = new CfnReportGroup.ReportExportConfigProperty { ExportConfigType = "S3", S3Destination = new CfnReportGroup.S3ReportExportConfigProperty { Bucket = bucketName, Packaging = "NONE", Path = "TestReports" } } }); var codeCoverageReportGroupName = buildProjectName + "-" + Constants.CodeCoverageReportGroup; var CodeCoverageReportGroup = new CfnReportGroup(this, "CodeCoverageReportGroup", new CfnReportGroupProps { Name = codeCoverageReportGroupName, Type = "CODE_COVERAGE", ExportConfig = new CfnReportGroup.ReportExportConfigProperty { ExportConfigType = "S3", S3Destination = new CfnReportGroup.S3ReportExportConfigProperty { Bucket = bucketName, Packaging = "NONE", Path = "CodeCoverageReports" } } }); buildProject.AddToRolePolicy( new PolicyStatement(new PolicyStatementProps { Effect = Effect.ALLOW, Actions = new [] { "codebuild:BatchPutCodeCoverages" }, Resources = new [] { Fn.Join("", new string[] { "arn:", Fn.Ref("AWS::Partition"), ":codebuild:", Fn.Ref("AWS::Region"), ":", Fn.Ref("AWS::AccountId"), ":report-group/", buildProject.ProjectName, "-*" }) } }) ); var code = Repository.FromRepositoryName(this, "TestCoverageRepo", "TestCoverageRepo"); var artifactsBucket = Bucket.FromBucketName(this, "BucketByName", bucketName); var sourceOutput = new Artifact_("sourceArtifact"); var buildOutput = new Artifact_("buildArtifact"); var pipelineName = Constants.Project + "Pipeline"; new Pipeline(this, pipelineName, new PipelineProps { PipelineName = pipelineName, ArtifactBucket = artifactsBucket, Stages = new[] { new StageProps { StageName = "Source", Actions = new [] { new CodeCommitSourceAction(new CodeCommitSourceActionProps { ActionName = "SourceAction", Repository = code, Output = sourceOutput }) } }, new StageProps { StageName = "Build", Actions = new [] { new CodeBuildAction(new CodeBuildActionProps { ActionName = "BuildAction", Project = buildProject, Input = sourceOutput, Outputs = new [] { buildOutput } }) } }, } }); }
public CiCdStack(Construct parent, string id, CiCdStackProps props) : base(parent, id, props) { var apiRepository = Amazon.CDK.AWS.CodeCommit.Repository.FromRepositoryArn(this, "Repository", props.apiRepositoryArn); var environmentVariables = new Dictionary <string, IBuildEnvironmentVariable>(); environmentVariables.Add("AWS_ACCOUNT_ID", new BuildEnvironmentVariable() { Type = BuildEnvironmentVariableType.PLAINTEXT, Value = Aws.ACCOUNT_ID }); environmentVariables.Add("AWS_DEFAULT_REGION", new BuildEnvironmentVariable() { Type = BuildEnvironmentVariableType.PLAINTEXT, Value = Aws.REGION }); var codebuildProject = new PipelineProject(this, "BuildProject", new PipelineProjectProps { Environment = new BuildEnvironment { ComputeType = ComputeType.SMALL, BuildImage = LinuxBuildImage.UBUNTU_14_04_PYTHON_3_5_2, Privileged = true, EnvironmentVariables = environmentVariables } }); var codeBuildPolicy = new PolicyStatement(); codeBuildPolicy.AddResources(apiRepository.RepositoryArn); codeBuildPolicy.AddActions( "codecommit:ListBranches", "codecommit:ListRepositories", "codecommit:BatchGetRepositories", "codecommit:GitPull" ); codebuildProject.AddToRolePolicy( codeBuildPolicy ); props.ecrRepository.GrantPullPush(codebuildProject.GrantPrincipal); var sourceOutput = new Artifact_(); var sourceAction = new Amazon.CDK.AWS.CodePipeline.Actions.CodeCommitSourceAction( new Amazon.CDK.AWS.CodePipeline.Actions.CodeCommitSourceActionProps { ActionName = "CodeCommit-Source", Branch = "master", Trigger = CodeCommitTrigger.POLL, Repository = apiRepository, Output = sourceOutput }); var buildOutput = new Artifact_(); var buildAction = new CodeBuildAction(new CodeBuildActionProps { ActionName = "Build", Input = sourceOutput, Outputs = new Artifact_[] { buildOutput }, Project = codebuildProject }); var deployAction = new EcsDeployAction(new EcsDeployActionProps { ActionName = "DeployAction", Input = buildOutput, Service = props.ecsService, }); var pipeline = new Pipeline(this, "Pipeline"); pipeline.AddStage(new StageOptions { StageName = "Source", Actions = new Action[] { sourceAction } }); pipeline.AddStage(new StageOptions { StageName = "Build", Actions = new Action[] { buildAction } }); pipeline.AddStage(new StageOptions { StageName = "Deploy", Actions = new Action[] { deployAction } }); }