Skip to content

Commit 52b01ec

Browse files
authored
Initial (#2)
1 parent cdf0b1a commit 52b01ec

30 files changed

+2033
-23
lines changed

.dockerignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.git/
2+
.gitignore
3+
.github/
4+
.releaserc.json
5+
Dockerfile
6+
*.md
7+
*_test.go
8+
*.test
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
---
2+
name: Bug Report
3+
about: Report a bug or unexpected behavior
4+
title: '[BUG] '
5+
labels: bug
6+
assignees: ''
7+
---
8+
9+
## Description
10+
11+
<!-- Describe what the bug is -->
12+
13+
## Steps to Reproduce
14+
15+
<!-- Provide steps to reproduce the issue -->
16+
17+
```bash
18+
terraform-gitlab-drift scan --group my-group
19+
```
20+
21+
## Expected vs Actual Behavior
22+
23+
<!-- What should happen vs what actually happens -->
24+
25+
## Error Output
26+
27+
```
28+
<!-- Paste error logs here -->
29+
```
30+
31+
## Environment
32+
33+
- **OS**:
34+
- **Version**: <!-- terraform-gitlab-drift version -->
35+
- **GitLab**: <!-- gitlab.com or self-hosted version -->
36+
- **Installation**: <!-- binary/Docker/go install -->
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
name: Feature Request
3+
about: Suggest a new feature or enhancement
4+
title: '[FEATURE] '
5+
labels: enhancement
6+
assignees: ''
7+
---
8+
9+
## Description
10+
11+
<!-- What feature would you like to see? -->
12+
13+
## Problem
14+
15+
<!-- What problem does this solve? Why is it needed? -->
16+
17+
## Proposed Solution
18+
19+
<!-- How should this work? -->
20+
21+
```bash
22+
# Example usage
23+
terraform-gitlab-drift scan --new-flag
24+
```
25+
26+
## Alternatives
27+
28+
<!-- Any alternative solutions you've considered? -->
29+
30+
## Additional Context
31+
32+
<!-- Links, examples, or other context -->

.github/workflows/ci.yml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches: [master]
6+
pull_request:
7+
branches: [master]
8+
9+
jobs:
10+
test:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@v4
14+
- uses: actions/setup-go@v5
15+
with:
16+
go-version: '1.25'
17+
18+
- name: Run tests
19+
run: go test -v -race -coverprofile=coverage.out ./...
20+
21+
- name: Upload coverage
22+
uses: codecov/codecov-action@v4
23+
with:
24+
file: ./coverage.out
25+
26+
lint:
27+
runs-on: ubuntu-latest
28+
steps:
29+
- uses: actions/checkout@v4
30+
- uses: actions/setup-go@v5
31+
with:
32+
go-version: '1.25'
33+
34+
- name: golangci-lint
35+
uses: golangci/golangci-lint-action@v9
36+
with:
37+
version: latest
38+
39+
build:
40+
runs-on: ubuntu-latest
41+
steps:
42+
- uses: actions/checkout@v4
43+
- uses: actions/setup-go@v5
44+
with:
45+
go-version: '1.25'
46+
47+
- name: Build binary
48+
run: go build -v -o bin/terraform-gitlab-drift .

.github/workflows/release.yml

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
name: Release
2+
3+
on:
4+
push:
5+
branches: [master]
6+
7+
permissions:
8+
contents: write
9+
packages: write
10+
11+
jobs:
12+
release:
13+
runs-on: ubuntu-latest
14+
steps:
15+
- uses: actions/checkout@v4
16+
with:
17+
fetch-depth: 0
18+
19+
- uses: actions/setup-go@v5
20+
with:
21+
go-version: '1.25'
22+
23+
- name: Semantic Release
24+
uses: cycjimmy/semantic-release-action@v4
25+
with:
26+
extra_plugins: |
27+
@semantic-release/git
28+
@semantic-release/changelog
29+
env:
30+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
31+
32+
- name: Set up Docker Buildx
33+
uses: docker/setup-buildx-action@v3
34+
35+
- name: Login to GHCR
36+
uses: docker/login-action@v3
37+
with:
38+
registry: ghcr.io
39+
username: ${{ github.actor }}
40+
password: ${{ secrets.GITHUB_TOKEN }}
41+
42+
- name: Docker meta
43+
id: meta
44+
uses: docker/metadata-action@v5
45+
with:
46+
images: ghcr.io/${{ github.repository }}
47+
tags: |
48+
type=semver,pattern={{version}}
49+
type=raw,value=latest
50+
51+
- name: Build and push
52+
uses: docker/build-push-action@v6
53+
with:
54+
context: .
55+
push: true
56+
tags: ${{ steps.meta.outputs.tags }}
57+
labels: ${{ steps.meta.outputs.labels }}

.gitignore

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,14 @@
1-
# If you prefer the allow list template instead of the deny list, see community template:
2-
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
3-
#
4-
# Binaries for programs and plugins
5-
*.exe
6-
*.exe~
7-
*.dll
8-
*.so
9-
*.dylib
1+
bin/
2+
.idea/
3+
.envrc
4+
tmp/
105

11-
# Test binary, built with `go test -c`
126
*.test
137

14-
# Code coverage profiles and other test artifacts
158
*.out
169
coverage.*
1710
*.coverprofile
1811
profile.cov
1912

20-
# Dependency directories (remove the comment below to include it)
21-
# vendor/
22-
23-
# Go workspace file
2413
go.work
2514
go.work.sum
26-
27-
# env file
28-
.env
29-
30-
# Editor/IDE
31-
# .idea/
32-
# .vscode/

.releaserc.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"branches": ["master"],
3+
"plugins": [
4+
"@semantic-release/commit-analyzer",
5+
"@semantic-release/release-notes-generator",
6+
"@semantic-release/changelog",
7+
"@semantic-release/github",
8+
[
9+
"@semantic-release/git",
10+
{
11+
"assets": ["CHANGELOG.md"],
12+
"message": "chore(release): ${nextRelease.version} [skip ci]\n\n${nextRelease.notes}"
13+
}
14+
]
15+
]
16+
}

