Reputation: 591
I have a gitlab pipeline that runs some terraform, so say I have a pipeline that declares
Plan (Example):
variables:
TF_VAR_myenvvariable: /home/$example
So now inside my Terraform Project under providers.tf
I have
provider "docker" {
...
registry_auth {
address = "example.com"
config_file = "${TF_VAR_myenvvariable}/docker.json"
}
}
How do I get this to work? It's not working. I can't use the var.myenvvariable because you can't use variables in the providers block.
Upvotes: 0
Views: 67
Reputation: 74574
Environment variables named with the prefix TF_VAR_
are one way to assign values to root module input variables, but if you use that mechanism then inside your module you would obtain that value by referring to the Terraform variable that the value was assigned to, rather than directly to the environment variable.
For example, if you write a root module like this:
variable "docker_config_dir" {
type = string
}
provider "docker" {
...
registry_auth {
address = "example.com"
config_file = "${var.docker_config_dir}/docker.json"
}
}
...then you can use the environment variable TF_VAR_docker_config_dir
to assign a value to that input variable, which will then decide what var.docker_config_dir
evaluates to.
If this "docker" provider is kreuzwerker/docker
then I notice that it offers a different method that you can use instead if you prefer.
The provider documentation says that if you omit the config_file
argument from your registry_auth
block altogether then the provider will default to ~/.docker/config.json
, or to whatever is in the DOCKER_CONFIG
environment variable.
Therefore you could set the DOCKER_CONFIG
environment variable directly as a way to specify the location of your docker.json
file, and then you would not need to define an input variable or set the config_file
argument in your Terraform configuration at all.
DOCKER_CONFIG
is one of the standard environment variables defined by Docker itself, and so this approach has the advantage that you can set the environment variable just once and then it will work for both this Terraform provider and for the docker
CLI, if you need to run them both on your computer.
Upvotes: 1
Reputation: 18094
There are at least 2 docker providers in the Terraform registry that seem compatible to your provider configuration:
You haven't specified which one you're using. Given these are very similar, they probably work exactly the same way.
Many Terraform providers allow you to omit properties and use environment variables instead. For example, when using calxus/docker
provider you can set the DOCKER_HOST
instead of setting the config_file
property.
So you can configure your gitlab pipeline as follows:
Plan (Example):
variables:
DOCKER_HOST: /home/$example
Provider configuration:
provider "docker" {
# ...
registry_auth {
address = "example.com"
}
}
Or, as an alternative, use username and password:
Plan (Example):
variables:
DOCKER_REGISTRY_USER: myuser
DOCKER_REGISTRY_PASS: mypassword
Provider configuration:
provider "docker" {
# ...
registry_auth {
address = "example.com"
}
}
Upvotes: 1