It's template project for releasing phoenix project as Docker image to DigitalOcean. Database is using PostgreSQL now.
Before start development your project, Please replace all phx_template
in this Repo to your own project name.
To start your Phoenix server:
- Install dependencies with
mix deps.get
- Create and migrate your database with
mix ecto.setup
- Install Node.js dependencies with
cd assets && npm install
- Start Phoenix endpoint with
mix phx.server
Now you can visit localhost:4000
from your browser.
Status Badges: Go to project setting page of CircleCI, its under NOTIFICATIONS section.
Environment variables:
- DOCKER_USER: for image build
- DOCKER_PASS: for image push
- OTP release with Distillery
- Runtime configuration by config providers.
- Auto database create by pre hook.
- Database migration command supported in production.
- Release as Docker image.
- Unit testing by CircleCI.
- Build Docker image by CircleCI.
- Docker
- asdf - language version manager
For Setup infra and deploy
- terraform
- packer
- ansible
To build docker image:
make image DOCKER_USER=[YOUR_DOCKER_USER_NAME]
To test the image with db at local: (First time db image up may failed, the workaround is try again.)
make run
If you want to handle deployment by yourself, You can deploy the docker image manually with required environment variables (refer to deploy/provision.local.env
).
Setup Image:
Using packer
to create image snapshot as base image. At first, you need to setup DigitalOcean secrets.
cp infra/secret.vars.example.json infra/secret.vars.json
Edit the infra/secret.vars.json
file with your won setting. The do_token
is your Personal access token of DigitalOcean and ssh_fingerprint
is the fingerprint of SSH key which you added to DigitalOcean. (Here assume you add the ssh key at "~/.ssh/id_rsa.pub" to DigitalOcean.)
After setup the secret file, you can build the image by
. ./infra/create_image.sh
If it is created successfully, you can get the snapshot name from terminal output like below.
...
==> Builds finished. The artifacts of successful builds are:
--> phx_template_base: A snapshot was created: 'packer-1548918676' (ID: 43089013) in regions 'sgp1'
Launch server node:
Replace snapshot image name to the variable base_image_name
in infra/single_node.sh
file. Then launch a new VM by
. ./infra/single_node.sh apply
If it is launched successfully, you can the public IP of node from termil output like below.
...
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
Outputs:
Public ip = 123.123.123.123
Place the node ip in deploy/ansible/production/hosts
.
Example:
[all_in_one]
123.123.123.123
Place the username of docker hub in the field docker_user
in deploy/ansible/group_vars/all/vars.yml
. To handle the production secret values, please use Ansible Vault to encrypt it. There are two fields need to be encrypted at beginning, web.secret_key_base
, web.erlang.cookie
and db.password
.
- Rename the
deploy/ansible/.example.vault_pass
todeploy/ansible/.vault_pass
. Then put your Vault password in it. - Get encrypted the value by
ansible-vault
, It's the example ofsecret_key_base
,ansible-vault encrypt_string --vault-password-file deploy/ansible/.vault_pass "YOUR_SECRET_BASE" --name 'secret_key_base'
- Place the encrypted value in
deploy/ansible/production/group_vars/all/secret.yml
.
First time, you need to setup the remote server by
. ./deploy/setup.sh production
Then deploy the docker image to remote server by
. ./deploy/deploy.sh production
NOTE: Here assume you have built and pushed the latest docker image to docker hub by CircleCI already, or you need to do it manually.
If the deploy successfully, you can see the phoenix page by visit the http://[NODE_PUBLIC_IP].
- Build release Docker image.
- Production DB init and migration
- CircleCI for running test
- CircleCI for build image
- Manually infra setup and deploy.
- CircleCI for deploy
- Database node setup
- Project generator based on Mix task.