main.tf 2.9 KB

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