generated from actions/container-action
-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
74 lines (58 loc) · 1.37 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"regexp"
"strings"
"time"
"github.com/coreos/go-semver/semver"
)
type dhtag struct {
Name string `json:"name"`
}
type dhrepo struct {
Count int `json:"count"`
Results []dhtag `json:"results"`
}
func main() {
org := os.Getenv("INPUT_ORG")
repo := os.Getenv("INPUT_REPO")
url := fmt.Sprintf(`https://hub.docker.com/v2/repositories/%s/%s/tags/?page_size=10`, org, repo)
dhClient := http.Client{
Timeout: time.Second * 2, // Maximum of 2 secs
}
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
log.Fatal(err)
}
req.Header.Set("User-Agent", "gha-get-docker-hub-tags")
res, getErr := dhClient.Do(req)
if getErr != nil {
log.Fatal(getErr)
}
body, readErr := ioutil.ReadAll(res.Body)
if readErr != nil {
log.Fatal(readErr)
}
dhrepo1 := dhrepo{}
unmarshalErr := json.Unmarshal(body, &dhrepo1)
if unmarshalErr != nil {
log.Fatal(unmarshalErr)
}
var tags []*semver.Version
for _, tag := range dhrepo1.Results {
matched, _ := regexp.MatchString(`.*\..*\..*`, tag.Name)
if matched {
tags = append(tags, semver.New(strings.Trim(tag.Name, "vV")))
}
}
if len(tags) == 0 {
log.Fatal(fmt.Sprintf(`Unable to find tags for %s/%s`, org, repo))
}
semver.Sort(tags)
fmt.Println(fmt.Sprintf(`::set-output name=tag::%s`, tags[len(tags)-1]))
}