main.tf 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. data "github_repository" "this" {
  2. name = var.repository_name
  3. }
  4. resource "aws_codebuild_project" "this" {
  5. name = var.name
  6. description = "Codebuild for ${var.name}"
  7. service_role = var.service_role
  8. encryption_key = var.kms_key
  9. #badge_enabled = var.badge_enabled
  10. source {
  11. type = "GITHUB_ENTERPRISE"
  12. location = var.github_clone_url
  13. report_build_status = true
  14. git_clone_depth = 1
  15. buildspec = var.buildspec
  16. }
  17. source_version = var.source_version
  18. environment {
  19. compute_type = "BUILD_GENERAL1_SMALL"
  20. image = var.image
  21. type = "LINUX_CONTAINER"
  22. privileged_mode = var.privileged_mode
  23. dynamic "environment_variable" {
  24. for_each = var.env_vars
  25. iterator = each
  26. content {
  27. name = each.key
  28. value = each.value["value"]
  29. type = try(each.value["type"], "PLAINTEXT")
  30. }
  31. }
  32. environment_variable {
  33. name = "SECURITYGROUP"
  34. value = aws_security_group.this.id
  35. }
  36. environment_variable {
  37. name = "IAMINSTANCEPROFILE"
  38. value = aws_iam_instance_profile.magic_machine.id
  39. }
  40. environment_variable {
  41. name = "SUBNETID"
  42. value = var.public_subnets[0]
  43. }
  44. environment_variable {
  45. name = "GITBRANCH"
  46. value = var.source_version
  47. }
  48. }
  49. vpc_config {
  50. vpc_id = data.aws_vpc.this.id
  51. subnets = var.private_subnets
  52. security_group_ids = [
  53. aws_security_group.codebuild.id
  54. ]
  55. }
  56. artifacts {
  57. type = "NO_ARTIFACTS"
  58. }
  59. tags = merge(local.standard_tags, var.tags)
  60. # The security group must be created before the codebuild project for the
  61. # environmental variables.
  62. depends_on = [aws_security_group.this, aws_security_group.codebuild]
  63. # Govcloud incompatible with "project visibility"
  64. # See https://github.com/hashicorp/terraform-provider-aws/issues/22473#issuecomment-1081187035
  65. lifecycle { ignore_changes = [project_visibility] }
  66. }
  67. resource "aws_codebuild_webhook" "this" {
  68. # Disable the webhook for now. It is too aggresive when making quick changes.
  69. count = var.enable_webhook == true ? 1 : 0
  70. project_name = var.name
  71. filter_group {
  72. filter {
  73. type = "EVENT"
  74. pattern = "PUSH"
  75. }
  76. filter {
  77. type = "HEAD_REF"
  78. pattern = var.webhook_filter_pattern
  79. }
  80. }
  81. depends_on = [aws_codebuild_project.this]
  82. }
  83. resource "github_repository_webhook" "this" {
  84. count = var.enable_webhook == true ? 1 : 0
  85. active = true
  86. events = ["push"]
  87. repository = data.github_repository.this.name
  88. configuration {
  89. url = aws_codebuild_webhook.this[count.index].payload_url
  90. secret = aws_codebuild_webhook.this[count.index].secret
  91. content_type = "json"
  92. insecure_ssl = false
  93. }
  94. }