应用程序自动构建版本控制

huangapple go评论78阅读模式
英文:

Application auto build versioning

问题

每次编译Go应用程序时,是否可以自动递增次要版本号?

我想在我的程序中设置一个版本号,其中包含一个自动递增的部分:

$ myapp -version
MyApp版本0.5.132

其中0.5是我设置的版本号,132是每次编译二进制文件时自动递增的值。

在Go中是否可能实现这一点?

英文:

Is it possible to increment a minor version number automatically each time a Go app is compiled?

I would like to set a version number inside my program, with an autoincrementing section:

$ myapp -version
MyApp version 0.5.132

Being 0.5 the version number I set, and 132 a value that increments automatically each time the binary is compiled.

Is this possible in Go?

答案1

得分: 378

Go链接器(go tool link)有一个选项可以设置未初始化的字符串变量的值:

-X importpath.name=value
	将导入路径中名为name的字符串变量的值设置为value。
	请注意,在Go 1.5之前,此选项接受两个单独的参数。
	现在它接受一个在第一个等号上分割的参数。

作为构建过程的一部分,您可以使用此选项设置一个版本字符串变量。您可以通过go工具使用-ldflags传递此选项。例如,给定以下源文件:

package main

import "fmt"

var xyz string

func main() {
    fmt.Println(xyz)
}

然后:

$ go run -ldflags "-X main.xyz=abc" main.go
abc

为了在构建时将main.minversion设置为构建日期和时间:

go build -ldflags "-X main.minversion=`date -u +.%Y%m%d.%H%M%S`" service.go

如果以这种方式编译而没有初始化main.minversion,它将包含空字符串。

英文:

The Go linker (go tool link) has an option to set the value of an uninitialised string variable:

> -X importpath.name=value
> Set the value of the string variable in importpath named name to

value.
Note that before Go 1.5 this option took two separate arguments.
Now it takes one argument split on the first = sign.

As part of your build process, you could set a version string variable using this. You can pass this through the go tool using -ldflags. For example, given the following source file:

package main

import "fmt"

var xyz string

func main() {
    fmt.Println(xyz)
}

Then:

$ go run -ldflags "-X main.xyz=abc" main.go
abc

In order to set main.minversion to the build date and time when building:

go build -ldflags "-X main.minversion=`date -u +.%Y%m%d.%H%M%S`" service.go

If you compile without initializing main.minversion in this way, it will contain the empty string.

答案2

得分: 42

使用ldflagsmain包中设置变量:

使用文件main.go

package main

import "fmt"

var (
    version string
    build   string
)

func main() {
    fmt.Println("version=", version)
    fmt.Println("build=", build)
}

然后运行:

go run \
  -ldflags "-X main.version=1.0.0 -X main.build=12082019" \ 
  main.go

构建:

go build -o mybinary \
  -ldflags "-X main.version=1.0.0 -X 'main.build=$(date)'" \ 
  main.go

使用ldflagsnon-main包中设置变量:

使用文件config.go

package config

import "fmt"

var (
    Version string
)

func LogVersion() {
    fmt.Println("version=", Version)
}

还需要文件main.go

package main

import (
    "fmt"
    "github.com/user/repo/config"
)

func main() {
    config.LogVersion()
}

首先构建您的二进制文件:

go build -o mybinary main.go 

找到要设置的变量名称的完整路径:

go tool nm <path_to_binary> | grep Version

再次运行和构建二进制文件,但使用ldflags

go run \
  -ldflags "-X github.com/user/repo/config.Version=1.0.0" \
  main.go --version       


go build -o mybinary \
  -ldflags "-X github.com/user/repo/config.Version=1.0.0" \
  main.go     

灵感来自https://github.com/golang/go/wiki/GcToolchainTricks#including-build-information-in-the-executable


如果您使用goreleaser,请阅读https://goreleaser.com/environment/#using-the-mainversion:

默认情况下,GoReleaser设置了三个ldflags:

main.version:当前的Git标签
main.commit:当前的git提交SHA
main.date:按照RFC3339格式的日期


如果您想看到实际效果,请访问https://github.com/hoto/fuzzy-repo-finder/blob/master/pkg/config/config.go

英文:

Use ldflags to set variables in main package:

With file main.go:

package main

import &quot;fmt&quot;

var (
    version string
    build   string
)

func main() {
    fmt.Println(&quot;version=&quot;, version)
    fmt.Println(&quot;build=&quot;, build)
}

Then run:

go run \
  -ldflags &quot;-X main.version=1.0.0 -X main.build=12082019&quot; \ 
  main.go

Build:

go build -o mybinary \
  -ldflags &quot;-X main.version=1.0.0 -X &#39;main.build=$(date)&#39;&quot; \ 
  main.go

