main.tf 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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(var.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. project_name = var.name
  70. filter_group {
  71. filter {
  72. type = "EVENT"
  73. pattern = "PUSH"
  74. }
  75. filter {
  76. type = "HEAD_REF"
  77. pattern = var.webhook_filter_pattern
  78. }
  79. }
  80. depends_on = [aws_codebuild_project.this]
  81. }
  82. resource "github_repository_webhook" "this" {
  83. active = true
  84. events = ["push"]
  85. repository = data.github_repository.this.name
  86. configuration {
  87. url = aws_codebuild_webhook.this.payload_url
  88. secret = aws_codebuild_webhook.this.secret
  89. content_type = "json"
  90. insecure_ssl = false
  91. }
  92. }