如何获取Heroku中golang-buildpack的hook脚本中的环境变量?

How to get the environment variables in the hook script of the golang-buildpack in Heroku?

我在构建数据库迁移自动化脚本时遇到了一个非常奇怪的问题。我按照官方文档:

Pre/Post Compile Hooks

If the file bin/go-pre-compile or bin/go-post-compile exists and is executable then it will be executed either before compilation (go-pre-compile) of the repo, or after compilation (go-post-compile).

Because the buildpack installs compiled executables to bin, the go-post-compile hook can be written in go if it's installed by the specified (see above).

然后我制作了下面的 go-post-compile 脚本:

var (
    appURI := os.Getenv("APP_URI")
    databaseURL := os.Getenv("DATABASE_URL")
)

func main() {
    fmt.Printf("app_uri: %s\n", appURI)
    fmt.Printf("database_url: %s\n", databaseURL)
    sourcePath := fmt.Sprintf("file://%s/db/migrations", appURI)
    m, err := migrate.New(sourcePath, databaseURL)
    if err != nil {
        logger.Error("Failed to establish the connection of the migration.", err)
    }
    err = m.Up()
    if err != nil && err.Error() == "no change" {
        fmt.Println("  > NOTE: There is no change related to the operation of the migration.")
        return
    } else if err != nil {
        logger.Error("Failed to establish the connection of the migration.", err)
    }
    fmt.Println("  > Done.")
}

当我将代码推送到 heroku 时,出现以下错误:

remote: -----> Running bin/go-post-compile hook
remote: app_uri: 
remote: database_url: ?sslmode=require
remote: panic: runtime error: invalid memory address or nil pointer dereference
remote: [signal SIGSEGV: segmentation violation code=0x1 addr=0x58 pc=0x503b06]
remote: 

似乎进程在 运行 go-post-compile 脚本时无法获取环境变量。

在 buildpack 编译应用程序后,该过程与配置变量配合得很好。

那你有什么想法吗?

经过一番研究,我找到了关于运行数据库迁移自动化的解决方案。开发人员应该 运行 在 Heroku 中 release phase 这些类型的命令。可以查看官方文档:https://devcenter.heroku.com/articles/release-phase