Skip to content

Commit

Permalink
feat: add support for LFS (#161)
Browse files Browse the repository at this point in the history
  • Loading branch information
wass3rw3rk authored Aug 29, 2024
1 parent e02873d commit 612bfde
Show file tree
Hide file tree
Showing 11 changed files with 216 additions and 89 deletions.
80 changes: 48 additions & 32 deletions DOCS.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,46 +20,61 @@ Sample of cloning a repository:

```yaml
steps:
- name: clone_hello-world
- name: clone_vela-git-test
image: target/vela-git:latest
pull: always
parameters:
path: hello-world
ref: refs/heads/master
remote: https://github.com/octocat/hello-world.git
sha: 7fd1a60b01f91b314f59955a4e4d4e80d8edf11d
path: vela-git-test
ref: refs/heads/main
remote: https://github.com/go-vela/vela-git-test.git
sha: ee1e671529ad86a11ed628a04b37829e71783682
```
Sample of cloning a repository with submodules:
```diff
steps:
- name: clone_hello-world
- name: clone_vela-git-test
image: target/vela-git:latest
pull: always
parameters:
path: hello-world
ref: refs/heads/master
remote: https://github.com/octocat/hello-world.git
sha: 7fd1a60b01f91b314f59955a4e4d4e80d8edf11d
path: vela-git-test
ref: refs/heads/main
remote: https://github.com/go-vela/vela-git-test.git
sha: ee1e671529ad86a11ed628a04b37829e71783682
+ submodules: true
```
Sample of cloning a repository with tags:
```diff
steps:
- name: clone_hello-world
- name: clone_vela-git-test
image: target/vela-git:latest
pull: always
parameters:
path: hello-world
ref: refs/heads/master
remote: https://github.com/octocat/hello-world.git
sha: 7fd1a60b01f91b314f59955a4e4d4e80d8edf11d
path: vela-git-test
ref: refs/heads/main
remote: https://github.com/go-vela/vela-git-test.git
sha: ee1e671529ad86a11ed628a04b37829e71783682
+ tags: true
```
Sample of cloning a repository and resolving LFS objects:
```diff
steps:
- name: clone_vela-git-test
image: target/vela-git:latest
pull: always
parameters:
path: vela-git-test
ref: refs/heads/main
remote: https://github.com/go-vela/vela-git-test.git
sha: ee1e671529ad86a11ed628a04b37829e71783682
+ lfs: true
```
## Secrets
> **NOTE:** Users should refrain from configuring sensitive information in your pipeline in plain text.
Expand All @@ -70,20 +85,20 @@ Users can use [Vela internal secrets](https://go-vela.github.io/docs/tour/secret
```diff
steps:
- name: clone_hello-world
- name: clone_vela-git-test
image: target/vela-git:latest
pull: always
+ secrets: [ git_username, git_password ]
parameters:
- username: octocat
- password: superSecretPassword
path: /home/octocat_hello-world_1
ref: refs/heads/master
remote: https://github.com/octocat/hello-world.git
sha: 7fd1a60b01f91b314f59955a4e4d4e80d8edf11d
path: /home/go-vela_vela-git-test_1
ref: refs/heads/main
remote: https://github.com/go-vela/vela-git-test.git
sha: ee1e671529ad86a11ed628a04b37829e71783682
```
> This example will add the secrets to the `clone_hello-world` step as environment variables:
> This example will add the secrets to the `clone_vela-git-test` step as environment variables:
>
> * `GIT_USERNAME=<value>`
> * `GIT_PASSWORD=<value>`
Expand All @@ -101,16 +116,16 @@ Users can use [Vela external secrets](https://go-vela.github.io/docs/concepts/pi

```diff
steps:
- name: clone_hello-world
- name: clone_vela-git-test
image: target/vela-git:latest
pull: always
parameters:
- username: octocat
- password: superSecretPassword
path: /home/octocat_hello-world_1
ref: refs/heads/master
remote: https://github.com/octocat/hello-world.git
sha: 7fd1a60b01f91b314f59955a4e4d4e80d8edf11d
path: /home/go-vela_vela-git-test_1
ref: refs/heads/main
remote: https://github.com/go-vela/vela-git-test.git
sha: ee1e671529ad86a11ed628a04b37829e71783682
```

> This example will read the secret values in the volume stored at `/vela/secrets/`
Expand Down Expand Up @@ -139,6 +154,7 @@ The following parameters are used to configure the image:
| `submodules` | enables fetching of submodules | `false` | `false` | `PARAMETER_SUBMODULES`<br>`GIT_SUBMODULES` |
| `tags` | enables fetching of tags | `false` | `false` | `PARAMETER_TAGS`<br>`GIT_TAGS` |
| `depth` | enables fetching with a specific depth | `false` | `100` | `PARAMETER_DEPTH`<br>`GIT_DEPTH` |
| `lfs` | enables resolving LFS objects | `false` | `false` | `PARAMETER_LFS`<br>`GIT_LFS` |

## Template

Expand All @@ -150,15 +166,15 @@ You can start troubleshooting this plugin by tuning the level of logs being disp

```diff
steps:
- name: clone_hello-world
- name: clone_vela-git-test
image: target/vela-git:latest
pull: always
parameters:
+ log_level: trace
path: hello-world
ref: refs/heads/master
remote: https://github.com/octocat/hello-world.git
sha: 7fd1a60b01f91b314f59955a4e4d4e80d8edf11d
path: vela-git-test
ref: refs/heads/main
remote: https://github.com/go-vela/vela-git-test.git
sha: ee1e671529ad86a11ed628a04b37829e71783682
```

Below are a list of common problems and how to solve them:
Below are a list of common problems and how to solve them:
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ FROM alpine:3.20.2@sha256:0a4eaa0eecf5f8c050e5bba433f58c052be7587ee8af3e8b3910ef

COPY --from=certs /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt

RUN apk add --update --no-cache git github-cli
RUN apk add --update --no-cache git git-lfs github-cli

COPY release/vela-git /bin/vela-git

Expand Down
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -223,10 +223,10 @@ docker-test:
@echo
@echo "### Testing vela-git:local image"
@docker run --rm \
-e PARAMETER_REMOTE=https://github.com/octocat/hello-world.git \
-e PARAMETER_PATH=home/octocat_hello-world_1 \
-e PARAMETER_REF=refs/heads/master \
-e PARAMETER_SHA=7fd1a60b01f91b314f59955a4e4d4e80d8edf11d \
-e PARAMETER_REMOTE=https://github.com/go-vela/vela-git-test.git \
-e PARAMETER_PATH=home/go-vela_vela-git-test_1 \
-e PARAMETER_REF=refs/heads/main \
-e PARAMETER_SHA=ee1e671529ad86a11ed628a04b37829e71783682 \
-e PARAMETER_TAGS \
-e PARAMETER_SUBMODULES \
-e VELA_NETRC_MACHINE=github.com \
Expand Down
32 changes: 16 additions & 16 deletions cmd/vela-git/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,46 +14,46 @@ func TestGit_Build_Validate(t *testing.T) {
name: "success",
failure: false,
build: &Build{
Branch: "master",
Path: "/home/octocat_hello-world_1",
Ref: "refs/heads/master",
Sha: "7fd1a60b01f91b314f59955a4e4d4e80d8edf11d",
Branch: "main",
Path: "/home/go-vela_vela-git-test_1",
Ref: "refs/heads/main",
Sha: "ee1e671529ad86a11ed628a04b37829e71783682",
},
},
{
name: "failure with no branch",
failure: true,
build: &Build{
Path: "/home/octocat_hello-world_1",
Ref: "refs/heads/master",
Sha: "7fd1a60b01f91b314f59955a4e4d4e80d8edf11d",
Path: "/home/go-vela_vela-git-test_1",
Ref: "refs/heads/main",
Sha: "ee1e671529ad86a11ed628a04b37829e71783682",
},
},
{
name: "failure with no path",
failure: true,
build: &Build{
Branch: "master",
Ref: "refs/heads/master",
Sha: "7fd1a60b01f91b314f59955a4e4d4e80d8edf11d",
Branch: "main",
Ref: "refs/heads/main",
Sha: "ee1e671529ad86a11ed628a04b37829e71783682",
},
},
{
name: "failure with no ref",
failure: true,
build: &Build{
Branch: "master",
Path: "/home/octocat_hello-world_1",
Sha: "7fd1a60b01f91b314f59955a4e4d4e80d8edf11d",
Branch: "main",
Path: "/home/go-vela_vela-git-test_1",
Sha: "ee1e671529ad86a11ed628a04b37829e71783682",
},
},
{
name: "failure with no sha",
failure: true,
build: &Build{
Branch: "master",
Path: "/home/octocat_hello-world_1",
Ref: "refs/heads/master",
Branch: "main",
Path: "/home/go-vela_vela-git-test_1",
Ref: "refs/heads/main",
},
},
}
Expand Down
20 changes: 19 additions & 1 deletion cmd/vela-git/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,30 @@ func defaultBranchCmd(branch string) *exec.Cmd {
func resetCmd(sha string) *exec.Cmd {
logrus.Trace("returning resetCmd")

return exec.Command(
cmd := exec.Command(
"git",
"reset",
"--hard",
sha,
)

// skip resolving LFS objects by default
// https://github.com/git-lfs/git-lfs/blob/main/docs/man/git-lfs-smudge.adoc
cmd.Env = append(cmd.Env, "GIT_LFS_SKIP_SMUDGE=1")

return cmd
}

// getLFSCmd is a helper function to
// resolve LFS objects.
func getLFSCmd() *exec.Cmd {
logrus.Trace("returning command to pull LFS objects")

return exec.Command(
"git",
"lfs",
"pull",
)
}

// submoduleCmd is a helper function to
Expand Down
32 changes: 24 additions & 8 deletions cmd/vela-git/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ func TestGit_fetchCmdWithTags(t *testing.T) {
"--depth",
"10",
"origin",
"refs/heads/master",
"refs/heads/main",
)

got := fetchCmd("refs/heads/master", true, "10")
got := fetchCmd("refs/heads/main", true, "10")

if !reflect.DeepEqual(got, want) {
t.Errorf("fetchTagsCmd is %v, want %v", got, want)
Expand All @@ -46,10 +46,10 @@ func TestGit_fetchCmdNoTags(t *testing.T) {
"--depth",
"100",
"origin",
"refs/heads/master",
"refs/heads/main",
)

got := fetchCmd("refs/heads/master", false, "")
got := fetchCmd("refs/heads/main", false, "")

if !reflect.DeepEqual(got, want) {
t.Errorf("fetchNoTagsCmd is %v, want %v", got, want)
Expand Down Expand Up @@ -94,10 +94,10 @@ func TestGit_remoteAddCmd(t *testing.T) {
"remote",
"add",
"origin",
"https://github.com/octocat/hello-world.git",
"https://github.com/go-vela/vela-git-test.git",
)

got := remoteAddCmd("https://github.com/octocat/hello-world.git")
got := remoteAddCmd("https://github.com/go-vela/vela-git-test.git")

if !reflect.DeepEqual(got, want) {
t.Errorf("remoteAddCmd is %v, want %v", got, want)
Expand Down Expand Up @@ -125,10 +125,11 @@ func TestGit_resetCmd(t *testing.T) {
"git",
"reset",
"--hard",
"7fd1a60b01f91b314f59955a4e4d4e80d8edf11d",
"ee1e671529ad86a11ed628a04b37829e71783682",
)
want.Env = append(want.Env, "GIT_LFS_SKIP_SMUDGE=1")

got := resetCmd("7fd1a60b01f91b314f59955a4e4d4e80d8edf11d")
got := resetCmd("ee1e671529ad86a11ed628a04b37829e71783682")

if !reflect.DeepEqual(got, want) {
t.Errorf("resetCmd is %v, want %v", got, want)
Expand All @@ -150,3 +151,18 @@ func TestGit_submoduleCmd(t *testing.T) {
t.Errorf("submoduleCmd is %v, want %v", got, want)
}
}

func TestGit_getLFSCmd(t *testing.T) {
// setup types
want := exec.Command(
"git",
"lfs",
"pull",
)

got := getLFSCmd()

if !reflect.DeepEqual(got, want) {
t.Errorf("getLFSCmd is %v, want %v", got, want)
}
}
7 changes: 7 additions & 0 deletions cmd/vela-git/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,12 @@ func main() {
Name: "repo.tags",
Usage: "enables fetching tags for the repo being cloned",
},
&cli.BoolFlag{
EnvVars: []string{"PARAMETER_LFS", "GIT_LFS"},
FilePath: "/vela/parameters/git/lfs,/vela/secrets/git/lfs",
Name: "repo.lfs",
Usage: "enables resolving LFS objects",
},
}

err = app.Run(os.Args)
Expand Down Expand Up @@ -197,6 +203,7 @@ func run(c *cli.Context) error {
Remote: c.String("repo.remote"),
Submodules: c.Bool("repo.submodules"),
Tags: c.Bool("repo.tags"),
LFS: c.Bool("repo.lfs"),
},
}

Expand Down
8 changes: 8 additions & 0 deletions cmd/vela-git/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,14 @@ func (p *Plugin) Exec() error {
}
}

// if LFS is enabled, get/resolve the LFS objects
if p.Repo.LFS {
err = execCmd(getLFSCmd())
if err != nil {
return err
}
}

return nil
}

Expand Down
Loading

0 comments on commit 612bfde

Please sign in to comment.