获取Heroku中golang-buildpack的hook脚本中的环境变量的方法
时间:2024-02-19
浏览:292
作者:佚名
在Golang实战开发的过程中,我们经常会遇到一些这样那样的问题,然后要卡好半天,等问题解决了才发现原来一些细节知识点还是没有掌握好。今天golang学习网就整理分享《获取Heroku中golang-buildpack的hook脚本中的环境变量的方法》,聊聊,希望可以帮助到正在努力赚钱的你。
当我构建数据库迁移自动化脚本时,我遇到了一个非常奇怪的问题。我按照官方文档:
编译前/编译后挂钩 如果文件 bin/go-pre-compile 或 bin/go-post-compile 存在并且可执行,那么它将在存储库编译之前(go-pre-compile)或编译之后(go-post-compile)执行)。 因为buildpack将编译后的可执行文件安装到bin中,所以如果按指定安装的话,go-post-compile钩子可以用go编写(见上文)。
然后我制作了下面的 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脚本时,进程似乎无法获取环境变量。
构建包编译应用程序后,该过程与配置变量配合良好。
那么你有什么想法吗?
解决方案
经过一番研究,我找到了运行数据库迁移自动化的解决方案。开发人员应该在 Heroku 的 release 阶段 中运行此类命令。可以查看官方文档:https://devcenter.heroku.com/articles/release-phase
今天带大家了解了的相关知识,希望对你有所帮助;关于Golang的技术知识我们会一点点深入介绍,欢迎大家关注golang学习网公众号,一起学习编程~
本文内容来源于互联网,如有侵权请联系删除。
近期热门
站内热门
最新文章
热门专题
