main.tf 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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_submodules_config {
  16. fetch_submodules = false
  17. }
  18. }
  19. source_version = var.source_version
  20. environment {
  21. compute_type = "BUILD_GENERAL1_SMALL"
  22. image = var.codebuild_image
  23. type = "LINUX_CONTAINER"
  24. privileged_mode = true
  25. }
  26. artifacts {
  27. type = "NO_ARTIFACTS"
  28. }
  29. tags = merge(var.standard_tags, var.tags)
  30. }
  31. resource "aws_ecr_repository" "this" {
  32. name = var.name
  33. image_scanning_configuration {
  34. scan_on_push = true
  35. }
  36. }
  37. data "aws_iam_policy_document" "ecr_cross_account_policy" {
  38. statement {
  39. sid = "ECRWrite"
  40. effect = "Allow"
  41. actions = [
  42. "ecr:GetAuthorizationToken",
  43. "ecr:GetDownloadUrlForLayer",
  44. "ecr:BatchGetImage",
  45. "ecr:BatchCheckLayerAvailability",
  46. "ecr:PutImage",
  47. "ecr:InitiateLayerUpload",
  48. "ecr:UploadLayerPart",
  49. "ecr:CompleteLayerUpload",
  50. "ecr:DescribeRepositories",
  51. "ecr:ListImages",
  52. "ecr:DescribeImages",
  53. ]
  54. principals {
  55. type = "AWS"
  56. identifiers = [ for a in var.responsible_accounts[var.environment]: "arn:${var.aws_partition}:iam::${a}:root" ]
  57. }
  58. }
  59. # Allow codebuild access
  60. statement {
  61. sid = "CodeBuildAccessPrincipal"
  62. effect = "Allow"
  63. actions = [
  64. "ecr:GetDownloadUrlForLayer",
  65. "ecr:BatchGetImage",
  66. "ecr:BatchCheckLayerAvailability",
  67. ]
  68. principals {
  69. type = "Service"
  70. identifiers = ["codebuild.amazonaws.com"]
  71. }
  72. }
  73. }
  74. resource "aws_ecr_repository_policy" "this" {
  75. repository = aws_ecr_repository.this.name
  76. policy = data.aws_iam_policy_document.ecr_cross_account_policy.json
  77. }
  78. resource "aws_ecr_lifecycle_policy" "this" {
  79. repository = aws_ecr_repository.this.name
  80. policy = file("${path.module}/default-lifecycle-policy.json")
  81. }
  82. resource "aws_codebuild_webhook" "this" {
  83. project_name = var.name
  84. branch_filter = var.webhook_branch_filter
  85. depends_on = [ aws_codebuild_project.this_no_artifact ]
  86. }
  87. resource "github_repository_webhook" "this" {
  88. count = var.enable_webhooks ? 1 : 0
  89. active = true
  90. events = ["push"]
  91. repository = data.github_repository.this.name
  92. configuration {
  93. url = aws_codebuild_webhook.this.payload_url
  94. secret = aws_codebuild_webhook.this.secret
  95. content_type = "json"
  96. insecure_ssl = false
  97. }
  98. }