I’m deploying Terraform modules in my CI into the Terraform registry in my project.
I need to have “rolling tags” on my Terraform module, that is, I want every branch to publish their version of the module. However, this is difficult for two reasons:
-
Terraform modules must conform to semver versions. This means that I can’t use the branch name as a version as is common practice with e.g. Docker image tags. However, I have a workaround where I just publish all such “rolling” versions as
0.0.0-my-branch-name
. -
The Gitlab Terraform registry does not allow you to re-upload the same module/version combo. Instead I have to delete the module in that version from the registry and then re-upload a new version. This is easy to do with the UI but in order to do this in my CI loop I will have to use the Gitlab API. And that’s where the problem lies.
I’ve figured out how to upload a module without problems, I can see it appear in the UI. Below you can see the curl command that I use to do that:
❯ curl --header "PRIVATE-TOKEN: $GITLAB_TOKEN" --upload-file file.tgz "https://gitlab.mydomain.com/api/v4/projects/123/packages/terraform/modules/my-module/my-system/0.0.1/file"
{"message":"201 Created"}%
As you can see, this works fine.
But now I’m trying to delete that same module through the API and I’m having no luck in doing that:
❯ curl --header "PRIVATE-TOKEN: $GITLAB_TOKEN" --request DELETE "https://gitlab.mydomain.com/api/v4/projects/123/packages/terraform/modules/my-module/my-system/0.0.1/file"
{"error":"404 Not Found"}%
❯ curl --header "PRIVATE-TOKEN: $GITLAB_TOKEN" --request DELETE "https://gitlab.mydomain.com/api/v4/projects/123/packages/terraform/modules/my-module/my-system/0.0.1/"
{"error":"404 Not Found"}%
❯ curl --header "PRIVATE-TOKEN: $GITLAB_TOKEN" --request DELETE "https://gitlab.mydomain.com/api/v4/projects/123/packages/terraform/modules/my-module/my-system/0.0.1"
{"error":"404 Not Found"}%
According to this it should be possible to delete packages from the Infrastructure (Terraform) Registry via the API, but how? What URL do I use to refer to the package I want to delete?
The weird thing is that, as I said, I can see the package (Terraform module) in the Gitlab UI but if I query the packages via the API I get:
❯ curl --header "PRIVATE-TOKEN: $GITLAB_TOKEN" "https://gitlab.mydomain.com/api/v4/projects/123/packages"
[]%
So where in the world are my terraform packages???