Skip to content

Instantly share code, notes, and snippets.

@cheerfulstoic
Created February 11, 2021 09:45
Show Gist options
  • Save cheerfulstoic/125bff73443d3ddd47963cb38f03269f to your computer and use it in GitHub Desktop.
Save cheerfulstoic/125bff73443d3ddd47963cb38f03269f to your computer and use it in GitHub Desktop.
CircleCI / Docker configuration to build, test, and push an image to Docker Hub
# file path should be .circleci/config.yml
version: 2 # use CircleCI 2.0 instead of CircleCI Classic
jobs: # basic units of work in a run
build: # runs not using Workflows must have a `build` job as entry point
parallelism: 1 # run only one instance of this job
docker: # run the steps with Docker
- image: circleci/elixir:1.11.3 # ...with this image as the primary container; this is where all `steps` will run
auth:
username: $DOCKER_USER
password: $DOCKER_PASS # context / project UI env-var reference
environment: # environment variables for primary container
MIX_ENV: test
working_directory: ~/app # directory where steps will run
steps: # commands that comprise the `build` job
- checkout # check out source code to working directory
- run: mix local.hex --force # install Hex locally (without prompt)
- run: mix local.rebar --force # fetch a copy of rebar (without prompt)
- restore_cache: # restores saved mix cache
# Read about caching dependencies: https://circleci.com/docs/2.0/caching/
keys: # list of cache keys, in decreasing specificity
- v1-mix-cache-{{ .Branch }}-{{ checksum "mix.lock" }}
- v1-mix-cache-{{ .Branch }}
- v1-mix-cache
- restore_cache: # restores saved build cache
keys:
- v1-build-cache-{{ .Branch }}
- v1-build-cache
- run: |
mix do deps.get, compile # get updated dependencies & compile them
MIX_ENV=dev mix check # code analysis using `ex_check`
MIX_ENV=test mix test # run all tests in project
- save_cache: # generate and store mix cache
key: v1-mix-cache-{{ .Branch }}-{{ checksum "mix.lock" }}
paths: "deps"
- save_cache: # generate and store mix cache
key: v1-mix-cache-{{ .Branch }}
paths: "deps"
- save_cache: # generate and store mix cache
key: v1-mix-cache
paths: "deps"
- save_cache: # don't forget to save a *build* cache, too
key: v1-build-cache-{{ .Branch }}
paths: "_build"
- save_cache: # don't forget to save a *build* cache, too
key: v1-build-cache
paths: "_build"
- store_test_results: # upload junit test results for display in Test Summary
# Read more: https://circleci.com/docs/2.0/collect-test-data/
path: _build/test/lib/my_app # Replace with the name of your :app
- setup_remote_docker
- run:
name: Build image
command: |
docker login -u $DOCKER_USER -p $DOCKER_PASS
docker build . --build-arg mix_env=prod --build-arg secret_key_base=$SECRET_KEY_BASE -t org/my-app:release
docker tag org/my-app:release org/my-app:$CIRCLE_SHA1
docker tag org/my-app:release org/my-app:$CIRCLE_BUILD_NUM
docker push org/my-app
- run:
name: Tag main as latest
filters:
branches:
only: main
command: |
docker login -u $DOCKER_USER -p $DOCKER_PASS
docker tag org/my-app:release org/my-app:latest
docker push org/my-app
# ---- Build Stage ----
FROM elixir:1.11.3-alpine AS builder
EXPOSE 80
ENV APP_HOME /app
RUN mkdir $APP_HOME
WORKDIR $APP_HOME
COPY mix.exs .
COPY mix.lock .
ARG mix_env
ENV MIX_ENV $mix_env
ARG secret_key_base
ENV SECRET_KEY_BASE $secret_key_base
RUN mix local.hex --force && \
mix local.rebar --force && \
mix deps.get
COPY . /app
RUN MIX_ENV=prod mix release
CMD ["mix", "phx.server"]
# ---- Application Stage ----
FROM alpine:3
RUN apk add --no-cache --update bash openssl ncurses-libs
WORKDIR /app
COPY --from=builder /app/_build/prod/rel/my_app .
CMD ["/app/bin/my_app", "start"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment