diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 9517886..7027032 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -24,11 +24,30 @@ jobs: run: | sudo apt-get install -y ledger - - name: Build - run: go build -v ./... - - name: Test run: go test -v ./... env: GITHUB_URL: ${{ secrets.GIT_URL }} GITHUB_TOKEN: ${{ secrets.GIT_ACCESS_TOKEN }} + + - name: set up Docker Buildx + id: buildx + uses: docker/setup-buildx-action@v3 + + - name: build and publish image + if: ${{ github.ref == 'refs/heads/main' }} + env: + DOCKER_HUB_USER: ${{ secrets.DOCKER_HUB_USER }} + DOCKER_HUB_TOKEN: ${{ secrets.DOCKER_HUB_TOKEN }} + GITHUB_SHA: ${{ github.sha }} + GITHUB_REF: ${{ github.ref }} + run: | + ref="$(echo ${GITHUB_REF} | cut -d'/' -f3)" + version=${ref}-${GITHUB_SHA:0:7}-$(date -u +%Y-%m-%dT%H:%M:%S) + echo "GITHUB_REF=${GITHUB_REF}, GITHUB_SHA=${GITHUB_SHA}, GIT_BRANCH=${ref}" + echo "version=${version}" + echo ${DOCKER_HUB_TOKEN} | docker login -u ${DOCKER_HUB_USER} --password-stdin + docker buildx build --push \ + --build-arg VERSION=${version} \ + --platform linux/amd64,linux/arm/v7,linux/arm64 \ + -t ${DOCKER_HUB_USER}/teledger:${ref} . diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..0d25758 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,19 @@ +FROM golang:1.22.0-alpine as build + + +ADD ./ /repo +WORKDIR /repo + +ARG VERSION=docker-dev +RUN echo go version: `go version` +RUN echo build version: ${VERSION} + +RUN go build -o teledger -ldflags "-X main.version=${VERSION} -s -w" ./app/main.go + +FROM golang:1.22.0-alpine + +WORKDIR /srv +RUN apk add --no-cache ledger=~3.3.2 && echo ledger: `which ledger` +COPY --from=build /repo/teledger /srv/teledger + +CMD ["/srv/teledger"] diff --git a/app/bot/bot.go b/app/bot/bot.go index 73709d6..33683ee 100644 --- a/app/bot/bot.go +++ b/app/bot/bot.go @@ -25,6 +25,7 @@ type Opts struct { URL string `long:"url" env:"URL" required:"true" description:"bot url"` Debug bool `long:"debug" env:"DEBUG" description:"debug mode"` + Version string } @@ -49,6 +50,7 @@ func (opts *Opts) Execute() error { dispatcher.AddHandler(handlers.NewCommand("start", start)) dispatcher.AddHandler(handlers.NewCommand("bal", opts.bal)) + dispatcher.AddHandler(handlers.NewCommand("version", opts.vesrion)) dispatcher.AddHandler(handlers.NewMessage(nil, message)) // Start receiving updates. @@ -80,6 +82,14 @@ func start(b *gotgbot.Bot, ctx *ext.Context) error { return nil } +func (opts *Opts) vesrion(b *gotgbot.Bot, ctx *ext.Context) error { + msg := ctx.EffectiveMessage + log.Printf("[INFO] version request. user=%s\n", msg.From.Username) + if _, err := b.SendMessage(msg.Chat.Id, fmt.Sprintf("teledger v:%s", opts.Version), nil); err != nil { + return fmt.Errorf("unable to send message: %w", err) + } + return nil +} func (opts *Opts) bal(b *gotgbot.Bot, ctx *ext.Context) error { msg := ctx.EffectiveMessage diff --git a/app/main.go b/app/main.go index 873cc35..a52dc9c 100644 --- a/app/main.go +++ b/app/main.go @@ -15,6 +15,7 @@ var version = "dev" func main() { fmt.Printf("teledger v:%s\n", version) opts := bot.Opts{} + opts.Version = version _, err := flags.Parse(&opts) if err != nil { // if e, ok := err.(*flags.Error); ok && e.Type == flags.ErrHelp {