Skip to content

Commit de6ab13

Browse files
committed
Create Codebuild module for hugo
1 parent 005106a commit de6ab13

9 files changed

+256
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Local .terraform directories
22
**/.terraform/*
33

4+
/.idea
5+
46
# .tfstate files
57
*.tfstate
68
*.tfstate.*

buildspec.yml

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
version: 0.2
2+
3+
env:
4+
variables:
5+
HUGO_VERSION: "0.105.0"
6+
7+
phases:
8+
install:
9+
commands:
10+
- echo Entered the install phase...
11+
- |
12+
[[ -e hugo-$HUGO_VERSION ]] || wget https://github.com/gohugoio/hugo/releases/download/v${HUGO_VERSION}/hugo_extended_${HUGO_VERSION}_linux-arm64.tar.gz
13+
- |
14+
[[ -e hugo-$HUGO_VERSION ]] || tar xaf hugo_extended_${HUGO_VERSION}_linux-arm64.tar.gz
15+
- |
16+
[[ -e hugo-$HUGO_VERSION ]] || mv hugo hugo-$HUGO_VERSION
17+
finally:
18+
- echo This always runs even if the update or install command fails
19+
pre_build:
20+
commands:
21+
- echo Entered the pre_build phase...
22+
finally:
23+
- echo This always runs even if the login command fails
24+
build:
25+
commands:
26+
- echo Entered the build phase...
27+
- echo Build started on `date`
28+
- ./hugo-$HUGO_VERSION --cleanDestinationDir
29+
finally:
30+
- echo This always runs even if the install command fails
31+
post_build:
32+
commands:
33+
- echo Entered the post_build phase...
34+
- ./hugo-$HUGO_VERSION deploy
35+
- echo Build completed on `date`
36+
37+
cache:
38+
paths:
39+
- hugo-*
40+
- public/**/*
41+
- resources/_gen/**/*

codebuild.tf

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
resource "aws_codebuild_project" "self" {
2+
name = var.name
3+
build_timeout = "5"
4+
service_role = aws_iam_role.codebuild.arn
5+
6+
artifacts {
7+
type = "NO_ARTIFACTS"
8+
}
9+
10+
cache {
11+
type = "S3"
12+
location = aws_s3_bucket.cache.bucket
13+
}
14+
15+
environment {
16+
compute_type = "BUILD_GENERAL1_SMALL"
17+
image = "aws/codebuild/amazonlinux2-aarch64-standard:2.0"
18+
type = "ARM_CONTAINER"
19+
image_pull_credentials_type = "CODEBUILD"
20+
}
21+
22+
concurrent_build_limit = 1
23+
24+
/*logs_config {
25+
cloudwatch_logs {
26+
group_name = "log-group"
27+
stream_name = "log-stream"
28+
}
29+
}*/
30+
31+
source {
32+
type = "GITHUB"
33+
location = "https://github.com/tabletcorry/web_hugo.git"
34+
git_clone_depth = 1
35+
36+
report_build_status = true
37+
38+
git_submodules_config {
39+
fetch_submodules = true
40+
}
41+
42+
buildspec = file("${path.module}/buildspec.yml")
43+
}
44+
45+
source_version = "main"
46+
}
47+
48+
resource "aws_codebuild_webhook" "self" {
49+
project_name = aws_codebuild_project.self.name
50+
build_type = "BUILD"
51+
filter_group {
52+
filter {
53+
type = "EVENT"
54+
pattern = "PUSH"
55+
}
56+
57+
filter {
58+
type = "HEAD_REF"
59+
pattern = "main"
60+
}
61+
}
62+
}

