diff --git a/.github/workflows/hub-mirror.yml b/.github/workflows/hub-mirror.yml index 13e00d7..24af03b 100644 --- a/.github/workflows/hub-mirror.yml +++ b/.github/workflows/hub-mirror.yml @@ -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 diff --git a/main.go b/main.go index ab2df1f..5a62f70 100644 --- a/main.go +++ b/main.go @@ -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() { @@ -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) } diff --git a/output.tmpl b/output.tmpl new file mode 100644 index 0000000..ec1252d --- /dev/null +++ b/output.tmpl @@ -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 }} +``` diff --git a/pkg/cli.go b/pkg/cli.go index f1dfb8d..a2f460a 100644 --- a/pkg/cli.go +++ b/pkg/cli.go @@ -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, "/", ".") } diff --git a/pkg/cli_test.go b/pkg/cli_test.go index b815188..90f6bc0 100644 --- a/pkg/cli_test.go +++ b/pkg/cli_test.go @@ -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) }