parent
a617dd1fbf
commit
865fb1db89
@ -0,0 +1,13 @@ |
||||
Библиотека используется для автоматического версионирования GO-приложения с использованием переменных gitlab |
||||
|
||||
В dockerfile надо изменить строку билда на такую: |
||||
``` |
||||
RUN go build -ldflags "-X github.com/PCManiac/compile_vars.version=${CI_PIPELINE_IID} -X github.com/PCManiac/compile_vars.build_time=`date +%FT%T%z`" -o server ./... |
||||
``` |
||||
|
||||
Ну и в .gitlab-ci.yml собирать докер с подстановкой переменных |
||||
``` |
||||
- docker build --build-arg CI_PIPELINE_IID="$CI_PIPELINE_IID" --build-arg GITLAB_TOKEN="$CI_JOB_TOKEN" --pull -t "$CI_REGISTRY_IMAGE" . |
||||
``` |
||||
|
||||
При инициализации модуля в log будет добавлена строка с номером версии и временем билда. При необходимости эти значения также можно получить с помощью функций GetVersion() и GetBuildTime() |
||||
@ -0,0 +1,13 @@ |
||||
module github.com/PCManiac/compile_vars |
||||
|
||||
go 1.17 |
||||
|
||||
require ( |
||||
github.com/PCManiac/logrus_init v0.0.0 |
||||
github.com/sirupsen/logrus v1.9.0 |
||||
) |
||||
|
||||
require ( |
||||
github.com/caarlos0/env/v6 v6.10.0 // indirect |
||||
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect |
||||
) |
||||
@ -0,0 +1,26 @@ |
||||
github.com/PCManiac/logrus_init v0.0.0 h1:sn8aQyHLmN/AMTkZfKCQo4mOB5Ckhs+FL85ygNybyMw= |
||||
github.com/PCManiac/logrus_init v0.0.0/go.mod h1:mDbCY64MUIrl+gcI37TQGpb22ADZlY8WBJ6tR2pteqk= |
||||
github.com/caarlos0/env/v6 v6.10.0 h1:lA7sxiGArZ2KkiqpOQNf8ERBRWI+v8MWIH+eGjSN22I= |
||||
github.com/caarlos0/env/v6 v6.10.0/go.mod h1:hvp/ryKXKipEkcuYjs9mI4bBCg+UI0Yhgm5Zu0ddvwc= |
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= |
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= |
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= |
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= |
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= |
||||
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= |
||||
github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0= |
||||
github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= |
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= |
||||
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= |
||||
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= |
||||
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= |
||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= |
||||
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= |
||||
golang.org/x/sys v0.0.0-20211109184856-51b60fd695b3/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= |
||||
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ= |
||||
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= |
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= |
||||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= |
||||
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= |
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= |
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
||||
@ -0,0 +1,33 @@ |
||||
package compile_vars |
||||
|
||||
import ( |
||||
_ "github.com/PCManiac/logrus_init" |
||||
log "github.com/sirupsen/logrus" |
||||
) |
||||
|
||||
var ( |
||||
version string |
||||
build_time string |
||||
) |
||||
|
||||
func init() { |
||||
if version == "" { |
||||
version = "not set" |
||||
} |
||||
if build_time == "" { |
||||
build_time = "not set" |
||||
} |
||||
|
||||
log.WithFields(log.Fields{ |
||||
"version": version, |
||||
"build_time": build_time, |
||||
}).Info("Initializing.") |
||||
} |
||||
|
||||
func GetVersion() string { |
||||
return version |
||||
} |
||||
|
||||
func GetBuildTime() string { |
||||
return build_time |
||||
} |
||||
Loading…
Reference in new issue