.circleci/config.yml
version: 2 # use CircleCI 2.0
jobs: # basic units of work in a run
build: # runs not using Workflows must have a `build` job as entry point
docker: # run the steps with Docker
# CircleCI Go images available at: https://hub.docker.com/r/circleci/golang/
- image: circleci/golang:1.11
environment:
TEST_DATABASE_URL: postgresql://postgres@localhost/test
- image: circleci/redis
- image: circleci/postgres:10-alpine
environment:
POSTGRES_USER: postgres
POSTGRES_DB: test
POSTGRES_PASSWORD: ""
working_directory: /tmp/gosaas
environment: # environment variables for the build itself
- TEST_RESULTS: /tmp/test-results # path to where test results will be saved
steps: # steps that comprise the `build` job
- checkout # check out source code to working directory
- run: mkdir -p $TEST_RESULTS # create the test results directory
- restore_cache:
keys:
- go-mod-v1-{{ checksum "go.sum" }}
# Normally, this step would be in a custom primary image;
# we've added it here for the sake of explanation.
- run: go mod download
- run: sudo apt install -y postgresql-client
# CircleCi's Go Docker image includes netcat
# this allows polling the redis port to confirm it is open before proceeding
- run:
name: Waiting for redis to be ready
command: |
for i in `seq 1 10`;
do
nc -z localhost 6379 && echo Success && exit 0
echo -n .
sleep 1
done
echo Failed waiting for redis && exit 1
- run:
name: Waiting for Postgres to be ready
command: |
for i in `seq 1 10`;
do
nc -z localhost 5432 && echo Success && exit 0
echo -n .
sleep 1
done
echo Failed waiting for Postgres && exit 1
- run:
name: Initialize the database
command: |
for f in /tmp/gosaas/migrations/*.sql;
do
psql -d $TEST_DATABASE_URL -f $f
done
- run:
name: Run unit tests
# store the results of our tests in the $TEST_RESULTS directory
command: |
go test ./... | tee ${TEST_RESULTS}/go-test.out
- run: go build # pull and build dependencies for the project
- save_cache:
key: go-mod-v1-{{ checksum "go.sum" }}
paths:
- "/go/pkg/mod"
- store_artifacts: # upload test summary for display in Artifacts
path: /tmp/test-results
destination: raw-test-output
- store_test_results: # upload test results for display in Test Summary
path: /tmp/test-results