I'm setting automated unit test
for golang
in travis ci
.
It requires to set .travis.yml
to run go test
before build.
I want to know what to code in .travis.yml
to achieve this goal.
Here is the stack
・client: react/axios
・api: golang/gin
・web-server: nginx
・db: mysql
・container: docker
・ci-tool: travis
・deploy: aws elastic beanstalk
Here is repository structure.
article
├ client
├ api
│ ├ main.go
│ ├ dao
│ │ ├ dao.go
│ │ └ dao_test.go
│ ├ //
│ └ Dockerfile
├ nginx
└ docker-compose.yml
.travis.yml
language: generic
sudo: required
services:
- docker
before_install:
- docker build -t jpskgc/api-test -f ./api/Dockerfile.dev ./api
script:
- docker run -e CI=true jpskgc/api-test go test -v ./dao
after_success:
- docker build -t jpskgc/multi-client ./client
- docker build -t jpskgc/multi-nginx ./nginx
- docker build -t jpskgc/multi-api ./api
- echo "$DOCKER_PASSWORD" | docker login -u "$DOCKER_ID" --password-stdin
- docker push jpskgc/multi-client
- docker push jpskgc/multi-nginx
- docker push jpskgc/multi-api
deploy:
provider: elasticbeanstalk
region: ap-northeast-1
app: multi-docker
env: MultiDocker-env
bucket_name: elasticbeanstalk-ap-northeast-1-340135579499
bucket_path: docker-multi
on:
branch: master
access_key_id:
secure: $AWS_ACCESS_KEY
secret_access_key:
secure: $AWS_SECRET_KEY
docker-compose.yml
version: '3'
services:
db:
image: mysql
ports:
- '3306:3306'
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: article
MYSQL_USER: docker
MYSQL_PASSWORD: docker
nginx:
restart: always
build:
dockerfile: Dockerfile.dev
context: ./nginx
ports:
- '3050:80'
depends_on:
- client
- api
api:
build:
dockerfile: Dockerfile.dev
context: ./api
volumes:
- ./api:/go/src/github.com/jpskgc/article/app
depends_on:
- db
tty: true
environment:
- AWS_ACCESS_KEY_ID
- AWS_SECRET_ACCESS_KEY
- MYSQL_USER
- MYSQL_PASSWORD
- MYSQL_HOST
- GO111MODULE
client:
build:
dockerfile: Dockerfile.dev
context: ./client
volumes:
- /app/node_modules
- ./client:/app
The full source code is here:
https://github.com/jpskgc/article
I want the golang unit test
to run before build in travis ci
.
But the actual is I don't know what to code in .travis.yml
to do that.
Here is the error in travis ci.
It looks like db connection fails. I want to know how to set up db connection to avoid this error.
$ docker run -e CI=true [secure]/api-test go test -v ./dao
panic: dial tcp :3306: connect: connection refused
goroutine 1 [running]:
main.main()
/app/main.go:30 +0xc72
The command "docker run -e CI=true [secure]/api-test go test -v ./dao" exited with 2.
Done. Your build exited with 1.