Dockerfile

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
FROM golang:1.25-alpine AS builder
2+
3+
WORKDIR /build
4+
5+
COPY go.mod go.sum ./
6+
RUN go mod download
7+
8+
COPY . .
9+
RUN CGO_ENABLED=0 GOOS=linux go build -ldflags="-w -s" -o terraform-gitlab-drift .
10+
11+
FROM alpine:3.23.3
12+
13+
RUN apk --no-cache add ca-certificates
14+
15+
COPY --from=builder /build/terraform-gitlab-drift /usr/local/bin/terraform-gitlab-drift
16+
17+
WORKDIR /workspace

Makefile

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
.PHONY: build test lint fmt deps gotestsum test-update
2+
3+
build:
4+
go build -race -o bin/terraform-gitlab-drift .
5+
6+
test:
7+
go test -v -race -count=1 ./...
8+
9+
lint:
10+
golangci-lint run -v
11+
12+
fmt:
13+
go fmt ./...
14+
15+
deps:
16+
go mod download
17+
go mod tidy
18+
go mod verify
19+
20+
gotestsum:
21+
gotestsum --watch -- --count=1 --timeout=5s
22+
23+
test-update:
24+
UPDATE_GOLDEN=1 go test -v -count=1 ./...

README.md

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,88 @@
11
# terraform-gitlab-drift
2-
CLI tool to find GitLab resources missing from your Terraform code
2+
3+
[![CI](https://github.com/xMoelletschi/terraform-gitlab-drift/actions/workflows/ci.yml/badge.svg)](https://github.com/xMoelletschi/terraform-gitlab-drift/actions/workflows/ci.yml)
4+
[![Release](https://img.shields.io/github/v/release/xMoelletschi/terraform-gitlab-drift)](https://github.com/xMoelletschi/terraform-gitlab-drift/releases)
5+
[![License](https://img.shields.io/badge/license-Apache%202.0-blue.svg)](LICENSE)
6+
7+
Detect GitLab resources not managed by Terraform and generate Terraform code to bring them under management.
8+
9+
Uses the [GitLab Terraform Provider](https://registry.terraform.io/providers/gitlabhq/gitlab/latest/docs) resource definitions.
10+
11+
## Features
12+
13+
- 🔍 **Drift Detection**: Scan GitLab groups and projects to identify resources not managed by Terraform
14+
- 📝 **Code Generation**: Automatically generate Terraform code for unmanaged resources
15+
- 🔄 **Diff Comparison**: Show differences between existing and generated Terraform configurations
16+
- 🐳 **Docker-ready**: Designed for CI/CD pipeline
17+
18+
## Quick Start
19+
20+
### Local Installation
21+
22+
```bash
23+
go install github.com/xMoelletschi/terraform-gitlab-drift@latest
24+
terraform-gitlab-drift scan --group my-group
25+
```
26+
27+
## GitLab CI Usage
28+
29+
### Basic Drift Check
30+
31+
```yaml
32+
drift-check:
33+
image: ghcr.io/xmoelletschi/terraform-gitlab-drift:latest
34+
script:
35+
- terraform-gitlab-drift scan --group $CI_PROJECT_ROOT_NAMESPACE
36+
```
37+
38+
## Configuration
39+
40+
### Command-line Flags
41+
42+
| Flag | Environment Variable | Default | Description |
43+
| ----------------- | -------------------- | -------------------- | ------------------------------------------------- |
44+
| `--gitlab-token` | `GITLAB_TOKEN` | - | GitLab API token (required) |
45+
| `--gitlab-url` | - | `https://gitlab.com` | GitLab instance URL |
46+
| `--group` | - | - | Top-level group to scan (required for gitlab.com) |
47+
| `--terraform-dir` | - | `.` | Path to Terraform directory |
48+
| `--overwrite` | - | `false` | Overwrite files in terraform directory |
49+
| `--show-diff` | - | `true` | Show diff between generated and existing files |
50+
| `--verbose`, `-v` | - | `false` | Enable verbose (debug) logging |
51+
| `--json` | - | `false` | Output logs in JSON format |
52+
53+
### Supported Resources
54+
55+
- ✅ GitLab Groups ([`gitlab_group`](https://registry.terraform.io/providers/gitlabhq/gitlab/latest/docs/resources/group))
56+
- ✅ GitLab Projects ([`gitlab_project`](https://registry.terraform.io/providers/gitlabhq/gitlab/latest/docs/resources/project))
57+
- 🚧 More resources coming soon
58+
59+
## Contributing
60+
61+
Contributions are welcome! Please:
62+
63+
1. Fork the repository
64+
2. Create a feature branch (`git checkout -b feature/amazing-feature`)
65+
3. Push to the branch (`git push origin feature/amazing-feature`)
66+
4. Open a Pull Request
67+
68+
Please make sure to:
69+
70+
- Add tests for new features
71+
- Update documentation as needed
72+
- Ensure CI checks pass
73+
74+
## License
75+
76+
This project is licensed under the Apache License 2.0 - see the [LICENSE](LICENSE) file for details.
77+
78+
## Acknowledgments
79+
80+
Built with:
81+
82+
- [Cobra](https://github.com/spf13/cobra) - CLI framework
83+
- [GitLab Go SDK](https://gitlab.com/gitlab-org/api/client-go) - GitLab API client
84+
- [HCL](https://github.com/hashicorp/hcl) - Terraform configuration parsing
85+
86+
---
87+
88+
**Note**: This tool is not affiliated with HashiCorp or GitLab.

0 commit comments

Comments
 (0)