Travis CI + Go:为不同的操作系统创建特定的构建流程

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

Travis CI + Go: creating a specific build flow for different OS

问题

我有一个Go项目,我想使用Travis-CI构建并部署到特定的提供商。我熟悉Gimme项目,它可以使用交叉编译来实现这一目标。但是因为Travis已经支持Linux和OSX,我只需要在Windows构建中使用这个功能。

当然,最大的动机是避免交叉编译时出现运行时错误,因为这种错误非常多。

我的问题是,如何在同一个.travis.yml文件中创建不同的构建流程:

  1. 本地Linux/OS构建(使用"os"部分)。
  2. 使用Gimme进行Windows编译。

第一个选项的.travis.yml文件将如下所示:

language: go

go: 
  - 1.5.1

branches: 
  only: 
    - master

os:
    - osx
    - linux


before_script:
    - go get -d -v ./...

script:
    - go build -v ./...
    # - go test -v ./...

before_deploy: 
  -  chmod +x ./before_deploy.sh
  - ./before_deploy.sh

第二个选项的.travis.yml文件将如下所示:

language: go

go: 
  - 1.5.1

branches: 
  only: 
    - master

env:
    - GIMME_OS=windows GIMME_ARCH=amd64


before_script:
    - go get -d -v ./...

script:
    - go build -v ./...
    # - go test -v ./...

before_deploy: 
  -  chmod +x ./before_deploy.sh
  - ./before_deploy.sh

有没有一种简洁的方式将这两个选项结合起来(使用环境变量或任何其他疯狂的想法)?

英文:

I have a Go project that I want to build with Travis-CI and deploy it to a specific provider.
I familiar with Gimme project that will use a cross-compilation to do so.
But because Travis already support linux and osx I only need this feature for Windows build.

The big motivation is, of course, to avoid cross-compilation run time error as there are plenty of it.

My question is how can I create, in the same .travis.yml file, a different build flow:

  1. Native linux/os build (with "os" section).
  2. Windows compilation using Gimmme

The .travis.yml file for the first option will look something like:

language: go

go: 
  - 1.5.1

branches: 
  only: 
    - master

os:
    - osx
    - linux


before_script:
    - go get -d -v ./...

script:
    - go build -v ./...
    # - go test -v ./...

before_deploy: 
  -  chmod +x ./before_deploy.sh
  - ./before_deploy.sh

The .travis.yml file for the second option will look something like:

language: go

go: 
  - 1.5.1

branches: 
  only: 
    - master

env:
    - GIMME_OS=windows GIMME_ARCH=amd64


before_script:
    - go get -d -v ./...

script:
    - go build -v ./...
    # - go test -v ./...

before_deploy: 
  -  chmod +x ./before_deploy.sh
  - ./before_deploy.sh

Is there a nice clean way to combine these two (with Environment variables or any other crazy idea)?

答案1

得分: 3

可能很简单,但是无法为特定的操作系统创建矩阵环境...

然后只需使用本地环境变量进行选择:

language: go
go: 
  - 1.5.1
branches: 
  only: 
    - master
os:
  - osx
  - linux
install:
  - if [ "$TRAVIS_OS_NAME" == "linux" ]; then
        export GIMME_OS=windows;
        export GIMME_ARCH=amd64;
    fi
before_script:
  - go get -d -v ./...
script:
  - go build -v ./...
after_script:
  - go test -v ./...
before_deploy: 
  - ./before_deploy.sh

另一种方式:

language: go
go: 
  - 1.5.1
branches: 
  only: 
    - master
matrix:
  include:
    - os: linux
      env: GIMME_OS=windows; GIMME_ARCH=amd64;
    - os: osx
before_script:
  - go get -d -v ./...
script:
  - go build -v ./...
after_script:
  - go test -v ./...
before_deploy: 
  - ./before_deploy.sh

注意: 命令- chmod +x ./before_deploy.sh可以直接在您的存储库中执行并提交...

注意: 环境变量可以通过以下方式访问:http://docs.travis-ci.com/user/environment-variables/#Default-Environment-Variables 或调用printenv

英文:

It might be simple, but matrix environement can not be done for a specific OS ...

Then just select with local environement variable:

language: go
go: 
  - 1.5.1
branches: 
  only: 
    - master
os:
  - osx
  - linux
install:
  - if [ "$TRAVIS_OS_NAME" == "linux" ]; then
        export GIMME_OS=windows;
        export GIMME_ARCH=amd64;
    fi
before_script:
  - go get -d -v ./...
script:
  - go build -v ./...
after_script:
  - go test -v ./...
before_deploy: 
  - ./before_deploy.sh

An other way:

language: go
go: 
  - 1.5.1
branches: 
  only: 
    - master
matrix:
  include:
    - os: linux
      env: GIMME_OS=windows; GIMME_ARCH=amd64;
    - os: osx
before_script:
  - go get -d -v ./...
script:
  - go build -v ./...
after_script:
  - go test -v ./...
before_deploy: 
  - ./before_deploy.sh

Note: the commande: - chmod +x ./before_deploy.sh can be directly done in your repository and commited on it ...

Note: The environament variable can be accessibe: http://docs.travis-ci.com/user/environment-variables/#Default-Environment-Variables or calling \printenv`

huangapple
  • 本文由 发表于 2015年10月14日 03:48:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/33111375.html
匿名

发表评论

匿名网友

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

确定