Skip to content

Commit 83ddc24

Browse files
Weiyureal666Haoyang TangCMGS
committed
switch the git lib from libgit2 to go-git (#225)
* switch the git lib from libgit2 to go-git * refactor repo methods Co-authored-by: Haoyang Tang <haoyang.tang@shopee.com> Co-authored-by: CMGS <ilskdw@gmail.com>
1 parent f5fa132 commit 83ddc24

21 files changed

+268
-217
lines changed

.github/workflows/golangci-lint.yml

-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ jobs:
1414
steps:
1515
- name: Checkout
1616
uses: actions/checkout@v2
17-
- name: Set up libgit2
18-
run: make libgit2
1917
- name: golangci-lint
2018
uses: golangci/golangci-lint-action@v1
2119
with:

.github/workflows/goreleaser.yml

-3
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@ jobs:
2424
with:
2525
go-version: 1.14
2626

27-
- name: Set up libgit2
28-
run: make libgit2
29-
3027
- name: Run GoReleaser
3128
uses: goreleaser/goreleaser-action@v2
3229
with:

.goreleaser.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@ before:
88
builds:
99
- binary: eru-core
1010
env:
11-
- CGO_ENABLED=1
11+
- CGO_ENABLED=0
1212
ldflags:
1313
- -s -w
1414
- -X github.com/projecteru2/core/versioninfo.REVISION={{.Commit}}
1515
- -X github.com/projecteru2/core/versioninfo.VERSION={{.Env.VERSION}}
1616
- -X github.com/projecteru2/core/versioninfo.BUILTAT={{.Date}}
1717
goos:
1818
- linux
19+
- darwin
1920
goarch:
2021
- amd64
2122

Dockerfile

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
FROM golang:alpine AS BUILD
22

33
# make binary
4-
RUN apk add --no-cache build-base musl-dev libgit2-dev git curl make cmake
4+
RUN apk add --no-cache build-base musl-dev git curl make cmake
55
RUN git clone https://github.com/projecteru2/core.git /go/src/github.com/projecteru2/core
66
WORKDIR /go/src/github.com/projecteru2/core
77
RUN make build && ./eru-core --version
@@ -10,6 +10,6 @@ FROM alpine:latest
1010

1111
RUN mkdir /etc/eru/
1212
LABEL ERU=1
13-
RUN apk --no-cache add libcurl libssh2 libgit2 && rm -rf /var/cache/apk/*
13+
RUN apk --no-cache add libcurl libssh2 && rm -rf /var/cache/apk/*
1414
COPY --from=BUILD /go/src/github.com/projecteru2/core/eru-core /usr/bin/eru-core
1515
COPY --from=BUILD /go/src/github.com/projecteru2/core/core.yaml.sample /etc/eru/core.yaml.sample

Makefile

-13
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,6 @@ mock: deps
2929
mockery -dir vendor/github.com/docker/docker/client -name APIClient -output engine/docker/mocks
3030

3131
.ONESHELL:
32-
libgit2:
33-
cd /tmp
34-
rm -fr libgit2-1.0.1.tar.gz libgit2-1.0.1
35-
curl -Lv -O https://github.com/libgit2/libgit2/releases/download/v1.0.1/libgit2-1.0.1.tar.gz
36-
tar xvfz libgit2-1.0.1.tar.gz
37-
mkdir -p libgit2-1.0.1/build
38-
cd libgit2-1.0.1/build
39-
cmake ..
40-
cmake --build .
41-
sudo cp libgit2.pc /usr/lib/pkgconfig/
42-
sudo cp libgit2.so.1.0.1 /usr/lib
43-
sudo ln -s /usr/lib/libgit2.so.1.0.1 /usr/lib/libgit2.so
44-
sudo cp -aR ../include/* /usr/local/include/
4532

4633
cloc:
4734
cloc --exclude-dir=vendor,3rdmocks,mocks,tools --not-match-f=test .

README.md

+1-12
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,7 @@ Run ` make test `
1919

2020
Run `make deps` for generating vendor dir.
2121

22-
Under macOS we have to install `libgit2` manually, if you using [Homebrew](https://brew.sh/) please install like this:
23-
24-
```shell
25-
brew install libgit2
26-
make deps
27-
```
28-
29-
Make sure your libgit2 version is 1.0.0
30-
31-
In linux you can reference our image's [Dockerfile](https://github.com/projecteru2/core/blob/master/Dockerfile). Our server were running under CentOS 7, so if your server was different, something will not same.
32-
33-
On other hand, you can use our [footstone](https://hub.docker.com/r/projecteru2/footstone/) image for testing and compiling.
22+
You can use our [footstone](https://hub.docker.com/r/projecteru2/footstone/) image for testing and compiling.
3423

3524
#### GRPC
3625

cluster/calcium/calcium.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,14 @@ func New(config types.Config, embeddedStorage bool) (*Calcium, error) {
4242
scmtype := strings.ToLower(config.Git.SCMType)
4343
switch scmtype {
4444
case cluster.Gitlab:
45-
scm = gitlab.New(config)
45+
scm, err = gitlab.New(config)
4646
case cluster.Github:
47-
scm = github.New(config)
47+
scm, err = github.New(config)
4848
default:
4949
log.Warn("[Calcium] SCM not set, build API disabled")
5050
}
5151

52-
return &Calcium{store: store, config: config, scheduler: scheduler, source: scm}, nil
52+
return &Calcium{store: store, config: config, scheduler: scheduler, source: scm}, err
5353
}
5454

5555
// Finalizer use for defer

cluster/calcium/calcium_test.go

+14-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package calcium
22

33
import (
44
"context"
5+
"io/ioutil"
56
"sync"
67
"testing"
78
"time"
@@ -33,7 +34,12 @@ func (d *dummyLock) Unlock(ctx context.Context) error {
3334

3435
func NewTestCluster() *Calcium {
3536
c := &Calcium{}
36-
c.config = types.Config{GlobalTimeout: 30 * time.Second}
37+
c.config = types.Config{
38+
GlobalTimeout: 30 * time.Second,
39+
Git: types.GitConfig{
40+
CloneTimeout: 300 * time.Second,
41+
},
42+
}
3743
c.store = &storemocks.Store{}
3844
c.scheduler = &schedulermocks.Scheduler{}
3945
c.source = &sourcemocks.Source{}
@@ -46,10 +52,15 @@ func TestNewCluster(t *testing.T) {
4652
c, err := New(types.Config{}, true)
4753
assert.NoError(t, err)
4854
c.Finalizer()
49-
c, err = New(types.Config{Git: types.GitConfig{SCMType: "gitlab"}}, true)
55+
privFile, err := ioutil.TempFile("", "priv")
56+
assert.NoError(t, err)
57+
_, err = privFile.WriteString("privkey")
58+
assert.NoError(t, err)
59+
privFile.Close()
60+
c, err = New(types.Config{Git: types.GitConfig{SCMType: "gitlab", PrivateKey: privFile.Name()}}, true)
5061
assert.NoError(t, err)
5162
c.Finalizer()
52-
c, err = New(types.Config{Git: types.GitConfig{SCMType: "github"}}, true)
63+
c, err = New(types.Config{Git: types.GitConfig{SCMType: "github", PrivateKey: privFile.Name()}}, true)
5364
c.Finalizer()
5465
assert.NoError(t, err)
5566
}

core.yaml.sample

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ etcd:
2727
password: root
2828

2929
git:
30-
public_key: "***REMOVED***"
3130
private_key: "***REMOVED***"
3231
token: "***REMOVED***"
3332
scm_type: "github"
33+
clone_timeout: 300s
3434

3535
docker:
3636
log:

engine/docker/build.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,15 @@ func (e *Engine) BuildContent(ctx context.Context, scm coresource.Source, opts *
7575
}
7676
log.Debugf("[BuildContent] Build dir %s", buildDir)
7777
// create dockerfile
78-
if err := e.makeDockerFile(opts, scm, buildDir); err != nil {
78+
if err := e.makeDockerFile(ctx, opts, scm, buildDir); err != nil {
7979
return buildDir, nil, err
8080
}
8181
// create stream for Build API
8282
tar, err := CreateTarStream(buildDir)
8383
return buildDir, tar, err
8484
}
8585

86-
func (e *Engine) makeDockerFile(opts *types.BuildContentOptions, scm coresource.Source, buildDir string) error {
86+
func (e *Engine) makeDockerFile(ctx context.Context, opts *types.BuildContentOptions, scm coresource.Source, buildDir string) error {
8787
var preCache map[string]string
8888
var preStage string
8989
var buildTmpl []string
@@ -96,7 +96,7 @@ func (e *Engine) makeDockerFile(opts *types.BuildContentOptions, scm coresource.
9696
}
9797

9898
// get source or artifacts
99-
reponame, err := e.preparedSource(build, scm, buildDir)
99+
reponame, err := e.preparedSource(ctx, build, scm, buildDir)
100100
if err != nil {
101101
return err
102102
}
@@ -138,7 +138,7 @@ func (e *Engine) makeDockerFile(opts *types.BuildContentOptions, scm coresource.
138138
return createDockerfile(dockerfile, buildDir)
139139
}
140140

141-
func (e *Engine) preparedSource(build *types.Build, scm coresource.Source, buildDir string) (string, error) {
141+
func (e *Engine) preparedSource(ctx context.Context, build *types.Build, scm coresource.Source, buildDir string) (string, error) {
142142
// parse repository name
143143
// code locates under /:repositoryname
144144
var cloneDir string
@@ -157,7 +157,7 @@ func (e *Engine) preparedSource(build *types.Build, scm coresource.Source, build
157157
// clone code into cloneDir
158158
// which is under buildDir and named as repository name
159159
cloneDir = filepath.Join(buildDir, reponame)
160-
if err := scm.SourceCode(build.Repo, cloneDir, version, build.Submodule); err != nil {
160+
if err := scm.SourceCode(ctx, build.Repo, cloneDir, version, build.Submodule); err != nil {
161161
return "", err
162162
}
163163

go.mod

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ require (
1414
github.com/docker/go-connections v0.4.0
1515
github.com/docker/go-units v0.3.3
1616
github.com/docker/libtrust v0.0.0-20160708172513-aabc10ec26b7 // indirect
17+
github.com/go-git/go-git/v5 v5.1.0
1718
github.com/golang/groupcache v0.0.0-20191027212112-611e8accdfc9 // indirect
1819
github.com/golang/protobuf v1.3.5
1920
github.com/gorilla/context v1.1.1 // indirect
2021
github.com/gorilla/mux v0.0.0-20180605211556-cb4698366aa6 // indirect
2122
github.com/jinzhu/configor v1.1.1
22-
github.com/libgit2/git2go/v30 v30.0.3
2323
github.com/opencontainers/go-digest v1.0.0-rc1 // indirect
2424
github.com/opencontainers/image-spec v0.0.0-20180411145040-e562b0440392 // indirect
2525
github.com/opencontainers/runc v0.0.0-20180615140650-ad0f5255060d // indirect
@@ -34,7 +34,7 @@ require (
3434
github.com/urfave/cli/v2 v2.0.0-alpha.2
3535
go.etcd.io/etcd/v3 v3.3.0-rc.0.0.20200707003333-58bb8ae09f8e
3636
go.uber.org/automaxprocs v1.3.0
37-
golang.org/x/crypto v0.0.0-20200221231518-2aa609cf4a9d
37+
golang.org/x/crypto v0.0.0-20200302210943-78000ba7a073
3838
golang.org/x/mod v0.3.0 // indirect
3939
golang.org/x/net v0.0.0-20200319234117-63522dbf7eec
4040
golang.org/x/time v0.0.0-20191024005414-555d28b269f0 // indirect

0 commit comments

Comments
 (0)