Skip to content

Commit

Permalink
Refactoring out the shell module, should be able to pass in these val…
Browse files Browse the repository at this point in the history
…ues from CI (#5)
  • Loading branch information
rickihastings authored Jul 10, 2020
1 parent 0b8a7a5 commit a9c0b5e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 18 deletions.
19 changes: 4 additions & 15 deletions docker_builder/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,17 @@ terraform {
}
}

module "git_branch" {
source = "matti/resource/shell"
command = "git rev-parse --abbrev-ref HEAD"
}

module "git_sha" {
source = "matti/resource/shell"
command = "git rev-parse HEAD"
}

locals {
args = join(" ", var.args)
git_branch = lower(replace(module.git_branch.stdout, "/", "-"))
git_sha = module.git_sha.stdout
git_branch = lower(replace(var.git_branch, "/", "-"))

tag = "${var.repository_url}:${local.git_branch}"
extra_tags = local.git_branch == "master" ? ["${var.repository_url}:${local.git_sha}", "${var.repository_url}:latest"] : ["${var.repository_url}:${local.git_sha}"]
extra_tags = local.git_branch == "master" ? ["${var.repository_url}:${var.git_sha}", "${var.repository_url}:latest"] : ["${var.repository_url}:${var.git_sha}"]
}

resource "null_resource" "build_image" {
triggers = {
image_tag = module.git_sha.stdout
image_tag = var.git_sha
}

provisioner "local-exec" {
Expand All @@ -42,7 +31,7 @@ resource "null_resource" "build_image" {
--cache-from ${local.tag} \
--tag ${local.tag} \
--build-arg CI="true" \
${local.args} ${var.dockerfile};
${local.args} ${var.path};
docker push ${local.tag};
%{for tag in local.extra_tags}
Expand Down
2 changes: 1 addition & 1 deletion docker_builder/outputs.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
output "image" {
value = "${var.repository_url}:${module.git_sha.stdout}"
value = "${var.repository_url}:${var.git_sha}"
description = "The full path to the docker image"
}

Expand Down
14 changes: 12 additions & 2 deletions docker_builder/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ variable "region" {
description = "The AWS region to use"
}

variable "dockerfile" {
variable "path" {
type = string
description = "Location of Dockerfile"
description = "Path where the dockerfile exists"
}

variable "args" {
Expand All @@ -19,3 +19,13 @@ variable "args" {
default = []
}

variable "git_branch" {
type = string
description = "The git branch"
}

variable "git_sha" {
type = string
description = "The SHA of the git commit"
}

0 comments on commit a9c0b5e

Please sign in to comment.