iam.tf

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
resource "aws_iam_role" "codebuild" {
2+
name_prefix = var.name
3+
4+
assume_role_policy = <<EOF
5+
{
6+
"Version": "2012-10-17",
7+
"Statement": [
8+
{
9+
"Effect": "Allow",
10+
"Principal": {
11+
"Service": "codebuild.amazonaws.com"
12+
},
13+
"Action": "sts:AssumeRole"
14+
}
15+
]
16+
}
17+
EOF
18+
}
19+
20+
data "aws_s3_bucket" "target" {
21+
bucket = var.s3_deploy_target
22+
}
23+
24+
data "aws_cloudfront_distribution" "target" {
25+
id = var.cloudfront_deploy_target
26+
}
27+
28+
resource "aws_iam_role_policy" "codebuild" {
29+
role = aws_iam_role.codebuild.name
30+
31+
policy = <<POLICY
32+
{
33+
"Version": "2012-10-17",
34+
"Statement": [
35+
{
36+
"Effect": "Allow",
37+
"Resource": [
38+
"*"
39+
],
40+
"Action": [
41+
"logs:CreateLogGroup",
42+
"logs:CreateLogStream",
43+
"logs:PutLogEvents"
44+
]
45+
},
46+
{
47+
"Effect": "Allow",
48+
"Action": [
49+
"s3:GetObject",
50+
"s3:PutObject",
51+
"s3:List*",
52+
"s3:GetBucketAcl",
53+
"s3:GetBucketLocation",
54+
"s3:GetObjectVersion"
55+
],
56+
"Resource": [
57+
"${aws_s3_bucket.cache.arn}",
58+
"${aws_s3_bucket.cache.arn}/*"
59+
]
60+
},
61+
{
62+
"Effect": "Allow",
63+
"Action": [
64+
"s3:GetObject",
65+
"s3:PutObject",
66+
"s3:List*",
67+
"s3:GetBucketAcl",
68+
"s3:GetBucketLocation",
69+
"s3:GetObjectVersion"
70+
],
71+
"Resource": [
72+
"${data.aws_s3_bucket.target.arn}",
73+
"${data.aws_s3_bucket.target.arn}/*"
74+
]
75+
},
76+
{
77+
"Effect": "Allow",
78+
"Action": [
79+
"cloudfront:GetDistribution",
80+
"cloudfront:CreateInvalidation"
81+
],
82+
"Resource": [
83+
"${data.aws_cloudfront_distribution.target.arn}"
84+
]
85+
}
86+
]
87+
}
88+
POLICY
89+
}

inputs.tf

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
variable "name" {
2+
type = string
3+
}
4+
5+
variable "tags" {
6+
type = map(string)
7+
default = {}
8+
}
9+
10+
variable "s3_deploy_target" {
11+
type = string
12+
}
13+
14+
variable "cloudfront_deploy_target" {
15+
type = string
16+
}

locals.tf

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
locals {
2+
module_tags = {
3+
module = "tf-aws-codebuild-hugo"
4+
module_var_name = var.name
5+
}
6+
tags = merge(local.module_tags, var.tags)
7+
}

provider.tf

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
provider "aws" {
2+
alias = "us-east-1"
3+
region = "us-east-1"
4+
5+
default_tags {
6+
tags = local.tags
7+
}
8+
}
9+
10+
provider "aws" {
11+
default_tags {
12+
tags = local.tags
13+
}
14+
}

s3.tf

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
resource "aws_s3_bucket" "cache" {
2+
bucket_prefix = var.name
3+
}
4+
5+
resource "aws_s3_bucket_acl" "cache" {
6+
bucket = aws_s3_bucket.cache.id
7+
acl = "private"
8+
}
9+
10+
resource "aws_s3_bucket_public_access_block" "cache" {
11+
bucket = aws_s3_bucket.cache.id
12+
13+
block_public_acls = true
14+
block_public_policy = true
15+
}

versions.tf

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
terraform {
2+
required_providers {
3+
aws = {
4+
source = "hashicorp/aws"
5+
version = ">= 4.36.1"
6+
}
7+
}
8+
9+
required_version = ">= 0.15"
10+
}

0 commit comments

Comments
 (0)