Skip to content

Instantly share code, notes, and snippets.

@pen-pal
Last active December 4, 2023 02:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pen-pal/bb952da086dccb67d294e49f5624be66 to your computer and use it in GitHub Desktop.
Save pen-pal/bb952da086dccb67d294e49f5624be66 to your computer and use it in GitHub Desktop.
codebuild.tf
variable "codebuild_specs" {
type = any
default = {}
#sample example
#codebuild_specs = {
# "test-api0" = {
# app_type = "java"
# location = "https://github.com/pen-pal/test-api0"
# buildspec = "buildspec.yml"
# nodeenv = "16"
# namespace = "default"
# }
# "test-api1" = {
# app_type = "java"
# location = "https://github.com/pen-pal/test-api1"
# buildspec = "buildspec.yml"
# nodeenv = "16"
# namespace = "default"
# }
#}
}
resource "aws_codebuild_project" "codebuild" {
for_each = var.codebuild_specs
name = each.key
description = "codebuild service build."
build_timeout = "15"
queued_timeout = "15"
service_role = aws_iam_role.codebuild_role.arn
environment {
compute_type = "BUILD_GENERAL1_SMALL"
image = "aws/codebuild/standard:4.0"
type = "LINUX_CONTAINER"
privileged_mode = true
environment_variable {
name = "NAMESPACE"
value = each.value.namespace
}
environment_variable {
name = "_ENV"
value = each.value.nodeenv
type = "PLAINTEXT"
}
environment_variable {
name = "GIT_TOKEN"
value = "GIT_TOKEN"
type = "PARAMETER_STORE"
}
}
source {
type = "GITHUB"
location = each.value.location #"https://github.com/ORG/react.auth-app"]
git_clone_depth = 1
insecure_ssl = false
report_build_status = true
buildspec = "${aws_s3_bucket.builspec_bucket.arn}/${each.value.buildspec}"
}
artifacts {
type = "NO_ARTIFACTS"
}
}
resource "aws_codebuild_webhook" "codebuild" {
for_each = var.codebuild_specs
project_name = each.key
#project_name = aws_codebuild_project.codebuild.name
filter_group {
filter {
type = "EVENT"
pattern = "PUSH"
}
filter {
type = "HEAD_REF"
pattern = "^refs/tags/.*"
}
}
filter_group {
filter {
type = "EVENT"
pattern = "PUSH"
}
filter {
type = "HEAD_REF"
pattern = "^refs/heads/.*"
exclude_matched_pattern = true
}
}
filter_group {
filter {
type = "EVENT"
pattern = "PULL_REQUEST_CREATED,PULL_REQUEST_UPDATED,PULL_REQUEST_REOPENED"
}
filter {
type = "HEAD_REF"
pattern = "^refs/heads/.*"
exclude_matched_pattern = true
}
}
depends_on = [aws_codebuild_project.codebuild]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment