diff --git a/.changelog/15442.txt b/.changelog/15442.txt new file mode 100644 index 00000000000..81e63318ac5 --- /dev/null +++ b/.changelog/15442.txt @@ -0,0 +1,3 @@ +```release-note:enhancement +resource/aws_codebuild_project: Add `build_status_config` attribute to `source` and `secondary_sources` configuration blocks +``` \ No newline at end of file diff --git a/aws/resource_aws_codebuild_project.go b/aws/resource_aws_codebuild_project.go index 71cf794a9dc..f49df973834 100644 --- a/aws/resource_aws_codebuild_project.go +++ b/aws/resource_aws_codebuild_project.go @@ -470,6 +470,23 @@ func resourceAwsCodeBuildProject() *schema.Resource { Type: schema.TypeString, Required: true, }, + "build_status_config": { + Type: schema.TypeList, + Optional: true, + MaxItems: 1, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "context": { + Type: schema.TypeString, + Optional: true, + }, + "target_url": { + Type: schema.TypeString, + Optional: true, + }, + }, + }, + }, }, }, }, @@ -554,6 +571,23 @@ func resourceAwsCodeBuildProject() *schema.Resource { Type: schema.TypeBool, Optional: true, }, + "build_status_config": { + Type: schema.TypeList, + Optional: true, + MaxItems: 1, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "context": { + Type: schema.TypeString, + Optional: true, + }, + "target_url": { + Type: schema.TypeString, + Optional: true, + }, + }, + }, + }, }, }, }, @@ -1072,6 +1106,24 @@ func expandProjectSourceData(data map[string]interface{}) codebuild.ProjectSourc } } + // Only valid for BITBUCKET, GITHUB, GITHUB_ENTERPRISE source types. + if sourceType == codebuild.SourceTypeBitbucket || sourceType == codebuild.SourceTypeGithub || sourceType == codebuild.SourceTypeGithubEnterprise { + if v, ok := data["build_status_config"]; ok && len(v.([]interface{})) > 0 { + config := v.([]interface{})[0].(map[string]interface{}) + + buildStatusConfig := &codebuild.BuildStatusConfig{} + + if v, ok := config["context"]; ok { + buildStatusConfig.Context = aws.String(v.(string)) + } + if v, ok := config["target_url"]; ok { + buildStatusConfig.TargetUrl = aws.String(v.(string)) + } + + projectSource.BuildStatusConfig = buildStatusConfig + } + } + return projectSource } @@ -1461,6 +1513,8 @@ func flattenAwsCodeBuildProjectSourceData(source *codebuild.ProjectSource) inter m["git_submodules_config"] = flattenAwsCodebuildProjectGitSubmodulesConfig(source.GitSubmodulesConfig) + m["build_status_config"] = flattenAwsCodebuildProjectBuildStatusConfig(source.BuildStatusConfig) + if source.Auth != nil { m["auth"] = []interface{}{sourceAuthToMap(source.Auth)} } @@ -1483,6 +1537,19 @@ func flattenAwsCodebuildProjectGitSubmodulesConfig(config *codebuild.GitSubmodul return []interface{}{values} } +func flattenAwsCodebuildProjectBuildStatusConfig(config *codebuild.BuildStatusConfig) []interface{} { + if config == nil { + return []interface{}{} + } + + values := map[string]interface{}{ + "context": aws.StringValue(config.Context), + "target_url": aws.StringValue(config.TargetUrl), + } + + return []interface{}{values} +} + func flattenAwsCodeBuildVpcConfig(vpcConfig *codebuild.VpcConfig) []interface{} { if vpcConfig != nil { values := map[string]interface{}{} diff --git a/aws/resource_aws_codebuild_project_test.go b/aws/resource_aws_codebuild_project_test.go index 384bb416f81..7e7a6abd744 100644 --- a/aws/resource_aws_codebuild_project_test.go +++ b/aws/resource_aws_codebuild_project_test.go @@ -13,6 +13,16 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" ) +func init() { + RegisterServiceErrorCheckFunc(codebuild.EndpointsID, testAccErrorCheckSkipCodebuild) +} + +func testAccErrorCheckSkipCodebuild(t *testing.T) resource.ErrorCheckFunc { + return testAccErrorCheckSkipMessagesContaining(t, + "InvalidInputException: Region", + ) +} + // This is used for testing aws_codebuild_webhook as well as aws_codebuild_project. // The Terraform AWS user must have done the manual Bitbucket OAuth dance for this // functionality to work. Additionally, the Bitbucket user that the Terraform AWS @@ -858,6 +868,36 @@ func TestAccAWSCodeBuildProject_SecondarySources_GitSubmodulesConfig_GitHubEnter }) } +func TestAccAWSCodeBuildProject_Source_BuildStatusConfig_GitHubEnterprise(t *testing.T) { + var project codebuild.Project + rName := acctest.RandomWithPrefix("tf-acc-test") + resourceName := "aws_codebuild_project.test" + + if testAccGetPartition() == "aws-us-gov" { + t.Skip("CodeBuild Project build status config is not supported in GovCloud partition") + } + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t); testAccPreCheckAWSCodeBuild(t) }, + ErrorCheck: testAccErrorCheck(t, codebuild.EndpointsID), + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSCodeBuildProjectDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSCodeBuildProjectConfig_Source_BuildStatusConfig_GitHubEnterprise(rName), + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSCodeBuildProjectExists(resourceName, &project), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + func TestAccAWSCodeBuildProject_Source_InsecureSSL(t *testing.T) { var project codebuild.Project rName := acctest.RandomWithPrefix("tf-acc-test") @@ -4197,3 +4237,32 @@ resource "aws_codebuild_project" "test" { } `, rName)) } + +func testAccAWSCodeBuildProjectConfig_Source_BuildStatusConfig_GitHubEnterprise(rName string) string { + return testAccAWSCodeBuildProjectConfig_Base_ServiceRole(rName) + fmt.Sprintf(` +resource "aws_codebuild_project" "test" { + name = "%s" + service_role = aws_iam_role.test.arn + + artifacts { + type = "NO_ARTIFACTS" + } + + environment { + compute_type = "BUILD_GENERAL1_SMALL" + image = "2" + type = "LINUX_CONTAINER" + } + + source { + location = "https://example.com/organization/repository.git" + type = "GITHUB_ENTERPRISE" + + build_status_config { + context = "codebuild" + target_url = "https://example.com/$${CODEBUILD_BUILD_ID}" + } + } +} +`, rName) +} diff --git a/website/docs/r/codebuild_project.html.markdown b/website/docs/r/codebuild_project.html.markdown index 4d84aac4f54..2d0362958b3 100755 --- a/website/docs/r/codebuild_project.html.markdown +++ b/website/docs/r/codebuild_project.html.markdown @@ -323,6 +323,7 @@ Credentials for access to a private Docker registry. * `insecure_ssl` - (Optional) Ignore SSL warnings when connecting to source control. * `location` - (Optional) Location of the source code from git or s3. * `report_build_status` - (Optional) Whether to report the status of a build's start and finish to your source provider. This option is only valid when your source provider is `GITHUB`, `BITBUCKET`, or `GITHUB_ENTERPRISE`. +* `build_status_config` - (Optional) Contains information that defines how the build project reports the build status to the source provider. This option is only used when the source provider is `GITHUB`, `GITHUB_ENTERPRISE`, or `BITBUCKET`. * `source_identifier` - (Required) Source identifier. Source data will be put inside a folder named as this parameter inside AWS CodeBuild source directory * `type` - (Required) Type of repository that contains the source code to be built. Valid values: `CODECOMMIT`, `CODEPIPELINE`, `GITHUB`, `GITHUB_ENTERPRISE`, `BITBUCKET` or `S3`. @@ -337,7 +338,12 @@ This block is only valid when the `type` is `CODECOMMIT`, `GITHUB` or `GITHUB_EN * `fetch_submodules` - (Required) Whether to fetch Git submodules for the AWS CodeBuild build project. -### source +`build_status_config` supports the following: + +* `context` - (Optional) Specifies the context of the build status CodeBuild sends to the source provider. The usage of this parameter depends on the source provider. +* `target_url` - (Optional) Specifies the target url of the build status CodeBuild sends to the source provider. The usage of this parameter depends on the source provider. + +`vpc_config` supports the following: * `auth` - (Optional, **Deprecated**) Configuration block with the authorization settings for AWS CodeBuild to access the source code to be built. This information is for the AWS CodeBuild console's use only. Use the [`aws_codebuild_source_credential` resource](codebuild_source_credential.html) instead. Auth blocks are documented below. * `buildspec` - (Optional) Build specification to use for this build project's related builds. This must be set when `type` is `NO_SOURCE`.