Skip to content

Commit

Permalink
支持containerd
Browse files Browse the repository at this point in the history
  • Loading branch information
togettoyou committed Jan 27, 2024
1 parent e12501f commit 0968e35
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 34 deletions.
24 changes: 5 additions & 19 deletions .github/workflows/hub-mirror.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,34 +26,20 @@ jobs:
go-version: '1.20'
# 3. 运行 go 代码
- name: Run code
run: go run main.go --username=${{ secrets.DOCKER_USERNAME }} --password=${{ secrets.DOCKER_TOKEN }} --repository=${{ secrets.DOCKER_REPOSITORY }} --content='${{ github.event.issue.body }}' --maxContent=11 --outputPath=output.sh
# 4. 当成功输出 output.sh 文件时,为 issues 添加评论
run: go run main.go --username=${{ secrets.DOCKER_USERNAME }} --password=${{ secrets.DOCKER_TOKEN }} --repository=${{ secrets.DOCKER_REPOSITORY }} --content='${{ github.event.issue.body }}' --maxContent=11 --outputPath=output.md
# 4. 当成功输出 output.md 文件时,为 issues 添加评论
- name: Add comment
if: ${{ hashFiles('output.sh') }}
if: ${{ hashFiles('output.md') }}
uses: actions/github-script@v5
with:
script: |
const fs = require('fs')
const data = fs.readFileSync('output.sh', 'utf8')
const body = "Hello,您可以直接执行该命令:\n" +
"\n" +
"```shell\n" +
"echo -e '" + data.replace(/\n/g, '\\n') + "' | bash\n" +
"```\n" +
"\n" +
"或是手动执行:\n" +
"\n" +
"```shell\n" +
data +
"\n" +
"```\n" +
"\n" +
"希望可以帮助到您,祝您周" + "日一二三四五六".charAt(new Date().getDay()) + "愉快!"
const data = fs.readFileSync('output.md', 'utf8')
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: body
body: data
})
# 5. 当以上步骤成功时,为 issues 添加 success 标签
- name: Success issues
Expand Down
10 changes: 2 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var (
repository = pflag.StringP("repository", "", "", "推送仓库地址,为空默认为 hub.docker.com")
username = pflag.StringP("username", "", "", "仓库用户名")
password = pflag.StringP("password", "", "", "仓库密码")
outputPath = pflag.StringP("outputPath", "", "output.sh", "结果输出路径")
outputPath = pflag.StringP("outputPath", "", "output.md", "结果输出路径")
)

func main() {
Expand Down Expand Up @@ -80,13 +80,7 @@ func main() {
panic("没有转换成功的镜像")
}

tmpl, err := template.New("pull_images").Parse(
`{{if .Repository}}# if your repository is private,please login...
# docker login {{ .Repository }} --username={your username}
{{end}}
{{- range .Outputs }}
docker pull {{ .Target }}
docker tag {{ .Target }} {{ .Source }}{{ end }}`)
tmpl, err := template.ParseFiles("output.tmpl")
if err != nil {
panic(err)
}
Expand Down
13 changes: 13 additions & 0 deletions output.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
### docker 版本

```shell
{{- range .Outputs }}
docker pull {{ .Target }} && docker tag {{ .Target }} {{ .Source }}{{ end }}
```

### containerd 版本(以 k8s.io namespaces 为例)

```shell
{{- range .Outputs }}
ctr -n k8s.io image pull {{ .Target }} && ctr -n k8s.io image tag {{ .Target }} {{ .Source }}{{ end }}
```
2 changes: 1 addition & 1 deletion pkg/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (c *Cli) Source2Target(source string) (*Output, error) {
}

if c.repository == "" {
target = c.username + "/" + strings.ReplaceAll(target, "/", ".")
target = "docker.io/" + c.username + "/" + strings.ReplaceAll(target, "/", ".")
} else {
target = c.repository + "/" + strings.ReplaceAll(target, "/", ".")
}
Expand Down
12 changes: 6 additions & 6 deletions pkg/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,35 +19,35 @@ func TestSource2Target(t *testing.T) {
output, err = cli.Source2Target(source)
assert.Nil(t, err)
assert.Equal(t, source, output.Source)
assert.Equal(t, "togettoyou/registry.k8s.io.kube-apiserver", output.Target)
assert.Equal(t, "docker.io/togettoyou/registry.k8s.io.kube-apiserver", output.Target)

source = "registry.k8s.io/kube-apiserver:v1.27.4"
output, err = cli.Source2Target(source)
assert.Nil(t, err)
assert.Equal(t, source, output.Source)
assert.Equal(t, "togettoyou/registry.k8s.io.kube-apiserver:v1.27.4", output.Target)
assert.Equal(t, "docker.io/togettoyou/registry.k8s.io.kube-apiserver:v1.27.4", output.Target)

source = "registry.k8s.io/kube-apiserver:v1.27.4$kube-apiserver"
output, err = cli.Source2Target(source)
assert.Nil(t, err)
assert.Equal(t, "registry.k8s.io/kube-apiserver:v1.27.4", output.Source)
assert.Equal(t, "togettoyou/kube-apiserver:v1.27.4", output.Target)
assert.Equal(t, "docker.io/togettoyou/kube-apiserver:v1.27.4", output.Target)

source = "registry.k8s.io/kube-apiserver:v1.27.4$kube-apiserver:mytag"
output, err = cli.Source2Target(source)
assert.Nil(t, err)
assert.Equal(t, "registry.k8s.io/kube-apiserver:v1.27.4", output.Source)
assert.Equal(t, "togettoyou/kube-apiserver:mytag", output.Target)
assert.Equal(t, "docker.io/togettoyou/kube-apiserver:mytag", output.Target)

source = "nginx@sha256:123456$nginx"
output, err = cli.Source2Target(source)
assert.Nil(t, err)
assert.Equal(t, "nginx@sha256:123456", output.Source)
assert.Equal(t, "togettoyou/nginx", output.Target)
assert.Equal(t, "docker.io/togettoyou/nginx", output.Target)

source = "nginx@sha256:123456$nginx:mytag"
output, err = cli.Source2Target(source)
assert.Nil(t, err)
assert.Equal(t, "nginx@sha256:123456", output.Source)
assert.Equal(t, "togettoyou/nginx:mytag", output.Target)
assert.Equal(t, "docker.io/togettoyou/nginx:mytag", output.Target)
}

0 comments on commit 0968e35

Please sign in to comment.