-
Notifications
You must be signed in to change notification settings - Fork 111
/
jenkins_image.tf
64 lines (52 loc) · 1.99 KB
/
jenkins_image.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
data "aws_ecr_authorization_token" "token" {}
locals {
ecr_endpoint = split("/", aws_ecr_repository.jenkins_controller.repository_url)[0]
}
resource "aws_ecr_repository" "jenkins_controller" {
name = var.jenkins_ecr_repository_name
image_tag_mutability = "MUTABLE"
image_scanning_configuration {
scan_on_push = true
}
}
data "template_file" jenkins_configuration_def {
template = file("${path.module}/docker/files/jenkins.yaml.tpl")
vars = {
ecs_cluster_fargate = aws_ecs_cluster.jenkins_controller.arn
ecs_cluster_fargate_spot = aws_ecs_cluster.jenkins_agents.arn
cluster_region = local.region
jenkins_cloud_map_name = "controller.${var.name_prefix}"
jenkins_controller_port = var.jenkins_controller_port
jnlp_port = var.jenkins_jnlp_port
agent_security_groups = aws_security_group.jenkins_controller_security_group.id
execution_role_arn = aws_iam_role.ecs_execution_role.arn
subnets = join(",", var.jenkins_controller_subnet_ids)
}
}
resource "null_resource" "render_template" {
triggers = {
src_hash = file("${path.module}/docker/files/jenkins.yaml.tpl")
always_run = timestamp()
}
depends_on = [data.template_file.jenkins_configuration_def]
provisioner "local-exec" {
command = <<EOF
tee ${path.module}/docker/files/jenkins.yaml <<ENDF
${data.template_file.jenkins_configuration_def.rendered}
EOF
}
}
resource "null_resource" "build_docker_image" {
triggers = {
src_hash = file("${path.module}/docker/files/jenkins.yaml.tpl")
always_run = timestamp()
}
depends_on = [null_resource.render_template]
provisioner "local-exec" {
command = <<EOF
docker login -u AWS -p ${data.aws_ecr_authorization_token.token.password} ${local.ecr_endpoint} && \
docker build -t ${aws_ecr_repository.jenkins_controller.repository_url}:latest ${path.module}/docker/ && \
docker push ${aws_ecr_repository.jenkins_controller.repository_url}:latest
EOF
}
}