Skip to content

Commit 0cc8c77

Browse files
Ketbomealexandear
andauthored
Add Docker support for easier setup (#167)
Co-authored-by: Oleksandr Redko <oleksandr.red+github@gmail.com>
1 parent 18b0ce5 commit 0cc8c77

File tree

11 files changed

+214
-12
lines changed

11 files changed

+214
-12
lines changed

.dockerignore

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
.git
2+
.gitignore
3+
4+
/output/
5+
repos/
6+
git-cache/
7+
8+
.env
9+
.env.local
10+
11+
*.md
12+
!README.md
13+
screenshots/
14+
15+
.github/
16+
17+
.vscode/
18+
.idea/
19+
*.swp
20+
*.swo
21+
22+
.DS_Store
23+
Thumbs.db
24+
25+
*_test.go
26+
testdata/
27+
28+
tools/
29+
Makefile

.env.example

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Copy this file to .env and fill in your real information.
2+
3+
# GitLab instance URL.
4+
# See https://stackoverflow.com/questions/58236175/what-is-a-gitlab-instance-url-and-how-can-i-get-it.
5+
# For self-hosted GitLab, use your company's URL, like: https://gitlab.yourcompany.com.
6+
GITLAB_BASE_URL=https://gitlab.com
7+
8+
# GitLab personal access token with scopes: read_api, read_user, read_repository.
9+
# See https://gitlab.com/-/user_settings/personal_access_tokens.
10+
GITLAB_TOKEN=<your_gitlab_token_here>
11+
12+
# Your GitHub name with surname.
13+
COMMITTER_NAME="John Doe"
14+
15+
# Your GitHub email.
16+
COMMITTER_EMAIL=john.doe@example.com

.github/workflows/build.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,27 @@ jobs:
2121
go-version: 'stable'
2222
check-latest: true
2323

24+
- uses: docker/setup-buildx-action@v3
25+
2426
- run: go mod tidy -diff
2527

2628
- run: go mod download
2729

2830
- run: go mod verify
2931

3032
- run: go build -o /dev/null ./...
33+
34+
docker:
35+
runs-on: ubuntu-latest
36+
timeout-minutes: 5
37+
38+
steps:
39+
- uses: actions/checkout@v5
40+
41+
- uses: docker/setup-buildx-action@v3
42+
43+
- run: docker build --progress=plain -t github.com/alexandear/import-gitlab-commits:ci .
44+
45+
- name: Smoke test binary
46+
run: |
47+
docker run --rm github.com/alexandear/import-gitlab-commits:ci --help

.github/workflows/test.yml

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@ jobs:
2323
go-version: 'stable'
2424
check-latest: true
2525

26-
- run: go test -v -count=1 -race -shuffle=on -cover ./...
26+
- name: Install binary
27+
run: go install
2728

28-
- run: go test -tags=integration -run=TestGitLab -shuffle=on -count=1 -race -v ./...
29+
- name: Smoke tests
30+
run: |
31+
import-gitlab-commits -h
32+
import-gitlab-commits --help
33+
34+
- name: Unit tests
35+
run: go test -v -count=1 -race -shuffle=on -cover ./...
36+
37+
- name: Integration tests
38+
run: go test -tags=integration -run=TestGitLab -shuffle=on -count=1 -race -v ./...

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ bin/
44
repo\.*
55
\.vscode
66
\.env
7+
/output

Dockerfile

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
FROM golang:1.24-alpine AS builder
2+
3+
WORKDIR /app
4+
5+
COPY go.mod go.sum ./
6+
RUN go mod download
7+
8+
COPY . .
9+
10+
RUN CGO_ENABLED=0 GOOS=linux go build -ldflags="-s -w" -trimpath -o import-gitlab-commits
11+
12+
FROM alpine:latest
13+
14+
WORKDIR /root/
15+
16+
RUN apk --no-cache add git ca-certificates
17+
18+
COPY --from=builder /app/import-gitlab-commits /usr/local/bin/import-gitlab-commits
19+
20+
RUN chmod +x /usr/local/bin/import-gitlab-commits
21+
22+
ENTRYPOINT ["import-gitlab-commits"]

Makefile

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,22 @@ build:
1818
@echo build
1919
@go build -o $(GOBIN)/import-gitlab-commits
2020

21+
.PHONY: docker-build
22+
docker-build:
23+
@echo docker-build
24+
@docker build -t github.com/alexandear/import-gitlab-commits .
25+
26+
.PHONY: docker-run
27+
docker-run: docker-build
28+
@echo docker-run
29+
@docker run --rm \
30+
-e GITLAB_BASE_URL="${GITLAB_BASE_URL}" \
31+
-e GITLAB_TOKEN="${GITLAB_TOKEN}" \
32+
-e COMMITTER_NAME="${COMMITTER_NAME}" \
33+
-e COMMITTER_EMAIL="${COMMITTER_EMAIL}" \
34+
-v $$(pwd)/output:/root \
35+
github.com/alexandear/import-gitlab-commits
36+
2137
.PHONY: test
2238
test:
2339
@echo test

README.md

Lines changed: 59 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,52 @@ After:
2828

2929
## Getting Started
3030

31+
You can run the tool with Docker or a native Go build.
32+
33+
## Docker Usage
34+
35+
Run the tool using Docker Compose:
36+
37+
1. Copy the environment variables template:
38+
39+
```shell
40+
cp .env.example .env
41+
```
42+
43+
2. Edit `.env` and configure your credentials.
44+
45+
3. Build and run with [Docker Compose](https://docs.docker.com/compose/install/):
46+
47+
```shell
48+
docker compose up
49+
```
50+
51+
The imported repository will be created in the `output/` directory.
52+
53+
### Docker Build Only
54+
55+
If you prefer plain Docker:
56+
57+
Build:
58+
59+
```shell
60+
docker build -t github.com/alexandear/import-gitlab-commits .
61+
```
62+
63+
Run:
64+
65+
```shell
66+
docker run --rm \
67+
-e GITLAB_BASE_URL="https://gitlab.com" \
68+
-e GITLAB_TOKEN="<your_gitlab_token>" \
69+
-e COMMITTER_NAME="John Doe" \
70+
-e COMMITTER_EMAIL="john.doe@example.com" \
71+
-v $(pwd)/output:/root \
72+
github.com/alexandear/import-gitlab-commits
73+
```
74+
75+
### Using native Go build
76+
3177
1. Download and install [Go](https://go.dev/dl/).
3278
2. Install the program by running the following command in a shell:
3379

@@ -38,22 +84,25 @@ After:
3884
3. Set environment variables and run `import-gitlab-commits`:
3985

4086
```shell
41-
export GITLAB_BASE_URL=<your_gitlab_server_url>
87+
export GITLAB_BASE_URL=https://gitlab.com
4288
export GITLAB_TOKEN=<your_gitlab_token>
43-
export COMMITTER_NAME="<Name Surname>"
44-
export COMMITTER_EMAIL=<mail@example.com>
45-
89+
export COMMITTER_NAME="John Doe"
90+
export COMMITTER_EMAIL=john.doe@example.com
91+
4692
$(go env GOPATH)/bin/import-gitlab-commits
4793
```
4894

49-
Where:
95+
Where `$(go env GOPATH)/bin/` is the path where `import-gitlab-commits` is installed.
96+
97+
## Configuration
98+
99+
Configure the tool with these environment variables:
50100

51-
- `GITLAB_BASE_URL` is a GitLab [instance URL](https://stackoverflow.com/questions/58236175/what-is-a-gitlab-instance-url-and-how-can-i-get-it),
101+
- `GITLAB_BASE_URL` - a GitLab [instance URL](https://stackoverflow.com/questions/58236175/what-is-a-gitlab-instance-url-and-how-can-i-get-it),
52102
e.g., `https://gitlab.com`, `https://gitlab.gnome.org`, or any GitLab server.
53-
- `GITLAB_TOKEN` is a personal [access token](https://docs.gitlab.com/ee/user/profile/personal_access_tokens.html#create-a-personal-access-token).
54-
- `COMMITTER_NAME` is your GitHub name with surname, e.g., `John Doe` (can be passed to `git config user.name`).
55-
- `COMMITTER_EMAIL` is your GitHub email, e.g., `john.doe@example.com` (valid for `git config user.email`).
56-
- `$(go env GOPATH)/bin/` is the path where `import-gitlab-commits` is installed.
103+
- `GITLAB_TOKEN` - a personal [access token](https://docs.gitlab.com/ee/user/profile/personal_access_tokens.html#create-a-personal-access-token) with these scopes: `read_api`, `read_user`, `read_repository`.
104+
- `COMMITTER_NAME` - your GitHub name with surname, e.g., `John Doe` (can be passed to `git config user.name`).
105+
- `COMMITTER_EMAIL` - your GitHub email, e.g., `john.doe@example.com` (valid for `git config user.email`).
57106

58107
## Internals
59108

docker-compose.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
services:
2+
import-gitlab-commits:
3+
build: .
4+
container_name: import-gitlab-commits
5+
env_file:
6+
- .env
7+
environment:
8+
- GITLAB_BASE_URL=${GITLAB_BASE_URL:-https://gitlab.com}
9+
- GITLAB_TOKEN=${GITLAB_TOKEN}
10+
- COMMITTER_NAME=${COMMITTER_NAME}
11+
- COMMITTER_EMAIL=${COMMITTER_EMAIL}
12+
volumes:
13+
- ./output:/root
14+
restart: "no"

main.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"context"
55
"errors"
6+
"flag"
67
"fmt"
78
"log"
89
"net/url"
@@ -14,6 +15,23 @@ import (
1415

1516
const (
1617
runTimeout = 10 * time.Minute
18+
19+
helpText = `Import GitLab Commits
20+
21+
Imports commits from a private GitLab repository to a separate repository.
22+
23+
Usage:
24+
import-gitlab-commits [flags]
25+
26+
Flags:
27+
-h, --help Show help message
28+
29+
Environment Variables:
30+
GITLAB_BASE_URL GitLab instance URL (e.g., https://gitlab.com)
31+
GITLAB_TOKEN GitLab personal access token (scopes: read_api, read_user, read_repository)
32+
COMMITTER_NAME Your full name (e.g., John Doe)
33+
COMMITTER_EMAIL Your email (e.g., john.doe@example.com)
34+
`
1735
)
1836

1937
func Execute(logger *log.Logger) error {
@@ -53,6 +71,16 @@ func Execute(logger *log.Logger) error {
5371
}
5472

5573
func main() {
74+
help := flag.Bool("help", false, "Show help message")
75+
flag.BoolVar(help, "h", false, "Show help message (shorthand)")
76+
flag.Parse()
77+
78+
if *help {
79+
_, _ = os.Stdout.WriteString(helpText)
80+
81+
os.Exit(0)
82+
}
83+
5684
logger := log.New(os.Stdout, "", log.Lshortfile|log.Ltime)
5785

5886
if err := Execute(logger); err != nil {

0 commit comments

Comments
 (0)