mirror of
https://github.com/tiennm99/tiennm99bot.git
synced 2026-10-13 18:16:13 +00:00
fix(deploynotify): read commit SHA from Coolify SOURCE_COMMIT runtime env
Baking SOURCE_COMMIT as a Docker build arg never worked: Coolify exposes it as a runtime env var, not a build arg, so gitSHA was always empty and the owner DM was skipped. Read SOURCE_COMMIT from the container env at startup (falling back to the ldflags-baked gitSHA for local builds), forward it via the compose environment, and drop the dead build-arg baking.
This commit is contained in:
1 parent
1fa549fa18
commit
0abd33cb42
6 files changed
+55
-22
No files matched your search
+17
-4
@@ -36,11 +36,22 @@ import (
|
||||
"github.com/tiennm99/miti99bot/internal/telegram"
|
||||
)
|
||||
|
||||
// gitSHA is populated at build time via `-ldflags "-X main.gitSHA=<sha>"`
|
||||
// (see Makefile). Empty value means the binary was built without that flag —
|
||||
// deploynotify treats it as a signal to stay silent.
|
||||
// gitSHA is the local-build fallback, populated via `-ldflags "-X
|
||||
// main.gitSHA=<sha>"` (see Makefile). On Coolify the commit comes from the
|
||||
// SOURCE_COMMIT runtime env instead (resolveCommitSHA prefers it). Empty from
|
||||
// both sources means deploynotify stays silent.
|
||||
var gitSHA string
|
||||
|
||||
// resolveCommitSHA returns the commit identifier for the deploy notification.
|
||||
// Coolify injects SOURCE_COMMIT into the container environment at runtime, so
|
||||
// prefer it; fall back to the ldflags-baked gitSHA for local builds.
|
||||
func resolveCommitSHA(envSourceCommit string) string {
|
||||
if s := strings.TrimSpace(envSourceCommit); s != "" {
|
||||
return s
|
||||
}
|
||||
return gitSHA
|
||||
}
|
||||
|
||||
// factories is the static module catalog. Adding a new module is a one-line
|
||||
// change here. Lives in main rather than the modules package to avoid an
|
||||
// import cycle (modules → util → modules).
|
||||
@@ -157,7 +168,7 @@ func main() {
|
||||
Bot: b,
|
||||
Store: deploynotify.NewStore(provider.Collection("deploynotify")),
|
||||
OwnerID: cfg.BotOwnerID,
|
||||
GitSHA: gitSHA,
|
||||
GitSHA: resolveCommitSHA(cfg.SourceCommit),
|
||||
})
|
||||
|
||||
handler := server.New()
|
||||
@@ -260,6 +271,7 @@ func buildProvider(ctx context.Context, cfg config) (storage.Provider, func(), e
|
||||
type config struct {
|
||||
Port string
|
||||
TelegramBotToken string
|
||||
SourceCommit string // Coolify-injected commit SHA (runtime env) for deploynotify
|
||||
GeminiAPIKey string
|
||||
GoldPriceAPIURL string
|
||||
GoldFXAPIURL string
|
||||
@@ -299,6 +311,7 @@ func loadConfig() config {
|
||||
return config{
|
||||
Port: port,
|
||||
TelegramBotToken: envMap["TELEGRAM_BOT_TOKEN"],
|
||||
SourceCommit: envMap["SOURCE_COMMIT"],
|
||||
GeminiAPIKey: envMap["GEMINI_API_KEY"],
|
||||
GoldPriceAPIURL: envMap["GOLD_PRICE_API_URL"],
|
||||
GoldFXAPIURL: envMap["GOLD_FX_API_URL"],
|
||||
|
||||
@@ -7,6 +7,28 @@ import (
|
||||
"github.com/tiennm99/miti99bot/internal/storage"
|
||||
)
|
||||
|
||||
func TestResolveCommitSHA(t *testing.T) {
|
||||
prev := gitSHA
|
||||
defer func() { gitSHA = prev }()
|
||||
|
||||
// Coolify runtime env wins over the baked fallback.
|
||||
gitSHA = "baked"
|
||||
if got := resolveCommitSHA(" runtime-sha "); got != "runtime-sha" {
|
||||
t.Errorf("env present: got %q, want trimmed runtime-sha", got)
|
||||
}
|
||||
|
||||
// Empty env falls back to the ldflags-baked value.
|
||||
if got := resolveCommitSHA(""); got != "baked" {
|
||||
t.Errorf("env empty: got %q, want baked fallback", got)
|
||||
}
|
||||
|
||||
// Neither source set → empty (deploynotify stays silent).
|
||||
gitSHA = ""
|
||||
if got := resolveCommitSHA(" "); got != "" {
|
||||
t.Errorf("both empty: got %q, want empty", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFactoriesIncludesGoldAndCoin(t *testing.T) {
|
||||
catalog := factories()
|
||||
if catalog["gold"] == nil {
|
||||
|
||||
Reference in new issue
Block a user