Use ldflags to set variable in a non-main package:

With file config.go:

package config

import &quot;fmt&quot;

var (
    Version string
)

func LogVersion() {
    fmt.Println(&quot;version=&quot;, Version)
}

You will also need file main.go:

package main

import (
    &quot;fmt&quot;
    &quot;github.com/user/repo/config&quot;
}

func main() {
    config.LogVersion()
}

Build your binary first:

go build -o mybinary main.go 

Find the full path of variable name you want to set:

go tool nm &lt;path_to_binary&gt; | grep Version

Run and build the binary again but with the ldflags:

go run \
  -ldflags &quot;-X github.com/user/repo/config.Version=1.0.0&quot; \
  main.go --version       


go build -o mybinary \
  -ldflags &quot;-X github.com/user/repo/config.Version=1.0.0&quot; \
  main.go     

Inspired by https://github.com/golang/go/wiki/GcToolchainTricks#including-build-information-in-the-executable


Also if you are using goreleaser then read this https://goreleaser.com/environment/#using-the-mainversion :

> Default wise GoReleaser sets three ldflags:
>
> main.version: Current Git tag
> main.commit: Current git commit SHA
> main.date: Date according RFC3339


If you want to see this in action: https://github.com/hoto/fuzzy-repo-finder/blob/master/pkg/config/config.go

答案3

得分: 34

此外,我想发布一个小例子,展示如何使用git和makefile:

--- Makefile ----

这是我们想要给二进制输出命名的方式

BINARY=gomake

这些是我们想要传递给VERSION和BUILD的值

git tag 1.0.1

git commit -am "One more change after the tags"

VERSION=git describe --tags
BUILD=date +%FT%T%z

在这里设置-ldflags选项以进行go build,插入变量值

LDFLAGS_f1=-ldflags "-w -s -X main.Version=${VERSION} -X main.Build=${BUILD} -X main.Entry=f1"
LDFLAGS_f2=-ldflags "-w -s -X main.Version=${VERSION} -X main.Build=${BUILD} -X main.Entry=f2"

构建项目

build:
go build ${LDFLAGS_f1} -o ${BINARY}_f1
go build ${LDFLAGS_f2} -o ${BINARY}_f2

安装项目:复制二进制文件

install:
go install ${LDFLAGS_f1}

清理项目:删除二进制文件

clean:
if [ -f ${BINARY} ] ; then rm ${BINARY} ; fi

.PHONY: clean install

makefile将创建两个可执行文件。一个执行函数一,另一个将函数二作为主入口:

package main

import (
"fmt"
)

var (
Version string
Build string
Entry string

funcs = map[string]func() {
    "f1":functionOne,"f2":functionTwo,
}

)

func functionOne() {
fmt.Println("This is function one")
}

func functionTwo() {
fmt.Println("This is function two")
}

func main() {
fmt.Println("Version: ", Version)
fmt.Println("Build Time: ", Build)

funcs[Entry]()

}

然后只需运行:

make

你将得到:

mab@h2470988:~/projects/go/gomake/3/gomake$ ls -al
total 2020
drwxrwxr-x 3 mab mab 4096 Sep 7 22:41 .
drwxrwxr-x 3 mab mab 4096 Aug 16 10:00 ..
drwxrwxr-x 8 mab mab 4096 Aug 17 16:40 .git
-rwxrwxr-x 1 mab mab 1023488 Sep 7 22:41 gomake_f1
-rwxrwxr-x 1 mab mab 1023488 Sep 7 22:41 gomake_f2
-rw-rw-r-- 1 mab mab 399 Aug 16 10:21 main.go
-rw-rw-r-- 1 mab mab 810 Sep 7 22:41 Makefile
mab@h2470988:~/projects/go/gomake/3/gomake$ ./gomake_f1
Version: 1.0.1-1-gfb51187
Build Time: 2016-09-07T22:41:38+0200
This is function one
mab@h2470988:~/projects/go/gomake/3/gomake$ ./gomake_f2
Version: 1.0.1-1-gfb51187
Build Time: 2016-09-07T22:41:39+0200
This is function two

英文:

Additionally I would like to post a small example how to use git and a makefile:

--- Makefile ----

# This how we want to name the binary output
BINARY=gomake

# These are the values we want to pass for VERSION and BUILD
# git tag 1.0.1
# git commit -am &quot;One more change after the tags&quot;
VERSION=`git describe --tags`
BUILD=`date +%FT%T%z`

