The last article mentioned the access Travis of NPM to implement the method of automatically publishing NPM packages. This article mainly describes the use of Go project, which is mainly divided into executing unit tests and sending build packages to Github release. The basic operation of Travis can be referred to above. This article only covers different parts

Unit testing

Instead of describing how to write unit tests for Go, this article uses Filenamify (a library that legalizes file paths) as an example. The test file filenamify_test.go has been written for filenamify. You only need to run go test -v in tracis.

# https://github.com/flytam/filenamify/blob/master/.travis.yml
language: go

go:
    - 1.13.x
env:
    - GO111MODULE=on
script: go test -v
Copy the code

Then add the build status icon to the project. done

Release lot release

Sometimes our Go projects need to be packaged into viable files and released directly to Github release for others to download and execute. This can also be implemented using Travis, using the Travis Releases tool

1. Create a.travis. Yml file and fill in the basic Go configuration environment

language: go

go:
    - 1.13.x
env:
    - GO111MODULE=on # Enable Go mod
install:
    - go get -v
Copy the code

Write a Makefile

In nodeJS projects, we normally configure an NPM run test command to perform the test, but Go does not have NPM and package.json. This is where you need to write a Makefile (you can think of the Makefile as package.json and just use make XXX to execute it).

In the case of blog-sync, here I need to package a viable file for the entire platform, so the Makefile looks like this

GOCMD=go
GOBUILD=$(GOCMD) build
BINARY_NAME=bin
NAME=blog-sync

#mac
build:
	CGO_ENABLED=0 $(GOBUILD) -o $(BINARY_NAME)/$(NAME)-mac
# windows
build-linux:
	CGO_ENABLED=0 GOOS=windows GOARCH=amd64 $(GOBUILD) -o $(BINARY_NAME)/$(NAME)-linux
# linux
build-win:
	CGO_ENABLED=0 GOOS=linux GOARCH=amd64 $(GOBUILD) -o $(BINARY_NAME)/$(NAME)-win.exe
# all platform
build-all:
	make build
	make build-win
	make build-linux
Copy the code

Run the make build-all command to generate executable files for the three platforms in the bin directory.

language: go

go:
    - 1.13.x
env:
    - GO111MODULE=on # Enable Go mod
install:
    - go get -v
before_deploy: make build-all
deploy:
    provider: releases
    api_key: Automatically generated Github key
    file_glob: true
    file: bin/*
    skip_cleanup: true
    on:
        repo: flytam/blog-sync
        tags: true
Copy the code

3. Initialize the configuration using Setup

The Travis CLI has been installed
travis setup releases
Enter the github account password, encryption key, release file, etc
Copy the code

After simple customization, the configuration is as follows. For releases configuration, please refer to the documentation

# https://github.com/flytam/blog-sync/blob/master/.travis.yml
language: go

go:
    - 1.13.x
env:
    - GO111MODULE=on # Enable Go mod
install:
    - go get -v
before_deploy: make build-all Execute the command to generate the binary file before publishing
deploy:
    provider: releases
    api_key:
        secure: xxxx
    Use glob to match publish files in the bin directory
    file_glob: true
    file: bin/*
    skip_cleanup: true
    on:
        repo: flytam/blog-sync
        # tag triggers publication
        tags: true
Copy the code

4, publish,

Every time a tag is pushed to the repository, it triggers an automatic release of the executable to github release

Git tag 1.0.0 git pushCopy the code

5. As you can see, our auto-build release is successful