Skip to content

Commit 040c809

Browse files
authored
Merge pull request #27 from grishy/sg-fix-build-cli-version
Bumpup Go and CI actions + fix version print
2 parents 8343e22 + 32dd6b2 commit 040c809

File tree

8 files changed

+174
-59
lines changed

8 files changed

+174
-59
lines changed

.github/workflows/commit.yml

+54-17
Original file line numberDiff line numberDiff line change
@@ -9,33 +9,70 @@ permissions:
99
contents: read
1010

1111
jobs:
12-
golangci:
13-
runs-on: ubuntu-latest
12+
test:
13+
runs-on: ubuntu-24.04
1414
steps:
1515
- uses: actions/checkout@v4
1616
- uses: actions/setup-go@v5
1717
with:
18-
go-version: "1.23.3"
19-
cache: false
18+
go-version: "1.23.5"
2019

21-
- name: golangci-lint
22-
uses: golangci/golangci-lint-action@v6
20+
- run: go mod verify
21+
22+
- run: go test -v ./...
23+
24+
- uses: golangci/golangci-lint-action@v6
2325
with:
24-
version: v1.62.0
25-
build_test:
26-
runs-on: ubuntu-latest
26+
version: v1.63.4
27+
28+
build:
29+
runs-on: ubuntu-24.04
2730
steps:
2831
- uses: actions/checkout@v4
2932
- uses: actions/setup-go@v5
3033
with:
31-
go-version: "1.23.3"
32-
cache: false
34+
go-version: "1.23.5"
3335

34-
- name: Verify dependencies
35-
run: go mod verify
36+
- name: Set up QEMU for cross-compilation of Docker images
37+
uses: docker/setup-qemu-action@v3
3638

37-
- name: Build
38-
run: go build -v ./...
39+
- name: Set up Docker Buildx
40+
uses: docker/setup-buildx-action@v3
3941

40-
- name: Test
41-
run: go test -v ./...
42+
- run: go mod download
43+
44+
- uses: goreleaser/goreleaser-action@v6
45+
id: goreleaser
46+
with:
47+
distribution: goreleaser
48+
version: "~> v2"
49+
args: release --skip=publish --clean --snapshot
50+
env:
51+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
52+
53+
- name: Extract metadata (tags, labels) for Docker base on Goreleaser
54+
id: meta
55+
uses: docker/metadata-action@v5
56+
with:
57+
images: |
58+
ghcr.io/${{ github.repository }}
59+
tags: |
60+
type=raw,value=${{ fromJSON(steps.goreleaser.outputs.metadata).version }}
61+
62+
- name: Test build of Docker images
63+
uses: docker/build-push-action@v5
64+
with:
65+
context: .
66+
push: false
67+
platforms: |
68+
linux/amd64
69+
linux/arm64
70+
linux/arm/v7
71+
tags: ${{ steps.meta.outputs.tags }}
72+
labels: ${{ steps.meta.outputs.labels }}
73+
build-args: |
74+
VERSION=${{ fromJSON(steps.goreleaser.outputs.metadata).version }}
75+
COMMIT=${{ fromJSON(steps.goreleaser.outputs.metadata).commit }}
76+
DATE=${{ fromJSON(steps.goreleaser.outputs.metadata).date }}
77+
cache-from: type=gha
78+
cache-to: type=gha,mode=max

.github/workflows/release.yml

+30-19
Original file line numberDiff line numberDiff line change
@@ -11,49 +11,60 @@ permissions:
1111

1212
jobs:
1313
release:
14-
runs-on: ubuntu-latest
14+
runs-on: ubuntu-24.04
1515
steps:
16-
- uses: actions/checkout@v4
16+
- uses: actions/cheчckout@v4
1717
with:
1818
fetch-depth: 0
1919

2020
- uses: actions/setup-go@v5
2121
with:
22-
go-version: "1.23.3"
22+
go-version: "1.23.5"
2323

2424
- name: Set up QEMU for cross-compilation of Docker images
25-
uses: docker/setup-qemu-action@v2
25+
uses: docker/setup-qemu-action@v3
2626

2727
- name: Set up Docker Buildx
28-
uses: docker/setup-buildx-action@v2
28+
uses: docker/setup-buildx-action@v3
2929

3030
- name: Log in to the Container registry
31-
uses: docker/login-action@v2
31+
uses: docker/login-action@v3
3232
with:
3333
registry: ghcr.io
3434
username: ${{ github.actor }}
3535
password: ${{ secrets.GITHUB_TOKEN }}
3636

37-
- name: Extract metadata (tags, labels) for Docker
38-
id: meta
39-
uses: docker/metadata-action@v4
40-
with:
41-
images: |
42-
ghcr.io/${{ github.repository }}
43-
44-
- uses: goreleaser/goreleaser-action@v4
37+
- uses: goreleaser/goreleaser-action@v6
38+
id: goreleaser
4539
with:
4640
distribution: goreleaser
47-
version: latest
41+
version: "~> v2"
4842
args: release --clean
4943
env:
5044
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
5145

