Skip to content

Commit b773508

Browse files
author
Samuel Crespo
committed
Add variable to manage repo lifecycle within TF
The new variable `manage_repo` will manage Helm repository lifecycle automatically, adding, updating and deleting it from Helm repo list. Useful for use on CI/CD pipelines.
1 parent 4f838b5 commit b773508

File tree

4 files changed

+59
-0
lines changed

4 files changed

+59
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,18 +88,21 @@ module jenkins {
8888
|------|---------|
8989
| terraform | >= 0.13 |
9090
| helm | >= 2.0 |
91+
| null | >= 3.0 |
9192

9293
## Providers
9394

9495
| Name | Version |
9596
|------|---------|
9697
| helm | >= 2.0 |
98+
| null | >= 3.0 |
9799

98100
## Inputs
99101

100102
| Name | Description | Type | Default | Required |
101103
|------|-------------|------|---------|:--------:|
102104
| app | an application to deploy | `map(any)` | n/a | yes |
105+
| manage\_repo | Whether Helm repo should be added, updated and removed during Terraform execution | `bool` | `false` | no |
103106
| namespace | namespace where to deploy an application | `string` | n/a | yes |
104107
| repository | Helm repository | `string` | n/a | yes |
105108
| set | Value block with custom STRING values to be merged with the values yaml. | <pre>list(object({<br> name = string<br> value = string<br> }))</pre> | `null` | no |

main.tf

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,31 @@
1+
resource "null_resource" "repo_add" {
2+
count = var.manage_repo ? 1 : 0
3+
4+
triggers = {
5+
always_run = "${timestamp()}"
6+
}
7+
8+
provisioner "local-exec" {
9+
command = "helm repo add ${var.app["chart"]} ${var.repository}"
10+
}
11+
}
12+
13+
resource "null_resource" "repo_update" {
14+
count = var.manage_repo ? 1 : 0
15+
16+
triggers = {
17+
always_run = "${timestamp()}"
18+
}
19+
20+
provisioner "local-exec" {
21+
command = "helm repo update ${var.app["chart"]}"
22+
}
23+
24+
depends_on = [
25+
resource.null_resource.repo_add[0]
26+
]
27+
}
28+
129
resource "helm_release" "this" {
230
count = var.app["deploy"] ? 1 : 0
331
namespace = var.namespace
@@ -44,4 +72,25 @@ resource "helm_release" "this" {
4472
value = item.value.value
4573
}
4674
}
75+
76+
depends_on = [
77+
resource.null_resource.repo_update
78+
]
79+
}
80+
81+
resource "null_resource" "repo_delete" {
82+
count = var.manage_repo ? 1 : 0
83+
84+
triggers = {
85+
always_run = "${timestamp()}"
86+
}
87+
88+
provisioner "local-exec" {
89+
command = "helm repo remove ${var.app["chart"]}"
90+
}
91+
92+
depends_on = [
93+
resource.null_resource.repo_add[0],
94+
resource.helm_release.this
95+
]
4796
}

variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,9 @@ variable "repository" {
3636
description = "Helm repository"
3737
type = string
3838
}
39+
40+
variable "manage_repo" {
41+
description = "Whether Helm repo should be added, updated and removed during Terraform execution"
42+
default = false
43+
type = bool
44+
}

versions.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ terraform {
33

44
required_providers {
55
helm = ">= 2.0"
6+
null = ">= 3.0"
67
}
78
}

0 commit comments

Comments
 (0)