Skip to content

Commit

Permalink
initial docker-compose based CI
Browse files Browse the repository at this point in the history
  • Loading branch information
izelnakri committed Mar 1, 2020
1 parent 7c40472 commit b96964d
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 21 deletions.
11 changes: 6 additions & 5 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,17 @@ jobs:
test:
docker:
- image: docker:stable-git
working_directory: ~/code
working_directory: ~/
steps:
- checkout
- setup_remote_docker
- run:
name: Pull and run a docker container
command: |
DOCKER_TAG=$(echo paper_trail:${CIRCLE_BRANCH} | tr '/' '_')
docker pull ${HUB_USERNAME}/${DOCKER_TAG}
docker run -t -d --name="paper_trail" ${HUB_USERNAME}/${DOCKER_TAG} /bin/sh
- run: docker exec -it paper_trail mix test
apk add docker-compose
sed -i $(echo "s/\$CIRCLE_BRANCH/$CIRCLE_BRANCH/g") .env
docker-compose up -d
- run: docker-compose exec -T backend mix test --trace

workflows:
version: 2
Expand Down
8 changes: 8 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
POSTGRES_USER=postgres
POSTGRES_PASSWORD=postgres
POSTGRES_HOST=localhost
# PG_HOST=localhost
MIX_ENV=test
# CIRCLE_BRANCH=$$(if [ -v CIRCLE_BRANCH ]; then echo master; else git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'; fi)
CIRCLE_BRANCH=$CIRCLE_BRANCH
DOCKER_TAG=$(echo paper_trail:${CIRCLE_BRANCH} | tr '/' '_')
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ FROM "elixir:1.10.1-alpine"

WORKDIR /code/

RUN apk add postgresql
RUN mix local.hex --force && mix local.rebar --force

COPY ["mix.lock", "mix.exs", "/code/"]
Expand Down
4 changes: 2 additions & 2 deletions config/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ config :paper_trail, PaperTrail.Repo,
username: System.get_env("POSTGRES_USER"),
password: System.get_env("POSTGRES_PASSWORD"),
database: "paper_trail_test",
hostname: System.get_env("POSTGRES_HOST"),
hostname: System.get_env("PGHOST"),
poolsize: 10

config :paper_trail, PaperTrail.UUIDRepo,
adapter: Ecto.Adapters.Postgres,
username: System.get_env("POSTGRES_USER"),
password: System.get_env("POSTGRES_PASSWORD"),
database: "paper_trail_uuid_test",
hostname: System.get_env("POSTGRES_HOST"),
hostname: System.get_env("PGHOST"),
poolsize: 10
26 changes: 12 additions & 14 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -1,42 +1,40 @@
version: '3.2'
version: '3.4'
services:
db:
image: postgres:12.2-alpine
environment:
POSTGRES_PASSWORD: $PGUSER
POSTGRES_USER: $PGPASSWORD
POSTGRES_PASSWORD: $POSTGRES_USER
POSTGRES_USER: $POSTGRES_PASSWORD
PGDATA: /var/lib/postgresql/data/pgdata
restart: always
networks:
- backend_network
volumes:
- pgdata:/var/lib/postgresql/data
backend:
image: $BACKEND_IMAGE_NAME
image: inakri/paper_trail:$CIRCLE_BRANCH
build:
context: .
dockerfile: $BACKEND_DOCKERFILE
cache_from:
- $BASE_IMAGE
- $BACKEND_IMAGE_CACHE
dockerfile: Dockerfile
environment:
PGUSER: $PGUSER
PGPASSWORD: $PGPASSWORD
PGPORT: $PGPORT
POSTGRES_USER: $POSTGRES_USER
POSTGRES_PASSWORD: $POSTGRES_PASSWORD
PGPORT: 5432
PGHOST: db
MIX_ENV: $MIX_ENV
tty: true
ports:
- "4000:4000"
depends_on:
- db
networks:
- backend_network
command: $BACKEND_COMMAND
command: ["./setup-database.sh"]

volumes:
pgdata:
networks:
backend_network:
driver: bridge

# cache_from:
# - $BASE_IMAGE
# - $BACKEND_IMAGE_CACHE
24 changes: 24 additions & 0 deletions setup-database.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/sh
set -e

# Prepare Dialyzer if the project has Dialyxer set up
# if mix help dialyzer >/dev/null 2>&1
# then
# echo "\nFound Dialyxer: Setting up PLT..."
# mix do deps.compile, dialyzer --plt
# else
# echo "\nNo Dialyxer config: Skipping setup..."
# fi

# Wait for Postgres to become available.
until psql -h $PG_HOST -U "$POSTGRES_USER" -c '\q' 2>/dev/null; do
>&2 echo "Postgres is unavailable - sleeping"
sleep 1
done

echo "\nPostgres is available: continuing with database setup..."

mix ecto.create
# mix ecto.migrate

# echo "\nPostgres migrations finished..."

0 comments on commit b96964d

Please sign in to comment.