# Setup the -ldflags option for go build here, interpolate the variable values
LDFLAGS_f1=-ldflags &quot;-w -s -X main.Version=${VERSION} -X main.Build=${BUILD} -X main.Entry=f1&quot;
LDFLAGS_f2=-ldflags &quot;-w -s -X main.Version=${VERSION} -X main.Build=${BUILD} -X main.Entry=f2&quot;

# Builds the project
build:
	go build ${LDFLAGS_f1} -o ${BINARY}_f1
	go build ${LDFLAGS_f2} -o ${BINARY}_f2

# Installs our project: copies binaries
install:
	go install ${LDFLAGS_f1}

# Cleans our project: deletes binaries
clean:
	if [ -f ${BINARY} ] ; then rm ${BINARY} ; fi

.PHONY: clean install

The make file will create two executables. One is executing function one, the other will take function two as main entry:

package main

import (
        &quot;fmt&quot;
)

var (

        Version string
        Build   string
        Entry   string

        funcs = map[string]func() {
                &quot;f1&quot;:functionOne,&quot;f2&quot;:functionTwo,
        }

)

func functionOne() {
    fmt.Println(&quot;This is function one&quot;)
}

func functionTwo() {
    fmt.Println(&quot;This is function two&quot;)
}

func main() {

        fmt.Println(&quot;Version: &quot;, Version)
        fmt.Println(&quot;Build Time: &quot;, Build)

    funcs[Entry]()

}

Then just run:

make

You will get:

mab@h2470988:~/projects/go/gomake/3/gomake$ ls -al
total 2020
drwxrwxr-x 3 mab mab    4096 Sep  7 22:41 .
drwxrwxr-x 3 mab mab    4096 Aug 16 10:00 ..
drwxrwxr-x 8 mab mab    4096 Aug 17 16:40 .git
-rwxrwxr-x 1 mab mab 1023488 Sep  7 22:41 gomake_f1
-rwxrwxr-x 1 mab mab 1023488 Sep  7 22:41 gomake_f2
-rw-rw-r-- 1 mab mab     399 Aug 16 10:21 main.go
-rw-rw-r-- 1 mab mab     810 Sep  7 22:41 Makefile
mab@h2470988:~/projects/go/gomake/3/gomake$ ./gomake_f1
Version:  1.0.1-1-gfb51187
Build Time:  2016-09-07T22:41:38+0200
This is function one
mab@h2470988:~/projects/go/gomake/3/gomake$ ./gomake_f2
Version:  1.0.1-1-gfb51187
Build Time:  2016-09-07T22:41:39+0200
This is function two

答案4

得分: 28

我在构建混合命令行应用程序和库项目时,使用-ldflags参数遇到了麻烦,所以最后我使用了一个Makefile目标来生成一个包含我的应用程序版本和构建日期的Go源文件:

BUILD_DATE := `date +%Y-%m-%d\ %H:%M`
VERSIONFILE := cmd/myapp/version.go

gensrc:
    rm -f $(VERSIONFILE)
	@echo "package main" > $(VERSIONFILE)
    @echo "const (" >> $(VERSIONFILE)
    @echo "  VERSION = \"1.0\"" >> $(VERSIONFILE)
    @echo "  BUILD_DATE = \"$(BUILD_DATE)\"" >> $(VERSIONFILE)
    @echo ")" >> $(VERSIONFILE)

在我的init()方法中,我这样做:

flag.Usage = func() {
	fmt.Fprintf(os.Stderr, "%s version %s\n", os.Args[0], VERSION)
    fmt.Fprintf(os.Stderr, "built %s\n", BUILD_DATE)
    fmt.Fprintln(os.Stderr, "usage:")
	flag.PrintDefaults()
}

然而,如果你想要一个原子递增的构建编号而不是构建日期,你可能需要创建一个包含上次构建编号的本地文件。你的Makefile会将文件内容读入一个变量中,递增它,将其插入到version.go文件中,而不是日期,并将新的构建编号写回文件中。

英文:

I had trouble using the -ldflags parameter when building my mixed command-line app and library project, so I ended up using a Makefile target to generate a Go source file containing my app's version and the build date:

BUILD_DATE := `date +%Y-%m-%d\ %H:%M`
VERSIONFILE := cmd/myapp/version.go

gensrc:
    rm -f $(VERSIONFILE)
	@echo &quot;package main&quot; &gt; $(VERSIONFILE)
    @echo &quot;const (&quot; &gt;&gt; $(VERSIONFILE)
    @echo &quot;  VERSION = \&quot;1.0\&quot;&quot; &gt;&gt; $(VERSIONFILE)
    @echo &quot;  BUILD_DATE = \&quot;$(BUILD_DATE)\&quot;&quot; &gt;&gt; $(VERSIONFILE)
    @echo &quot;)&quot; &gt;&gt; $(VERSIONFILE)

