Problem to solve
I got this error, Error refreshing state: Unsupported state file format: This state file is encrypted and can not be read without an encryption configuration, when following the documents from GitLab and OpenTofu (https://opentofu.org/docs/language/state/encryption/).
I tried to deploy locally and all .tf files work just fine.
All tfvars are supplied through GitLab CI/CD Variables.
I only observe this error message in test and validate stages, for now.
Other stages, like fmt and graph completed without any issue.
Steps to reproduce
- Enable encryption and backend
http. - Trigger GitLab CI first to initialize environment and Terraform state.
- Trigger GitLab CI again to deploy changes. <= ERROR
Configuration
.gitlab-ci.yml:
include:
- component: gitlab.com/components/opentofu/job-templates@main
fmt:
stage: validate
extends: [.opentofu:fmt]
validate:
stage: validate
environment: stg
extends: [.opentofu:validate]
graph:
stage: validate
environment: stg
extends: [.opentofu:graph]
test:
stage: test
environment: stg
extends: [.opentofu:test]
backend block:
terraform {
backend "http" {}
}
encryption block:
terraform {
encryption {
key_provider "pbkdf2" "default" {
passphrase = var.passphrase
}
method "aes_gcm" "default" {
keys = key_provider.pbkdf2.default
}
state {
method = method.aes_gcm.default
enforced = true
}
plan {
method = method.aes_gcm.default
enforced = true
}
}
}
Versions
GitLab.comSaaS:GitLab Enterprise Edition 17.9.0-pre dcf7ed9f83d- GitLab OpenTofu CI/CD component:
0.50.0 - OpenTofu:
v1.9.0
Helpful resources
After some search, I found backend is explicit disabled in test and validate. Ref: gitlab-tofu.sh L416-425
And, I tried to clear cache and it works, but only works for the next run.
If there is no cache, both test and validate work just fine.
However, as soon as .terraform/terraform.tfstate exists, they both fail.
I also did a reset by:
- Clear runner caches.
- Stop and delete all environments.
- Remove all Terraform states.
Which didn’t help at all.