-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
executable file
·102 lines (83 loc) · 2.94 KB
/
main.tf
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
100
101
102
locals {
docker_build_context = var.docker_build_context
docker_build_target = var.docker_build_target
source_path = local.docker_build_context
artifact_id = try(random_string.this[0].id, "unknown")
artifact_dst_dir = var.artifact_dst_directory == "" ? "${path.module}/dist" : var.artifact_dst_directory
artifact_dst_path = abspath("${local.artifact_dst_dir}/${module.artifact_label.id}-${local.artifact_id}${local.artifact_type_dst_suffix_map[var.artifact_src_type]}")
artifact_src_path = var.artifact_src_path
artifact_type_dst_suffix_map = {
zip = ".zip"
directory = "/"
}
os_script_map = {
windows = <<-EOT
$ARTIFACT_DST_DIR=[System.IO.Path]::GetDirectoryName('$ARTIFACT_DST_PATH')
New-Item -ItemType Directory -Force -Path $ARTIFACT_DST_DIR
$ARTIFACT_CONTAINER_ID=$(docker create $ARTIFACT_IMAGE echo "ping")
docker cp $ARTIFACT_CONTAINER_ID:$ARTIFACT_SRC_PATH $ARTIFACT_DST_PATH
docker rm -fv $ARTIFACT_CONTAINER_ID
EOT
unix = <<-EOT
ARTIFACT_DST_DIR=$(dirname $ARTIFACT_DST_PATH)
mkdir -p $ARTIFACT_DST_DIR
ARTIFACT_CONTAINER_ID=$(docker create $ARTIFACT_IMAGE echo "ping")
docker cp $ARTIFACT_CONTAINER_ID:$ARTIFACT_SRC_PATH $ARTIFACT_DST_PATH
docker rm -fv $ARTIFACT_CONTAINER_ID
EOT
}
}
module "artifact_label" {
source = "cloudposse/label/null"
version = "0.25.0"
name = coalesce(module.this.name, var.name, "artifact")
label_order = ["name", "attributes"]
context = module.this.context
}
resource "docker_image" "this" {
count = module.artifact_label.enabled ? 1 : 0
name = module.artifact_label.id
build {
context = local.docker_build_context
target = local.docker_build_target
tag = ["${module.artifact_label.id}:${local.artifact_id}"]
build_args = var.docker_build_args
dockerfile = "Dockerfile"
force_remove = false
label = {}
no_cache = false
remove = false
}
}
data "archive_file" "this" {
count = module.artifact_label.enabled ? 1 : 0
type = "zip"
output_path = "${path.module}/dist/source-${module.artifact_label.id}.zip"
source_dir = local.source_path
}
resource "random_string" "this" {
count = module.artifact_label.enabled ? 1 : 0
length = 6
special = false
upper = false
keepers = {
artifact_sha = data.archive_file.this[0].output_sha
docker_build_args = jsonencode(var.docker_build_args)
force_rebuild_id = var.force_rebuild_id
}
}
resource "null_resource" "this" {
count = module.artifact_label.enabled ? 1 : 0
triggers = {
artifact_image = docker_image.this[0].image_id
artifact_path = local.artifact_dst_path
}
provisioner "local-exec" {
command = local.os_script_map[var.os_compatibility]
environment = {
ARTIFACT_IMAGE = docker_image.this[0].image_id
ARTIFACT_SRC_PATH = local.artifact_src_path
ARTIFACT_DST_PATH = local.artifact_dst_path
}
}
}