In my init() method, I do this:

flag.Usage = func() {
	fmt.Fprintf(os.Stderr, &quot;%s version %s\n&quot;, os.Args[0], VERSION)
    fmt.Fprintf(os.Stderr, &quot;built %s\n&quot;, BUILD_DATE)
    fmt.Fprintln(os.Stderr, &quot;usage:&quot;)
	flag.PrintDefaults()
}

If you wanted an atomically-increasing build number instead of a build date, however, you would probably need to create a local file that contained the last build number. Your Makefile would read the file contents into a variable, increment it, insert it in the version.go file instead of the date, and write the new build number back to the file.

答案5

得分: 13

在Windows操作系统上,给定以下程序

package main

import "fmt"

var (
    version string
    date    string
)

func main() {
    fmt.Printf("version=%s, date=%s", version, date)
}

您可以使用以下命令进行构建

go build -ldflags "-X main.version=0.0.1 -X main.date=%date:~10,4%-%date:~4,2%-%date:~7,2%T%time:~0,2%:%time:~3,2%:%time:~6,2%"

日期格式假设您的环境中 echo %date%Fri 07/22/2016echo %time%16:21:52.88

然后输出将为:version=0.0.1, date=2016-07-22T16:21:52

英文:

On Windows OS given the program below

package main

import &quot;fmt&quot;

var (
	version string
	date    string
)

func main() {
	fmt.Printf(&quot;version=%s, date=%s&quot;, version, date)
}

You can build using

go build -ldflags &quot;-X main.version=0.0.1 -X main.date=%date:~10,4%-%date:~4,2%-%date:~7,2%T%time:~0,2%:%time:~3,2%:%time:~6,2%&quot;

Date format assumes your environment echo %date% is Fri 07/22/2016 and echo %time% is 16:21:52.88

Then the output will be: version=0.0.1, date=2016-07-22T16:21:52

答案6

得分: 11

使用多个-ldflags的方法:

$ go build -ldflags "-X name1=value1 -X name2=value2" -o path/to/output
英文:

to use multi -ldflags:

$ go build -ldflags &quot;-X name1=value1 -X name2=value2&quot; -o path/to/output

答案7

得分: 0

建立在其他答案的基础上,使用最近的go版本,也可以将buildid写入ELF部分,尽管从程序内部来看,这并不容易阅读。

我将相同的值写入两个地方,使用类似以下的代码:

BuildInfo:= "BUILD #x, branch @ rev built yymmdd hh:mm:ss"
// 注意嵌套引号 "''",以便将带有空格的字符串正确传递给底层工具
ldFl := fmt.Sprintf("-X 'main.buildId=%s' -s -w '-buildid=%s'", BuildInfo, BuildInfo)
args := []string{
	"build",
	"-ldflags", ldFl,
    "-trimpath",
	"-gcflags", "-dwarf=false",
}
buildpath:="path/to/my/cmd"
args=append(args,buildpath)
buildCmd:=exec.Command("go", args...)

我在使用mage时使用这个代码,mage是一个用go编写的构建工具。你不需要上面的额外标志,但我选择使用它们来尽可能地剥离发布二进制文件中的信息。

(离题:Mage需要比Make之类的工具更多的前期工作,但比基于make的构建系统更容易扩展/维护 - 而且你不必在go和其他语法之间切换思维。)

英文:

Building on the other answers, with recent go versions it's also possible to write a buildid to an ELF section - though that's not so easily readable from within the program.

I write the same value to both, using something like the following:

BuildInfo:= &quot;BUILD #x, branch @ rev built yymmdd hh:mm:ss&quot;
// note the nested quotes &quot;&#39;&#39;&quot; required to get a string with 
// spaces passed correctly to the underlying tool
ldFl := fmt.Sprintf(&quot;-X &#39;main.buildId=%s&#39; -s -w &#39;-buildid=%s&#39;&quot;, BuildInfo, BuildInfo)
args := []string{
	&quot;build&quot;,
	&quot;-ldflags&quot;, ldFl,
    &quot;-trimpath&quot;,
	&quot;-gcflags&quot;, &quot;-dwarf=false&quot;,
}
buildpath:=&quot;path/to/my/cmd&quot;
args=append(args,buildpath)
buildCmd:=exec.Command(&quot;go&quot;, args...)

I use this with mage, a build tool written in go. You don't need the extra flags above, but I chose those to strip as much information as possible from release binaries.

(off topic: Mage requires a bit more upfront work than something like Make, but is much easier to extend/maintain than a make-based build system - plus you don't have to switch mental gears between go and some other syntax.)

huangapple
  • 本文由 发表于 2012年7月6日 08:52:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/11354518.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定