Include commit information in CI builds. Fixes #158

This commit is contained in:
Tulir Asokan 2020-06-03 20:32:53 +03:00
parent 7b75d763ed
commit 09385713de
5 changed files with 59 additions and 6 deletions

View file

@ -14,8 +14,9 @@ stages:
- apk add build-base olm-dev@edge_community
- mkdir -p .cache
- export GOPATH="$CI_PROJECT_DIR/.cache"
- export GO_LDFLAGS="-X main.Tag=$CI_COMMIT_TAG -X main.Commit=$CI_COMMIT_REF_NAME -X 'main.BuildTime=`date '+%b %_d %Y, %H:%M:%S'`'"
script:
- go build -o mautrix-whatsapp
- go build -ldflags "$GO_LDFLAGS" -o mautrix-whatsapp
artifacts:
paths:
- mautrix-whatsapp
@ -43,13 +44,14 @@ build static:
before_script:
- mkdir -p .cache
- export GOPATH="$CI_PROJECT_DIR/.cache"
- export GO_LDFLAGS="-X main.Tag=$CI_COMMIT_TAG -X main.Commit=$CI_COMMIT_REF_NAME -X 'main.BuildTime=`date '+%b %_d %Y, %H:%M:%S'`'"
variables:
CGO_ENABLED: 0
script:
- GOOS=linux GOARCH=amd64 go build -o mautrix-whatsapp_linux_amd64
- GOOS=linux GOARCH=arm64 go build -o mautrix-whatsapp_linux_arm64
- GOOS=linux GOARCH=arm go build -o mautrix-whatsapp_linux_arm
- GOOS=linux GOARCH=386 go build -o mautrix-whatsapp_linux_386
- GOOS=linux GOARCH=amd64 go build -ldflags "$GO_LDFLAGS" -o mautrix-whatsapp_linux_amd64
- GOOS=linux GOARCH=arm64 go build -ldflags "$GO_LDFLAGS" -o mautrix-whatsapp_linux_arm64
- GOOS=linux GOARCH=arm go build -ldflags "$GO_LDFLAGS" -o mautrix-whatsapp_linux_arm
- GOOS=linux GOARCH=386 go build -ldflags "$GO_LDFLAGS" -o mautrix-whatsapp_linux_386
artifacts:
paths:
- mautrix-whatsapp_linux_amd64

2
build.sh Executable file
View file

@ -0,0 +1,2 @@
#!/bin/sh
go build -ldflags "-X main.Tag=$(git describe --exact-match --tags 2>/dev/null) -X main.Commit=$(git rev-parse HEAD) -X 'main.BuildTime=`date '+%b %_d %Y, %H:%M:%S'`'" "$@"

View file

@ -105,6 +105,8 @@ func (handler *CommandHandler) CommandMux(ce *CommandEvent) {
handler.CommandLogoutMatrix(ce)
case "help":
handler.CommandHelp(ce)
case "version":
handler.CommandVersion(ce)
case "reconnect":
handler.CommandReconnect(ce)
case "disconnect":
@ -174,6 +176,21 @@ func (handler *CommandHandler) CommandDevTest(_ *CommandEvent) {
}
const cmdVersionHelp = `version - View the bridge version`
func (handler *CommandHandler) CommandVersion(ce *CommandEvent) {
version := fmt.Sprintf("%s+dev.unknown", Version)
if Tag == Version {
version = fmt.Sprintf("[%s](%s/releases/%s) (%s)", Version, URL, Tag, BuildTime)
} else if len(Commit) > 8 {
if !strings.HasSuffix(Version, "+dev") {
Version += "+dev"
}
version = fmt.Sprintf("%s.[%s](%s/commit/%s) (%s)", Version, Commit[:8], URL, Commit, BuildTime)
}
ce.Reply(fmt.Sprintf("[%s](%s) %s", Name, URL, version))
}
const cmdSetPowerLevelHelp = `set-pl [user ID] <power level> - Change the power level in a portal room. Only for bridge admins.`
func (handler *CommandHandler) CommandSetPowerLevel(ce *CommandEvent) {

32
main.go
View file

@ -20,6 +20,7 @@ import (
"fmt"
"os"
"os/signal"
"strings"
"sync"
"syscall"
"time"
@ -38,11 +39,30 @@ import (
"maunium.net/go/mautrix-whatsapp/types"
)
var (
// These are static
Name = "mautrix-whatsapp"
URL = "https://github.com/tulir/mautrix-whatsapp"
// This is changed when making a release
Version = "0.1.0+dev"
// These are filled at build time with the -X linker flag
Tag = "unknown"
Commit = "unknown"
BuildTime = "unknown"
)
func init() {
if len(Tag) > 0 && Tag[0] == 'v' {
Tag = Tag[1:]
}
}
var configPath = flag.MakeFull("c", "config", "The path to your config file.", "config.yaml").String()
//var baseConfigPath = flag.MakeFull("b", "base-config", "The path to the example config file.", "example-config.yaml").String()
var registrationPath = flag.MakeFull("r", "registration", "The path where to save the appservice registration.", "registration.yaml").String()
var generateRegistration = flag.MakeFull("g", "generate-registration", "Generate registration and quit.", "false").Bool()
var version = flag.MakeFull("v", "version", "View bridge version and quit.", "false").Bool()
var ignoreUnsupportedDatabase = flag.Make().LongKey("ignore-unsupported-database").Usage("Run even if database is too new").Default("false").Bool()
var migrateFrom = flag.Make().LongKey("migrate-db").Usage("Source database type and URI to migrate from.").Bool()
var wantHelp, _ = flag.MakeHelpFlag()
@ -347,6 +367,18 @@ func (bridge *Bridge) Main() {
} else if *migrateFrom {
bridge.MigrateDatabase()
return
} else if *version {
if Tag == Version {
fmt.Printf("%s %s (%s)\n", Name, Tag, BuildTime)
} else if len(Commit) > 8 {
if !strings.HasSuffix(Version, "+dev") {
Version += "+dev"
}
fmt.Printf("%s %s.%s (%s)\n", Name, Version, Commit[:8], BuildTime)
} else {
fmt.Printf("%s %s+dev.unknown\n", Name, Version)
}
return
}
bridge.Init()

View file

@ -235,7 +235,7 @@ func (user *User) Connect(evenIfNoSession bool) bool {
return false
}
user.Conn = whatsappExt.ExtendConn(conn)
_ = user.Conn.SetClientName("Mautrix-WhatsApp bridge", "mx-wa", "0.1.0")
_ = user.Conn.SetClientName("Mautrix-WhatsApp bridge", "mx-wa", Version)
user.log.Debugln("WhatsApp connection successful")
user.Conn.AddHandler(user)
return user.RestoreSession()