dongxia2068 2015-06-09 20:13
浏览 82
已采纳

通过Travis-CI在Go中将Git标签名称作为版本

Essentially what I want to do is embed the git tag name (from a github release) to a version string within my GO code.

If I use this code;

package main

import (
    "flag"
    "fmt"
)

var version string

func main() {

    var verFlag bool
    flag.BoolVar(&verFlag, "version", false, "Returns the version number")

    var confPath string
    flag.StringVar(&confPath, "conf", "conf.yml", "Location on config file")

    flag.Parse()

    // if the user wants to see the version
    if verFlag {
        fmt.Printf("%s", version)
        return
    }
}

what's the best way of setting "VERSION" in -ldflags '-X main.version VERSION' to $TRAVIS_TAG during a build in Travis-CI,

Additionally, Travis CI sets environment variables you can use in your build, e.g. to tag the build, or to run post-build deployments.

TRAVIS_TAG: If the current build for a tag, this includes the tag’s name

My last effort was to use this make file:

GOFLAGS ?= $(GOFLAGS:)

TAG := $(VERSION)
ifeq ($(TAG),)
  BRANCH := $(shell git rev-parse --abbrev-ref HEAD)
  DT := $(shell date '+%F::%T')
  VSN := $(BRANCH)-$(DT)
else
  VSN := $(TAG)
endif

ENV := $(shell printenv)

GOFLAGS = -ldflags '-X main.version $(VSN)'

default:
    all

all: 
    test
    install

install:
    get-deps
    @go build $(GOFLAGS) *.go

test:
    @go test $(GOFLAGS) ./...

get-deps:
    @go get ./...

clean:
    @go clean $(GOFLAGS) -i ./

with this travis config file;

language: go
script: make VERSION=$TRAVIS_TAG
go:
- 1.4.1
deploy:
  provider: releases
  api_key:
    secure: reallylongsecurestring
  file: releasebin
  skip_cleanup: true
  on:
    tags: true

However no matter what variation I try, and I have tried many different flavours of essentially this example, it seems that the binary that ends up in my github release does not contain the tag name. What is observed is the branch and date version string that results from when no version string is set in the make file. I have tried this make file on my dev machine and set environment variable to the expected in Travis and it works as expected. I am also clear that I dont expect this to happen on commit builds but only on releases from github ie when a tag is created.

  • 写回答

1条回答 默认 最新

  • dpmp9359 2015-06-17 12:37
    关注

    So I fixed this my self.

    My problem in the above example was in the Travis file. I seems I needed quotes around my "script" parameter. For completeness here is my new Travis file;

    language: go
    script: 
     - "make VERSION=$TRAVIS_TAG"
    go:
     - 1.4.1
    deploy:
      provider: releases
      api_key:
        secure: reallylongsecurestring
      file: releasebin
      skip_cleanup: true
      on:
        tags: true
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测