52-
- name: Build and push Docker images
53-
uses: docker/build-push-action@v4
46+
- name: Extract metadata (tags, labels) for Docker base on Goreleaser
47+
id: meta
48+
uses: docker/metadata-action@v5
49+
with:
50+
images: |
51+
ghcr.io/${{ github.repository }}
52+
tags: |
53+
type=raw,value=${{ fromJSON(steps.goreleaser.outputs.metadata).version }}
54+
55+
- name: Test build of Docker images
56+
uses: docker/build-push-action@v5
5457
with:
5558
context: .
56-
push: true
57-
platforms: linux/amd64,linux/arm64,linux/arm/v7
59+
platforms: |
60+
linux/amd64
61+
linux/arm64
62+
linux/arm/v7
5863
tags: ${{ steps.meta.outputs.tags }}
5964
labels: ${{ steps.meta.outputs.labels }}
65+
build-args: |
66+
VERSION=${{ fromJSON(steps.goreleaser.outputs.metadata).version }}
67+
COMMIT=${{ fromJSON(steps.goreleaser.outputs.metadata).commit }}
68+
DATE=${{ fromJSON(steps.goreleaser.outputs.metadata).date }}
69+
cache-from: type=gha
70+
cache-to: type=gha,mode=max

.golangci.yml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
run:
2+
timeout: 5m

.goreleaser.yaml

+22-7
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@ version: 2
55
report_sizes: true
66

77
builds:
8-
# https://goreleaser.com/customization/build/
8+
# https://goreleaser.com/customization/builds/go/
99
- env:
1010
- CGO_ENABLED=0
1111
flags:
1212
- -trimpath
1313
ldflags:
1414
- -w -s
15+
- -X main.version={{.Version}}
16+
- -X main.commit={{.ShortCommit}}
17+
- -X main.date={{.CommitDate}}
1518
goos:
1619
- linux
1720
- darwin
@@ -20,15 +23,11 @@ builds:
2023
- "amd64"
2124
- "arm"
2225
- "arm64"
23-
goarm:
24-
- "6"
25-
- "7"
2626
mod_timestamp: "{{ .CommitTimestamp }}"
2727

2828
archives:
2929
# https://goreleaser.com/customization/archive/
30-
- format: tar.gz
31-
name_template: "{{ .ProjectName }}_v{{ .Version }}_{{ .Os }}_{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}"
30+
- formats: ["tar.gz"]
3231

3332
checksum:
3433
# https://goreleaser.com/customization/checksum/
@@ -39,7 +38,7 @@ release:
3938
target_commitish: "{{ .Commit }}"
4039
mode: replace
4140
header: |
42-
## ℹ️ For information on how to install and use please see `README.md`
41+
## ℹ️ For information on how to install and use please see [README.md](https://github.com/grishy/go-avahi-cname)
4342
footer: |
4443
Your PRs & issues are welcome! Thanks 😉
4544
disable: false
@@ -48,3 +47,19 @@ release:
4847
changelog:
4948
# https://goreleaser.com/customization/changelog/
5049
sort: asc
50+
filters:
51+
exclude:
52+
- "^docs:"
53+
- "^test:"
54+
- "^ci:"
55+
- Merge pull request
56+
- Merge branch
57+
groups:
58+
- title: "New Features"
59+
regexp: "^.*feat[(\\w)]*:+.*$"
60+
order: 0
61+
- title: "Bug fixes"
62+
regexp: "^.*fix[(\\w)]*:+.*$"
63+
order: 1
64+
- title: Others
65+
order: 999

Dockerfile

+15-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,27 @@
11
# syntax=docker/dockerfile:1
22

3+
# NOTE: I build images for multi-arch because goreleaser works strange with multi-stage builds
4+
# Need a lot of additional configuration to build multi-arch images
5+
36
# STAGE 1: building the executable
4-
FROM docker.io/golang:1.23.3-alpine3.20 as builder
7+
FROM docker.io/golang:1.23.5-alpine3.20 AS builder
58
WORKDIR /build
69

10+
ARG VERSION
11+
ARG COMMIT
12+
ARG DATE
13+
714
COPY go.mod go.sum ./
815
RUN go mod download
916

10-
ENV CGO_ENABLED=0
1117
COPY . .
12-
RUN go build -ldflags="-w -s" -o /go-avahi-cname
18+
RUN CGO_ENABLED=0 \
19+
go build \
20+
-ldflags="-w -s \
21+
-X main.version='${VERSION}' \
22+
-X main.commit='${COMMIT}' \
23+
-X main.date='${DATE}'" \
24+
-o /go-avahi-cname
1325

1426
# STAGE 2: build the container to run
1527
FROM scratch

go.mod

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
module github.com/grishy/go-avahi-cname
22

3-
go 1.23.3
3+
go 1.23.5
44

