summaryrefslogtreecommitdiffstats
path: root/jjb/scripts/terraform_s3_docs_ship.sh
blob: 7b2542ae4606a4ab24785a159497fb2632e8020c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/bin/bash

# Copyright (c) 2021 Cisco and/or its affiliates.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

echo "---> terraform_s3_docs_ship.sh"

set -exuo pipefail

cat >"/w/workspace/main.tf" <<'END_OF_TERRAFORM_SCRIPT'
terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "5.18.1"
    }
  }
}

provider "aws" {
  region                      = "us-east-1"
  profile                     = "default"
  s3_use_path_style           = false
  skip_credentials_validation = true
  skip_metadata_api_check     = true
  skip_requesting_account_id  = true
}

locals {
  mime_types = {
    xml   = "application/xml",
    html  = "text/html",
    txt   = "text/plain",
    log   = "text/plain",
    css   = "text/css",
    md    = "text/markdown",
    rst   = "text/x-rst",
    csv   = "text/csv",
    svg   = "image/svg+xml",
    jpg   = "image/jpeg",
    png   = "image/png",
    gif   = "image/gif",
    js    = "application/javascript",
    pdf   = "application/pdf"
    json  = "application/json",
    otf   = "font/otf",
    ttf   = "font/ttf",
    woff  = "font/woff",
    woff2 = "font/woff2"
  }
}

variable "workspace_dir" {
  description = "Workspace base directory"
  type        = string
}

variable "file_match_pattern" {
  description = "File matching pattern"
  type        = string
  default     = "**/*"
}

variable "bucket" {
  description = "S3 bucket name"
  type        = string
}

variable "bucket_path" {
  description = "S3 bucket path to key"
  type        = string
}

resource "aws_s3_bucket_object" "object" {
  for_each = fileset(var.workspace_dir, var.file_match_pattern)

  bucket = var.bucket
  key    = "${var.bucket_path}${each.value}"
  source = "${var.workspace_dir}/${each.value}"

  cache_control = "no-store,max-age=0,s-maxage=0"
  etag          = filemd5("${var.workspace_dir}/${each.value}")
  content_type = lookup(
    local.mime_types,
    regex("\\.(?P<extension>[A-Za-z0-9]+)$", each.value).extension,
    "application/octet-stream"
  )
}
END_OF_TERRAFORM_SCRIPT