codebuild.tf 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. resource "aws_codebuild_project" "this" {
  2. name = var.name
  3. description = "Container for ${var.name}"
  4. service_role = var.service_role
  5. encryption_key = var.kms_key
  6. #badge_enabled = var.badge_enabled
  7. source {
  8. type = "GITHUB_ENTERPRISE"
  9. location = var.github_clone_url
  10. report_build_status = true
  11. git_clone_depth = 1
  12. buildspec = var.buildspec
  13. }
  14. source_version = var.source_version
  15. environment {
  16. compute_type = "BUILD_GENERAL1_SMALL"
  17. image = var.image
  18. type = "LINUX_CONTAINER"
  19. privileged_mode = var.privileged_mode
  20. dynamic "environment_variable" {
  21. for_each = var.env_vars
  22. iterator = each
  23. content {
  24. name = each.key
  25. value = each.value["value"]
  26. type = try(each.value["type"], "PLAINTEXT")
  27. }
  28. }
  29. }
  30. artifacts {
  31. type = "NO_ARTIFACTS"
  32. }
  33. tags = merge(var.standard_tags, var.tags)
  34. # Govcloud incompatible with "project visibility"
  35. # See https://github.com/hashicorp/terraform-provider-aws/issues/22473#issuecomment-1081187035
  36. lifecycle { ignore_changes = [project_visibility] }
  37. }
  38. # Only build the cloudwatch trigger if it's needed
  39. resource "aws_cloudwatch_event_rule" "schedule_rule" {
  40. count = var.schedule_expression == "" ? 0 : 1
  41. name = "scheduled_build-${var.name}"
  42. schedule_expression = var.schedule_expression
  43. }
  44. resource "aws_cloudwatch_event_target" "trigger_build" {
  45. count = var.schedule_expression == "" ? 0 : 1
  46. target_id = "trigger_build"
  47. rule = aws_cloudwatch_event_rule.schedule_rule[count.index].name
  48. arn = aws_codebuild_project.this.id
  49. role_arn = var.service_role
  50. }
  51. resource "aws_codebuild_webhook" "this" {
  52. count = var.enable_webhook == true ? 1 : 0
  53. project_name = var.name
  54. branch_filter = var.webhook_branch_filter
  55. depends_on = [aws_codebuild_project.this]
  56. }