55
require (
6-
github.com/earthboundkid/versioninfo/v2 v2.24.1
6+
github.com/caarlos0/go-version v0.2.0
77
github.com/godbus/dbus/v5 v5.1.0
88
github.com/holoplot/go-avahi v1.0.1
99
github.com/lmittmann/tint v1.0.6

go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
github.com/caarlos0/go-version v0.2.0 h1:TTD5dF3PBAtRHbfCKRE173SrVVpbE0yX95EDQ4BwTGs=
2+
github.com/caarlos0/go-version v0.2.0/go.mod h1:X+rI5VAtJDpcjCjeEIXpxGa5+rTcgur1FK66wS0/944=
13
github.com/cpuguy83/go-md2man/v2 v2.0.6 h1:XJtiaUW6dEEqVuZiMTn1ldk455QWwEIsMIJlo5vtkx0=
24
github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
3-
github.com/earthboundkid/versioninfo/v2 v2.24.1 h1:SJTMHaoUx3GzjjnUO1QzP3ZXK6Ee/nbWyCm58eY3oUg=
4-
github.com/earthboundkid/versioninfo/v2 v2.24.1/go.mod h1:VcWEooDEuyUJnMfbdTh0uFN4cfEIg+kHMuWB2CDCLjw=
55
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
66
github.com/godbus/dbus/v5 v5.1.0 h1:4KLkAxT3aOY8Li4FRJe/KvhoNFFxo0m6fNuFUO8QJUk=
77
github.com/godbus/dbus/v5 v5.1.0/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=

main.go

+47-9
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,25 @@ import (
99
"syscall"
1010
"time"
1111

12-
"github.com/earthboundkid/versioninfo/v2"
12+
goversion "github.com/caarlos0/go-version"
1313
"github.com/lmittmann/tint"
1414
"github.com/mattn/go-isatty"
1515
"github.com/urfave/cli/v2"
1616

1717
"github.com/grishy/go-avahi-cname/cmd"
1818
)
1919

20-
const forceExitTimeout = 3 * time.Second
20+
const (
21+
forceExitTimeout = 5 * time.Second
22+
appName = "go-avahi-cname"
23+
)
24+
25+
// Version information set during build
26+
var (
27+
version = "dev"
28+
commit = "none"
29+
date = "unknown"
30+
)
2131

2232
func main() {
2333
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
@@ -28,24 +38,41 @@ func main() {
2838
if err := run(ctx); err != nil {
2939
fmt.Println("Error:")
3040
fmt.Printf(" > %+v\n", err)
41+
os.Exit(1)
3142
}
3243
}
3344

34-
// run starts the CLI application
45+
// run starts and configures the CLI application
3546
func run(ctx context.Context) error {
47+
cli.VersionPrinter = func(c *cli.Context) {
48+
fmt.Print(buildVersion().String())
49+
}
50+
3651
app := &cli.App{
37-
Name: "go-avahi-cname",
38-
Usage: "A tool for publishing CNAME records with Avahi",
39-
Version: versioninfo.Short(),
52+
Name: appName,
53+
Usage: "Create local domain names using Avahi daemon",
54+
Version: version,
55+
Description: `A tool that helps you create local domain names for your computer by using the Avahi daemon.
56+
57+
It works in two ways:
58+
1. Automatic mode (use 'subdomain' command):
59+
Any subdomain you try to use (like myapp.computer.local) will automatically point to your computer
60+
61+
2. Manual mode (use 'cname' command):
62+
You can create your own domain names that point to your computer and keep them active
63+
64+
Need help? Visit https://github.com/grishy/go-avahi-cname`,
4065
Authors: []*cli.Author{{
41-
Name: "grishy",
66+
Name: "Sergei G.",
67+
4268
}},
4369
Flags: []cli.Flag{
4470
&cli.BoolFlag{
4571
Name: "debug",
4672
Aliases: []string{"d"},
4773
Usage: "Enable debug logging",
4874
EnvVars: []string{"DEBUG"},
75+
Value: false,
4976
},
5077
},
5178
Before: setupLogger,
@@ -58,7 +85,7 @@ func run(ctx context.Context) error {
5885
return app.Run(os.Args)
5986
}
6087

61-
// handleGracefulShutdown handles graceful shutdown with timeout
88+
// handleGracefulShutdown manages graceful shutdown with timeout
6289
func handleGracefulShutdown(ctx context.Context) {
6390
<-ctx.Done()
6491
slog.Info("initiating graceful shutdown...")
@@ -74,7 +101,7 @@ func handleGracefulShutdown(ctx context.Context) {
74101
}
75102
}
76103

77-
// setupLogger configures the global logger with appropriate settings
104+
// setupLogger configures the global structured logger with appropriate settings
78105
func setupLogger(c *cli.Context) error {
79106
w := os.Stdout
80107
level := slog.LevelInfo
@@ -92,3 +119,14 @@ func setupLogger(c *cli.Context) error {
92119

93120
return nil
94121
}
122+
123+
// buildVersion constructs version information for the application
124+
func buildVersion() goversion.Info {
125+
return goversion.GetVersionInfo(
126+
func(i *goversion.Info) {
127+
i.GitCommit = commit
128+
i.BuildDate = date
129+
i.GitVersion = version
130+
},
131+
)
132+
}

0 commit comments

Comments
 (0)