Skip to content

Commit 9ac9562

Browse files
committed
Restructure git inputs by parsing git_url
1 parent a71f6ae commit 9ac9562

3 files changed

Lines changed: 20 additions & 40 deletions

File tree

main.tf

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#########
1+
##########
22
# Defaults
33
##########
44

@@ -17,9 +17,11 @@ resource "random_string" "rand6" {
1717
}
1818

1919
locals {
20-
prefix = "aws-ia"
21-
delimiter = "-"
22-
random_project_name = "${local.prefix}${local.delimiter}${random_string.rand6.result}"
20+
git_provider = element(split("/", var.http_git_clone_url), 2)
21+
git_protocal = element(split(":", var.http_git_clone_url), 0)
22+
git_owner = element(split("/", var.http_git_clone_url), 3)
23+
git_repo = trimsuffix(element(split("/", var.http_git_clone_url), 4), ".git")
24+
random_project_name = "${local.git_repo}-${random_string.rand6.result}"
2325
codebuild_project_name = (var.project_name != "" ? var.project_name : "${local.random_project_name}")
2426
}
2527

@@ -37,7 +39,7 @@ resource "aws_codebuild_project" "codebuild_project" {
3739

3840
source {
3941
type = "GITHUB"
40-
location = var.git_repo
42+
location = "${local.git_protocal}://${local.git_provider}/${local.git_owner}/${local.git_repo}.git"
4143
git_clone_depth = var.git_clone_depth
4244
buildspec = templatefile("${path.cwd}/${var.build_spec_file}", {})
4345
git_submodules_config {
@@ -101,3 +103,4 @@ resource "aws_iam_role_policy_attachment" "codebuild_deploy" {
101103
role = aws_iam_role.codebuild_role[0].name
102104
policy_arn = "arn:aws:iam::aws:policy/AdministratorAccess"
103105
}
106+

outputs.tf

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,19 @@ output "codebuild_badge_url" {
1313
value = aws_codebuild_project.codebuild_project.badge_url
1414
}
1515

16-
output "codebuild_tags_all" {
17-
description = "CodeBuild Badge Url"
18-
value = aws_codebuild_project.codebuild_project.tags_all
19-
}
20-
21-
output "codebuild_role_arn" {
22-
description = "CodeBuild Role Arn"
23-
value = var.create_role_and_policy ? aws_iam_role.codebuild_role[0].arn : var.codebuild_role_arn
24-
}
25-
2616
output "codebuild_project_name" {
2717
description = "CodeBuild Project Name"
2818
value = aws_codebuild_project.codebuild_project.name
2919
}
3020

21+
output "git_repo_name" {
22+
description = "GitRepo Name"
23+
value = local.git_repo
24+
}
3125

32-
33-
34-
35-
36-
37-
26+
output "codebuild_tags_all" {
27+
description = "CodeBuild Badge Url"
28+
value = aws_codebuild_project.codebuild_project.tags_all
29+
}
3830

3931

variable.tf

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,56 +2,45 @@ variable "region" {
22
description = "Codebuild region"
33
default = "us-east-1"
44
}
5-
65
variable "artifacts_type" {
76
description = "Type of artifacts (supports: NO_ARTIFACTS or CODEPIPELINE)"
87
default = "NO_ARTIFACTS"
98
}
10-
119
variable "project_name" {
1210
description = "Codebuild Project Name (Leave empty to autogenerate}"
13-
default = "example_project"
11+
default = ""
1412
}
15-
16-
variable "git_repo" {
17-
description = "Github repo (FullPath for to pull via git clone) "
13+
variable "http_git_clone_url" {
14+
description = "Enter: Git Clone URL"
15+
type = string
1816
default = "https://github.com/aws-ia/terraform-modules-examples"
1917
}
20-
2118
variable "git_clone_depth" {
2219
description = "Repo clone depth"
2320
default = "1"
2421
}
25-
2622
variable "build_image" {
2723
description = "Build container image"
2824
default = "aws/codebuild/amazonlinux2-x86_64-standard:3.0"
2925
}
30-
3126
variable "compute_type" {
3227
description = "Compute type"
3328
default = "BUILD_GENERAL1_MEDIUM"
3429
}
35-
3630
variable "build_spec_file" {
3731
description = "Build spec file name "
3832
default = "buildspecs/terraform_env.yml"
39-
4033
}
41-
4234
variable "tags" {
4335
description = "Codebuild Tags"
4436
default = { "Name" = "aws-ia-codebuild" }
4537
}
46-
4738
variable "create_role_and_policy" {
4839
description = "Create a new IAM role and policy if true"
4940
type = bool
5041
default = true
5142
}
52-
5343
variable "codebuild_role_arn" {
54-
5544
description = "ARN of the existing codebuild role"
5645
type = string
5746
default = ""
@@ -72,8 +61,6 @@ variable "TF_VERSION" {
7261
}
7362

7463
### CODE BUILD ENVIORNMENT VARIABLES
75-
76-
7764
variable "codebuild_env_vars" {
7865
description = "Environment var for CodeBuild"
7966
type = object({
@@ -85,5 +72,3 @@ variable "codebuild_env_vars" {
8572
EXPORT_PROJECT_NAME = "NAME_OF_PROJECT"
8673
}
8774
}
88-
89-

0 commit comments

Comments
 (0)