Skip to content

Commit

Permalink
chore: support github actions
Browse files Browse the repository at this point in the history
  • Loading branch information
wweir committed Sep 27, 2024
1 parent d9c01b9 commit 01b7ac1
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 14 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/master.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Go
on:
push:
branches:
- master
- main
jobs:
build:
name: Build
runs-on: ubuntu-latest
steps:
- uses: actions/setup-go@v5
with:
go-version: ^1.23
check-latest: true
cache-dependency-path: |
**/go.sum
**/go.mod
- uses: actions/checkout@v4
- name: test and build
run: |
make
41 changes: 41 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: Release
on:
push:
tags:
- "v*.*.*"
jobs:
build:
name: Build
runs-on: ubuntu-latest
steps:
- uses: actions/setup-go@v5
with:
go-version: ^1.23
check-latest: true
cache-dependency-path: |
**/go.sum
**/go.mod
- uses: actions/checkout@v4
- name: build matrix
run: |
make build GO='GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go'
tar czvf contatto-linux-amd64.tar.gz ./bin/ ./etc/contatto.toml
make clean
make build GO='GOOS=linux GOARCH=arm CGO_ENABLED=0 go'
tar czvf contatto-linux-arm.tar.gz ./bin/ ./etc/contatto.toml
make clean
make build GO='GOOS=linux GOARCH=arm64 CGO_ENABLED=0 go'
tar czvf contatto-linux-arm64.tar.gz ./bin/ ./etc/contatto.toml
make clean
make build GO='GOOS=linux GOARCH=mips CGO_ENABLED=0 go'
tar czvf contatto-linux-mips.tar.gz ./bin/ ./etc/contatto.toml
make clean
make build GO='GOOS=linux GOARCH=mipsle CGO_ENABLED=0 go'
tar czvf contatto-linux-mipsle.tar.gz ./bin/ ./etc/contatto.toml
make clean
- uses: ncipollo/release-action@v1
with:
artifacts: "*.tar.gz"
token: ${{ secrets.GITHUB_TOKEN }}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@ go.work.sum
**.env
/bin
/config.toml
/contatto.toml
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ build: test
-X main.date=$(shell date +%Y-%m-%d)" \
-o bin/contatto .
run: build
./bin/contatto proxy -c config.toml
./bin/contatto proxy -c contatto.toml

clean:
rm -f ./bin/contatto
2 changes: 1 addition & 1 deletion conf/config.go → etc/config.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package conf
package etc

import (
"bytes"
Expand Down
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"log"

"github.com/alecthomas/kong"
"github.com/wweir/contatto/conf"
"github.com/wweir/contatto/etc"
)

var cli struct {
Expand All @@ -20,7 +20,7 @@ func init() {
func main() {
ctx := kong.Parse(&cli,
kong.UsageOnError(),
kong.Description(fmt.Sprintf(`Contatto %s (%s %s)`, conf.Version, conf.Branch, conf.Date)),
kong.Description(fmt.Sprintf(`Contatto %s (%s %s)`, etc.Version, etc.Branch, etc.Date)),
)
if err := ctx.Run(); err != nil {
log.Fatalf("run failed: %v\n", err)
Expand Down
20 changes: 10 additions & 10 deletions proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

"github.com/containerd/containerd/v2/core/remotes/docker"
"github.com/julienschmidt/httprouter"
"github.com/wweir/contatto/conf"
"github.com/wweir/contatto/etc"
)

type ProxyCmd struct {
Expand All @@ -23,7 +23,7 @@ type ProxyCmd struct {
}

func (c *ProxyCmd) Run() error {
if err := conf.InitConfig(c.Config); err != nil {
if err := etc.InitConfig(c.Config); err != nil {
return err
}

Expand All @@ -48,9 +48,9 @@ func (c *ProxyCmd) Run() error {
host = "docker.io"
}

rule, ok := conf.RuleHostM[host]
rule, ok := etc.RuleHostM[host]
if !ok { // no mapping rule, directly forward to the registry
if reg, ok := conf.RegHostM[host]; ok {
if reg, ok := etc.RegHostM[host]; ok {
r.Out.URL.Host = reg.Host
if reg.Insecure {
r.Out.URL.Scheme = "http"
Expand All @@ -66,7 +66,7 @@ func (c *ProxyCmd) Run() error {
dstPat := &ImagePattern{}

{ // rewrite host, scheme, query
dstReg := conf.RegM[conf.Config.MirrorRegistry]
dstReg := etc.RegM[etc.Config.MirrorRegistry]
dstPat.Registry = dstReg.Host

if dstReg.Insecure {
Expand Down Expand Up @@ -131,8 +131,8 @@ func (c *ProxyCmd) Run() error {
var raw, mirror ImagePattern
raw.ParseImage(w.Request.Header.Get("Contatto-Raw-Image"))
mirror.ParseImage(w.Request.Header.Get("Contatto-Mirror-Image"))
if conf.OnMissing != nil {
cmdline, err := conf.OnMissing(map[string]any{
if etc.OnMissing != nil {
cmdline, err := etc.OnMissing(map[string]any{
"Raw": raw, "Mirror": mirror, "raw": raw.String(), "mirror": mirror.String(),
})
if err != nil {
Expand Down Expand Up @@ -163,18 +163,18 @@ func (c *ProxyCmd) Run() error {
return nil
}

return http.ListenAndServe(conf.Config.Addr, proxy)
return http.ListenAndServe(etc.Config.Addr, proxy)
}

func (c *ProxyCmd) AuthCreds(host string) (string, string, error) {
slog.Info("read auth creds", "registry", host)

reg := conf.RegHostM[host]
reg := etc.RegHostM[host]
if reg.UserName != "" && reg.Password != "" {
return reg.UserName, reg.Password, nil
}

if auth, ok := conf.DockerAuth[host]; ok {
if auth, ok := etc.DockerAuth[host]; ok {
return auth.UserName, auth.Password, nil
}

Expand Down

0 comments on commit 01b7ac1

Please sign in to comment.