在部署期间将git修订版本号插入文件 [英] Insert git revision number into files during deployment
问题描述
我想在html和javascript文件中嵌入一个唯一的版本号,原因有两个。第一个原因是我想确保在加载javascript文件时,如果有新版本,系统不会尝试使用缓存,但是如果我可以使用类似于
< script src =/ app.js?v1.0.1-1-95425234>< / script>
我几乎可以确定这绝不会发生。第二个原因是对于我的远程错误日志记录,最好知道哪个版本的系统有错误,以便我可以适当地修复它,但不必在每次部署时手动更新此版本号。
我一路上遇到了 git describe
,我认为它的输出值非常适合这个。
基本上我想在我的javascript和html文件(比如 GIT-CURRENT-VERSION
)中取一个关键字并替换它输出为 git describe
我为我的部署方法使用了fabric,所以这会使很多在抓取最新版本的git之后,在服务器端做的感觉。
我怀疑 sed $ c的组合$ c>和
grep
会在这里实现,如果我能够找到一个这样的一行代码,那么添加到我的部署脚本将非常容易。 你的已部署的git树的根:
export VERSION =$(git describe)
find。 -type f -print0 | xargs -0 sed -is / GIT-CURRENT -VERSION / $ VERSION / g
(这只是粗略的测试 - 谨慎使用。)
请注意,这将使您的树进行局部修改。
What I'm doing I think should be fairly simple, but I can't seem to figure out how to make it work.
I want to embed a unique version number in html and javascript files, for two reasons. The first reason is that I want to ensure that when the javascript file is loaded, the system doesn't attempt to use the cache if there is a new version, but if I could call it using something like
<script src="/app.js?v1.0.1-1-95425234"></script>
I could almost certainly ensure this never happens. The second reason is for my remote error logging, it would be good to know what version of the system is having errors so that I can look into fixing it appropriately, but without having to update this version number manually on every deployment.
I came across git describe
along the way, and I think that it's output value would be really well suited for this.
Basically I want to take a key in my javascript and html files like GIT-CURRENT-VERSION
and replace it with the output of git describe
I'm using fabric for my deployment method, so this would make a lot of sense to do on the server side, after we have grabbed the latest version of git.
I suspect some combination of sed
and grep
would do the trick here, and if I could figure out a one liner for this, it would be really easy to add to my deployment script.
Thanks in advance!
You could do something like the following from the root of your deployed git tree:
export VERSION="$(git describe)"
find . -type f -print0 | xargs -0 sed -i "s/GIT-CURRENT-VERSION/$VERSION/g"
(That's only had cursory testing - use with caution.)
Note that that will leave your tree with local modifications.
这篇关于在部署期间将git修订版本号插入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!