Travis CI is a continuous integration service used to build and test software projects hosted on GitHub. Here's how you can set up a .travis.yml
file to configure Travis CI for a Go project.
A basic .travis.yml
file for a Go project might look like this:
yamllanguage: go
go:
- 1.19.x
- 1.20.x
env:
- GO111MODULE=on
script:
- go test -v ./...
language: go
: Specifies the programming language as Go.
go:
: Lists the versions of Go to be tested. In this example, it includes two versions: 1.19.x and 1.20.x.
env:
: Sets environment variables. In this case, GO111MODULE=on
ensures that Go modules are enabled.
script:
: Defines the commands to run as part of the build process. Here, it runs go test -v ./...
, which performs verbose testing on all packages.
Depending on your project's requirements, you might need a more complex configuration.
If your project has dependencies that need to be installed, you can use the before_install
and install
sections:
yamlbefore_install:
- go mod tidy
- go mod vendor
You can also include commands to run linting tools like golint
:
yamlinstall:
- go get -u golang.org/x/lint/golint
- go get -t -v ./...
- go install
To deploy your application after a successful build, you can use the deploy
section:
yamldeploy:
provider: script
script: bash deploy.sh
on:
branch: main
This example runs a custom deployment script (deploy.sh
) when builds on the main
branch succeed.
Here is a complete .travis.yml
file with additional configurations:
yamllanguage: go
go:
- 1.19.x
- 1.20.x
env:
- GO111MODULE=on
before_install:
- go mod tidy
- go mod vendor
install:
- go get -u golang.org/x/lint/golint
- go get -t -v ./...
- go install
script:
- golint ./...
- go vet ./...
- go test -v ./...
after_success:
- bash <(curl -s https://codecov.io/bash)
deploy:
provider: script
script: bash deploy.sh
on:
branch: main
before_install:
go mod tidy
to ensure the go.mod
file is correct and go mod vendor
to create a vendor
directory.install:
golint
tool and fetches all the necessary dependencies for testing and building the project.script:
golint
for linting the code, go vet
to check for errors, and go test
for running tests.after_success:
deploy:
main
branch.This configuration ensures that your Go project is properly built, tested, linted, and deployed using Travis CI.