main.tf 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. data "github_repository" "this" {
  2. name = var.name
  3. }
  4. resource "aws_codebuild_project" "this_no_artifact" {
  5. count = var.artifact_s3_bucket == "" ? 1 : 0
  6. name = var.name
  7. description = "Container for ${var.name}"
  8. service_role = var.service_role
  9. encryption_key = var.kms_key
  10. badge_enabled = var.badge_enabled
  11. source {
  12. type = "GITHUB_ENTERPRISE"
  13. location = data.github_repository.this.http_clone_url
  14. report_build_status = true
  15. git_clone_depth = 1
  16. git_submodules_config {
  17. fetch_submodules = var.fetch_submodules
  18. }
  19. }
  20. source_version = var.source_version
  21. environment {
  22. compute_type = "BUILD_GENERAL1_SMALL"
  23. image = var.codebuild_image
  24. type = "LINUX_CONTAINER"
  25. privileged_mode = true
  26. }
  27. artifacts {
  28. type = "NO_ARTIFACTS"
  29. }
  30. tags = merge(local.standard_tags, var.tags)
  31. # Govcloud incompatible with "project visibility"
  32. # See https://github.com/hashicorp/terraform-provider-aws/issues/22473#issuecomment-1081187035
  33. lifecycle { ignore_changes = [project_visibility] }
  34. }
  35. resource "aws_ecr_repository" "this" {
  36. name = var.name
  37. image_scanning_configuration {
  38. scan_on_push = true
  39. }
  40. }
  41. data "aws_iam_policy_document" "ecr_cross_account_policy" {
  42. statement {
  43. sid = "ECRWrite"
  44. effect = "Allow"
  45. actions = [
  46. "ecr:GetAuthorizationToken",
  47. "ecr:GetDownloadUrlForLayer",
  48. "ecr:BatchGetImage",
  49. "ecr:BatchCheckLayerAvailability",
  50. "ecr:PutImage",
  51. "ecr:InitiateLayerUpload",
  52. "ecr:UploadLayerPart",
  53. "ecr:CompleteLayerUpload",
  54. "ecr:DescribeRepositories",
  55. "ecr:ListImages",
  56. "ecr:DescribeImages",
  57. ]
  58. principals {
  59. type = "AWS"
  60. identifiers = sort([for a in local.responsible_accounts[var.environment] : "arn:${var.aws_partition}:iam::${a}:root"])
  61. }
  62. }
  63. # Allow codebuild access
  64. statement {
  65. sid = "CodeBuildAccessPrincipal"
  66. effect = "Allow"
  67. actions = [
  68. "ecr:GetDownloadUrlForLayer",
  69. "ecr:BatchGetImage",
  70. "ecr:BatchCheckLayerAvailability",
  71. ]
  72. principals {
  73. type = "Service"
  74. identifiers = ["codebuild.amazonaws.com"]
  75. }
  76. }
  77. }
  78. resource "aws_ecr_repository_policy" "this" {
  79. repository = aws_ecr_repository.this.name
  80. policy = data.aws_iam_policy_document.ecr_cross_account_policy.json
  81. }
  82. resource "aws_ecr_lifecycle_policy" "this" {
  83. repository = aws_ecr_repository.this.name
  84. policy = file("${path.module}/default-lifecycle-policy.json")
  85. }
  86. resource "aws_codebuild_webhook" "this" {
  87. project_name = var.name
  88. branch_filter = var.webhook_branch_filter
  89. depends_on = [aws_codebuild_project.this_no_artifact]
  90. }
  91. resource "github_repository_webhook" "this" {
  92. count = var.enable_webhooks ? 1 : 0
  93. active = true
  94. events = ["push"]
  95. repository = data.github_repository.this.name
  96. configuration {
  97. url = aws_codebuild_webhook.this.payload_url
  98. secret = aws_codebuild_webhook.this.secret
  99. content_type = "json"
  100. insecure_ssl = false
  101. }
  102. }