Skip to content

Commit 282cb78

Browse files
authored
feat: add support and documentation for remote repository mirroring (#110)
- Add support for mirroring all refs to a remote repository with the --mirror flag - Document new mirror usage in English and Chinese README files - Update parameter references to include mirror option - Show example configuration for remote mirror push - Add Trivy Security Scan badge to Chinese README files fix #47 Signed-off-by: appleboy <appleboy.tw@gmail.com>
1 parent dbd3c99 commit 282cb78

7 files changed

Lines changed: 145 additions & 2 deletions

File tree

DOCS.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,18 @@ An example of specifying the path to a repo:
5151
path: path/to/repo
5252
```
5353
54+
An example of mirroring all refs to a remote repository:
55+
56+
```yaml
57+
- name: mirror push
58+
image: appleboy/drone-git-push
59+
settings:
60+
remote: git@github.com:foo/bar-mirror.git
61+
mirror: true
62+
ssh_key:
63+
from_secret: deploy_key
64+
```
65+
5466
## Parameter Reference
5567
5668
| setting | description
@@ -71,3 +83,4 @@ An example of specifying the path to a repo:
7183
| author_email | the email address to use for the author of the commit (if blank, assume push commiter name)
7284
| followtags | push with --follow-tags option
7385
| rebase | pull --rebase before pushing
86+
| mirror | push all refs to remote repository with --mirror, defaults to false

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,17 @@ docker run --rm \
4646
-w "$(pwd)" \
4747
appleboy/drone-git-push
4848
```
49+
50+
Mirror all refs to a remote repository:
51+
52+
```sh
53+
docker run --rm \
54+
-e DRONE_COMMIT_AUTHOR=Octocat \
55+
-e DRONE_COMMIT_AUTHOR_EMAIL=octocat@github.com \
56+
-e PLUGIN_SSH_KEY="$(cat "${HOME}/.ssh/id_rsa")" \
57+
-e PLUGIN_REMOTE=git@github.com:foo/bar.git \
58+
-e PLUGIN_MIRROR=true \
59+
-v "$(pwd):$(pwd)" \
60+
-w "$(pwd)" \
61+
appleboy/drone-git-push
62+
```

README.zh-cn.md

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
21
# drone-git-push
32

43
[English](README.md) | [繁體中文](README.zh-tw.md)
54

65
[![GoDoc](https://godoc.org/github.com/appleboy/drone-git-push?status.svg)](https://godoc.org/github.com/appleboy/drone-git-push)
76
[![Lint and Testing](https://github.com/appleboy/drone-git-push/actions/workflows/testing.yml/badge.svg)](https://github.com/appleboy/drone-git-push/actions/workflows/testing.yml)
7+
[![Trivy Security Scan](https://github.com/appleboy/drone-git-push/actions/workflows/trivy.yml/badge.svg)](https://github.com/appleboy/drone-git-push/actions/workflows/trivy.yml)
88
[![codecov](https://codecov.io/gh/appleboy/drone-git-push/branch/master/graph/badge.svg)](https://codecov.io/gh/appleboy/drone-git-push)
99
[![Go Report Card](https://goreportcard.com/badge/github.com/appleboy/drone-git-push)](https://goreportcard.com/report/github.com/appleboy/drone-git-push)
1010
[![Docker Pulls](https://img.shields.io/docker/pulls/appleboy/drone-git-push.svg)](https://hub.docker.com/r/appleboy/drone-git-push/)
@@ -15,3 +15,48 @@
1515
## 构建
1616

1717
使用以下命令构建二进制文件:
18+
19+
```sh
20+
go build
21+
go test
22+
```
23+
24+
## Docker
25+
26+
使用以下命令构建 Docker 镜像:
27+
28+
```sh
29+
GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -a -tags netgo -o release/linux/amd64/drone-git-push
30+
docker build --rm -t appleboy/drone-git-push .
31+
```
32+
33+
## 使用方式
34+
35+
从工作目录执行:
36+
37+
```sh
38+
docker run --rm \
39+
-e DRONE_COMMIT_AUTHOR=Octocat \
40+
-e DRONE_COMMIT_AUTHOR_EMAIL=octocat@github.com \
41+
-e PLUGIN_SSH_KEY="$(cat "${HOME}/.ssh/id_rsa")" \
42+
-e PLUGIN_BRANCH=master \
43+
-e PLUGIN_REMOTE=git@github.com:foo/bar.git \
44+
-e PLUGIN_FORCE=false \
45+
-v "$(pwd):$(pwd)" \
46+
-w "$(pwd)" \
47+
appleboy/drone-git-push
48+
```
49+
50+
镜像所有 refs 到远程仓库:
51+
52+
```sh
53+
docker run --rm \
54+
-e DRONE_COMMIT_AUTHOR=Octocat \
55+
-e DRONE_COMMIT_AUTHOR_EMAIL=octocat@github.com \
56+
-e PLUGIN_SSH_KEY="$(cat "${HOME}/.ssh/id_rsa")" \
57+
-e PLUGIN_REMOTE=git@github.com:foo/bar.git \
58+
-e PLUGIN_MIRROR=true \
59+
-v "$(pwd):$(pwd)" \
60+
-w "$(pwd)" \
61+
appleboy/drone-git-push
62+
```

README.zh-tw.md

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
21
# drone-git-push
32

43
[English](README.md) | [简体中文](README.zh-cn.md)
54

65
[![GoDoc](https://godoc.org/github.com/appleboy/drone-git-push?status.svg)](https://godoc.org/github.com/appleboy/drone-git-push)
76
[![Lint and Testing](https://github.com/appleboy/drone-git-push/actions/workflows/testing.yml/badge.svg)](https://github.com/appleboy/drone-git-push/actions/workflows/testing.yml)
7+
[![Trivy Security Scan](https://github.com/appleboy/drone-git-push/actions/workflows/trivy.yml/badge.svg)](https://github.com/appleboy/drone-git-push/actions/workflows/trivy.yml)
88
[![codecov](https://codecov.io/gh/appleboy/drone-git-push/branch/master/graph/badge.svg)](https://codecov.io/gh/appleboy/drone-git-push)
99
[![Go Report Card](https://goreportcard.com/badge/github.com/appleboy/drone-git-push)](https://goreportcard.com/report/github.com/appleboy/drone-git-push)
1010
[![Docker Pulls](https://img.shields.io/docker/pulls/appleboy/drone-git-push.svg)](https://hub.docker.com/r/appleboy/drone-git-push/)
@@ -15,3 +15,48 @@
1515
## 建置
1616

1717
使用以下命令建置二進位檔:
18+
19+
```sh
20+
go build
21+
go test
22+
```
23+
24+
## Docker
25+
26+
使用以下命令建置 Docker 映像檔:
27+
28+
```sh
29+
GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -a -tags netgo -o release/linux/amd64/drone-git-push
30+
docker build --rm -t appleboy/drone-git-push .
31+
```
32+
33+
## 使用方式
34+
35+
從工作目錄執行:
36+
37+
```sh
38+
docker run --rm \
39+
-e DRONE_COMMIT_AUTHOR=Octocat \
40+
-e DRONE_COMMIT_AUTHOR_EMAIL=octocat@github.com \
41+
-e PLUGIN_SSH_KEY="$(cat "${HOME}/.ssh/id_rsa")" \
42+
-e PLUGIN_BRANCH=master \
43+
-e PLUGIN_REMOTE=git@github.com:foo/bar.git \
44+
-e PLUGIN_FORCE=false \
45+
-v "$(pwd):$(pwd)" \
46+
-w "$(pwd)" \
47+
appleboy/drone-git-push
48+
```
49+
50+
鏡像所有 refs 到遠端儲存庫:
51+
52+
```sh
53+
docker run --rm \
54+
-e DRONE_COMMIT_AUTHOR=Octocat \
55+
-e DRONE_COMMIT_AUTHOR_EMAIL=octocat@github.com \
56+
-e PLUGIN_SSH_KEY="$(cat "${HOME}/.ssh/id_rsa")" \
57+
-e PLUGIN_REMOTE=git@github.com:foo/bar.git \
58+
-e PLUGIN_MIRROR=true \
59+
-v "$(pwd):$(pwd)" \
60+
-w "$(pwd)" \
61+
appleboy/drone-git-push
62+
```

main.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,11 @@ func main() {
161161
Usage: "pull rebase branch before push",
162162
EnvVars: []string{"PLUGIN_REBASE", "GIT_PUSH_REBASE", "INPUT_REBASE"},
163163
},
164+
&cli.BoolFlag{
165+
Name: "mirror",
166+
Usage: "push all refs to remote repository with --mirror",
167+
EnvVars: []string{"PLUGIN_MIRROR", "GIT_PUSH_MIRROR", "INPUT_MIRROR"},
168+
},
164169
}
165170

166171
if err := app.Run(os.Args); err != nil {
@@ -197,6 +202,7 @@ func run(c *cli.Context) error {
197202
EmptyCommit: c.Bool("empty-commit"),
198203
NoVerify: c.Bool("no-verify"),
199204
Rebase: c.Bool("rebase"),
205+
Mirror: c.Bool("mirror"),
200206
},
201207
}
202208

plugin.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ type (
4343
EmptyCommit bool
4444
NoVerify bool
4545
Rebase bool
46+
Mirror bool
4647
}
4748

4849
// Plugin Structure
@@ -206,6 +207,10 @@ func (p Plugin) HandleTag(ctx context.Context) error {
206207

207208
// HandlePush pushs the changes to the remote repo.
208209
func (p Plugin) HandlePush(ctx context.Context) error {
210+
if p.Config.Mirror {
211+
return execute(repo.RemotePushMirror(ctx, p.Config.RemoteName))
212+
}
213+
209214
var (
210215
name = p.Config.RemoteName
211216
local = p.Config.LocalBranch

repo/remote.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,18 @@ func RemotePushNamedBranch(
9393

9494
return cmd
9595
}
96+
97+
// RemotePushMirror pushes all refs to remote repository using --mirror.
98+
func RemotePushMirror(ctx context.Context, remote string) *exec.Cmd {
99+
sanitizedRemote := sanitizeInput(remote)
100+
101+
cmd := exec.CommandContext(
102+
ctx,
103+
"git",
104+
"push",
105+
"--mirror",
106+
sanitizedRemote,
107+
)
108+
109+
return cmd
110+
}

0 commit comments

Comments
 (0)