drone-kaniko icon indicating copy to clipboard operation
drone-kaniko copied to clipboard

[Feature] Upload file to DockerHub Description

Open zicklag opened this issue 5 years ago • 6 comments

It would be awesome if you could push a file like the README of the repo to the DockerHub description automatically.

zicklag avatar Jul 10 '19 17:07 zicklag

You mean to here https://hub.docker.com/r/banzaicloud/drone-kaniko ?

bonifaido avatar Jul 10 '19 18:07 bonifaido

Yes.

zicklag avatar Jul 10 '19 18:07 zicklag

my solution for harbor

desc.go

package main
import (
	"encoding/json"
	"net/http"
	"io/ioutil"
	"fmt"
	"flag"
	"bytes"
	"strings"
	"os"
)
// Generated by curl-to-Go: https://mholt.github.io/curl-to-go

type Payload struct {
	Description string `json:"description"`
}

func main () {
	api := flag.String("api", "http://harbor-api-proxy.intra.xx.com/api/repositories", "harbor api for repo description")
	desc := flag.String("desc", "README.md", "description filename")
	repo := flag.String("repo", "", "repo name")
	flag.Parse()

	if *repo == "" {
		fmt.Println("must provide repo name")
		os.Exit(1)
	}

	repository := strings.Replace(*repo, "/", "%2F", -1)

	// read description from desc file
	b, err := ioutil.ReadFile(*desc)
	if err != nil {
		fmt.Print(err)
	}
	str := string(b)

	data := Payload{Description: str}
	payloadBytes, err := json.Marshal(data)
	if err != nil {
		fmt.Print(err)
	}
	body := bytes.NewReader(payloadBytes)

	url := *api + "/" + repository
	req, err := http.NewRequest("PUT", url, body)
	if err != nil {
		fmt.Print(err)
	}
	req.Header.Set("Accept", "application/json")
	req.Header.Set("Content-Type", "application/json")

	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		fmt.Print(err)
	}

	fmt.Println(resp.Status)
	defer resp.Body.Close()
}

static build desc

CGO_ENABLED go build -o bin/desc desc.go

add bin/desc in Dockerfile

ADD bin/desc /kaniko/desc

desc command usage

# ./bin/desc -h
Usage of ./bin/desc:
  -api string
    	harbor api for repo description (default "http://harbor-api-proxy.intra.xx.com/api/repositories")
  -desc string
    	description filename (default "README.md")
  -repo string
    	repo name

use desc in plugin.sh

# update repo description. use harbor api. Should run after image push. Otherwise in first build harbor api will return 404 Not Found
/kaniko/desc -api "${PLUGIN_DESC_API:-http://harbor-api-proxy.intra.xx.com/api/repositories}" -desc "${PLUGIN_DESC_FILE:-README.md}" -repo "${PLUGIN_REPO:-}"

annProg avatar Nov 07 '19 16:11 annProg

Sorry @annProg what it this program for exactly? :)

bonifaido avatar Nov 11 '19 09:11 bonifaido

Sorry @annProg what it this program for exactly? :)

Update repo description(default use README.md) in Harbor. It is similar to DockerHub Description.for reference only

annProg avatar Nov 11 '19 10:11 annProg

Screenshot in harbor image

annProg avatar Nov 11 '19 10:11 annProg