terraform-gitlab-drift

command module
v1.5.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 2, 2026 License: Apache-2.0 Imports: 1 Imported by: 0

README ΒΆ

terraform-gitlab-drift

CI Release License

Detect GitLab resources not managed by Terraform and generate Terraform code to bring them under management.

Uses the GitLab Terraform Provider resource definitions.

Features

  • πŸ” Drift Detection: Scan GitLab groups and projects to identify resources not managed by Terraform
  • πŸ“ Code Generation: Automatically generate Terraform code for unmanaged resources
  • πŸ”„ Diff Comparison: Show differences between existing and generated Terraform configurations
  • πŸ“¦ Import Commands: Generate terraform import commands for new resources
  • πŸ”€ Merge Request Creation: Automatically create or update GitLab MRs with generated .tf files
  • 🐳 Docker-ready: Designed for CI/CD pipelines

Quick Start

Local Installation
go install github.com/xMoelletschi/terraform-gitlab-drift@latest
terraform-gitlab-drift scan --group my-group

GitLab CI Usage

Basic Drift Check
drift-check:
  image: ghcr.io/xmoelletschi/terraform-gitlab-drift:latest
  script:
    - terraform-gitlab-drift scan --group $CI_PROJECT_ROOT_NAMESPACE
Auto-create MR on Drift
drift-remediation:
  image: ghcr.io/xmoelletschi/terraform-gitlab-drift:latest
  script:
    - terraform-gitlab-drift scan --group $CI_PROJECT_ROOT_NAMESPACE --create-mr --show-diff=false

When drift is detected, this creates (or updates) a merge request in the current repository with the generated .tf files. The target repo is auto-detected from the git remote; use --target-repo to override.

Configuration

Command-line Flags
Flag Environment Variable Default Description
--gitlab-token GITLAB_TOKEN - GitLab API token (required)
--gitlab-url - https://gitlab.com GitLab instance URL
--group - - Top-level group to scan (required for gitlab.com)
--terraform-dir - . Path to Terraform directory
--overwrite - false Overwrite files in terraform directory
--show-diff - true Show diff between generated and existing files
--skip - - Resource types to skip (comma-separated). Use premium to skip all Premium-tier resources
--create-mr - false Create a merge request with generated Terraform code
--target-repo - (auto-detected) GitLab project path or ID for the MR
--mr-branch - drift/backtrack Branch name for the drift MR
--mr-dest-path - (root) Path within target repo where .tf files go
--verbose, -v - false Enable verbose (debug) logging
--json - false Output logs in JSON format
Directory Structure

The tool generates one .tf file per GitLab namespace, using normalized names (lowercase, / and - replaced with _). Your Terraform directory should follow this structure to get accurate drift detection:

terraform/
β”œβ”€β”€ backend.tf
β”œβ”€β”€ providers.tf
β”œβ”€β”€ my_group.tf             # generated: top-level group + its projects
β”œβ”€β”€ my_group_sub_group.tf   # generated: sub-group + its projects
β”œβ”€β”€ group_membership.tf     # generated: variable with group β†’ user memberships
β”œβ”€β”€ project_membership.tf   # generated: variable with project β†’ shared groups
β”œβ”€β”€ group_labels.tf         # generated: variable with group β†’ labels
β”œβ”€β”€ project_labels.tf       # generated: variable with project β†’ labels
└── ...

Important: The drift check only compares files that match the generated filenames. If you have resources defined in differently named files (e.g. main.tf, projects.tf), they will not be detected and the tool will report those resources as unmanaged.

To fix this, move your resource definitions into the files matching the generated naming convention, or use --overwrite to let the tool manage the file structure for you.

Supported Resources

Contributing

Contributions are welcome! Please:

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Push to the branch (git push origin feature/amazing-feature)
  4. Open a Pull Request

Please make sure to:

  • Add tests for new features
  • Update documentation as needed
  • Ensure CI checks pass

License

This project is licensed under the Apache License 2.0 - see the LICENSE file for details.

Acknowledgments

Built with:


Note: This tool is not affiliated with HashiCorp or GitLab.

Documentation ΒΆ

The Go Gopher

There is no documentation for this package.

Directories ΒΆ

Path Synopsis
internal

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL