Skip to content

Commit

Permalink
feat: Add multiple tags
Browse files Browse the repository at this point in the history
  • Loading branch information
fdmsantos committed Aug 8, 2023
1 parent c49b401 commit 925048f
Show file tree
Hide file tree
Showing 12 changed files with 78 additions and 15 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ module "docker_image" {
- [Simple](https://github.com/redevaerk/terraform-docker-image/tree/main/examples/simple) - This example will build and push Docker image with default settings.
- [Build Image With Context](https://github.com/redevaerk/terraform-docker-image/tree/main/examples/build-image-with-context) - This example will build and push Docker image with a context in different folder.
- [Azure Container Registry](https://github.com/redevaerk/terraform-docker-image/tree/main/examples/build-image-with-context) - This example will build and push Docker image to Azure Container Registry.
- [Multiple Tags](https://github.com/redevaerk/terraform-docker-image/tree/main/examples/multiple-tags) - This example will build and push Docker image with multiple tags.

<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
## Requirements
Expand Down Expand Up @@ -49,6 +50,7 @@ No modules.

| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| <a name="input_add_latest_tag"></a> [add\_latest\_tag](#input\_add\_latest\_tag) | Add latest tag to the Image. If false, it's necessary to add at least one tag on 'variable.tags'. | `bool` | `true` | no |
| <a name="input_context"></a> [context](#input\_context) | Value to specify the build context. Currently, only a PATH context is supported. | `string` | `null` | no |
| <a name="input_dockerfile"></a> [dockerfile](#input\_dockerfile) | A directory containing your Dockerfile | `string` | `"Dockerfile"` | no |
| <a name="input_dynamic_build"></a> [dynamic\_build](#input\_dynamic\_build) | If true, will force the docker\_image resource to be replaced. This can be used to rebuild an image when contents of source code folders change. | `bool` | `true` | no |
Expand All @@ -59,17 +61,19 @@ No modules.
| <a name="input_dynamic_build_source_dir"></a> [dynamic\_build\_source\_dir](#input\_dynamic\_build\_source\_dir) | A directory containing your source code. This variable will be used in dynamic build when var.dynamic\_build\_attach\_source is true. | `string` | `"src"` | no |
| <a name="input_force_remove"></a> [force\_remove](#input\_force\_remove) | If true, then the image is removed forcibly when the resource is destroyed. | `bool` | `false` | no |
| <a name="input_keep_locally"></a> [keep\_locally](#input\_keep\_locally) | If true, then the Docker image won't be deleted on destroy operation. If this is false, it will delete the image from the docker local storage on destroy operation. | `bool` | `false` | no |
| <a name="input_keep_remotely"></a> [keep\_remotely](#input\_keep\_remotely) | If true, then the Docker image won't be deleted on destroy operation. If this is false, it will delete the image from the docker registry on destroy operation. | `bool` | `false` | no |
| <a name="input_labels"></a> [labels](#input\_labels) | A map of labels to add to the Image | `map(string)` | `{}` | no |
| <a name="input_name"></a> [name](#input\_name) | A unique name for your Image | `string` | n/a | yes |
| <a name="input_push"></a> [push](#input\_push) | If true, then the Image will be pushed to the registry | `bool` | `true` | no |
| <a name="input_registry"></a> [registry](#input\_registry) | Registry Name to push the Image | `string` | `"registry-1.docker.io"` | no |
| <a name="input_tag"></a> [tag](#input\_tag) | A tag for your Image | `string` | `"latest"` | no |
| <a name="input_tags"></a> [tags](#input\_tags) | Optionally a tags to add to the Image | `list(string)` | `[]` | no |

## Outputs

| Name | Description |
|------|-------------|
| <a name="output_image"></a> [image](#output\_image) | Name of the created docker image |
| <a name="output_images"></a> [images](#output\_images) | List of all created docker images |
| <a name="output_map_images"></a> [map\_images](#output\_map\_images) | Map per tag corresponding docker images |
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->

## License
Expand Down
1 change: 1 addition & 0 deletions examples/multiple-tags/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
src/ignorefile.txt
2 changes: 2 additions & 0 deletions examples/multiple-tags/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
FROM ubuntu:22.04
COPY src /
14 changes: 14 additions & 0 deletions examples/multiple-tags/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
provider "docker" {
host = "unix:///var/run/docker.sock"
registry_auth {
address = "registry-1.docker.io"
config_file = pathexpand("~/.docker/config.json")
}
}

module "docker_image" {
source = "../../"
name = var.name
tags = ["v1", "beta"]
keep_remotely = false
}
9 changes: 9 additions & 0 deletions examples/multiple-tags/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
output "images" {
description = "List of all created docker images"
value = module.docker_image.images
}

output "map_images" {
description = "Map per tag corresponding docker images"
value = module.docker_image.map_images
}
1 change: 1 addition & 0 deletions examples/multiple-tags/src/demofile.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello World
Empty file.
4 changes: 4 additions & 0 deletions examples/multiple-tags/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
variable "name" {
description = "A unique name for your Image"
type = string
}
10 changes: 10 additions & 0 deletions examples/multiple-tags/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
terraform {
required_version = ">= 1.0"

required_providers {
docker = {
source = "kreuzwerker/docker"
version = ">=3.0.0"
}
}
}
13 changes: 7 additions & 6 deletions main.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
locals {
image_name = "${var.registry}/${var.name}:${var.tag}"
tags = var.add_latest_tag && !contains(var.tags, "latest") ? compact(concat(["latest"], var.tags)) : compact(var.tags)
image_name = "${var.registry}/${var.name}"
context = coalesce(var.context, path.cwd)
build_triggers_source = var.dynamic_build && var.dynamic_build_attach_source ? {
source_dir = sha1(join("", [for f in fileset(local.context, "${var.dynamic_build_source_dir}/*") : filesha1("${local.context}/${f}")]))
Expand All @@ -20,20 +21,20 @@ locals {
}

resource "docker_registry_image" "this" {
count = var.push ? 1 : 0
name = docker_image.this.name
keep_remotely = true
for_each = var.push ? toset(docker_image.this.build[*].tag[0]) : toset([])
name = each.value
keep_remotely = var.keep_remotely
triggers = local.build_triggers
}

resource "docker_image" "this" {
name = local.image_name
name = "${local.image_name}:${local.tags[0]}"
force_remove = var.force_remove
keep_locally = var.keep_locally
build {
context = local.context
dockerfile = var.dockerfile
tag = [local.image_name]
tag = [for tag in local.tags : "${local.image_name}:${tag}"]
labels = var.labels
}
triggers = local.build_triggers
Expand Down
11 changes: 8 additions & 3 deletions outputs.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
output "image" {
description = "Name of the created docker image"
value = local.image_name
output "images" {
description = "List of all created docker images"
value = docker_image.this.build[*].tag[0]
}

output "map_images" {
description = "Map per tag corresponding docker images"
value = { for tag in local.tags : tag => "${local.image_name}:${tag}" }
}
20 changes: 16 additions & 4 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,16 @@ variable "name" {
type = string
}

variable "tag" {
description = "A tag for your Image"
type = string
default = "latest"
variable "add_latest_tag" {
description = "Add latest tag to the Image. If false, it's necessary to add at least one tag on 'variable.tags'."
type = bool
default = true
}

variable "tags" {
description = "Optionally a tags to add to the Image"
type = list(string)
default = []
}

variable "keep_locally" {
Expand All @@ -24,6 +30,12 @@ variable "keep_locally" {
default = false
}

variable "keep_remotely" {
description = "If true, then the Docker image won't be deleted on destroy operation. If this is false, it will delete the image from the docker registry on destroy operation."
type = bool
default = false
}

variable "force_remove" {
description = " If true, then the image is removed forcibly when the resource is destroyed."
type = bool
Expand Down

0 comments on commit 925048f

Please sign in to comment.