Skip to content

Commit

Permalink
Merge pull request #111 from sgobotta/feature/add-cd
Browse files Browse the repository at this point in the history
Revamp
  • Loading branch information
sgobotta authored Sep 18, 2023
2 parents a54a074 + 6f0f519 commit 3c50613
Show file tree
Hide file tree
Showing 83 changed files with 9,317 additions and 135 deletions.
7 changes: 6 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# App
APP_NAME=livedj
export MIX_ENV=dev
export APP_HOST=0.0.0.0
export PHX_HOST=0.0.0.0
export STAGE=local

# Email configuration
export LIVEDJ_FROM_EMAIL=[email protected]
Expand All @@ -14,7 +15,11 @@ export DB_DATABASE=livedj_dev
export DB_HOSTNAME=localhost

# Cache service
export REDIS_HOST=localhost
export REDIS_PASS=123456

# Misc
export UPLOADS_PATH=priv/static/uploads

# Youtube API Key
export YOUTUBE_API_KEY="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
33 changes: 33 additions & 0 deletions .env.prod.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Deploy
STAGE=local

# App
PHX_HOST=0.0.0.0
MIX_ENV=prod
STAGE=local
SECRET_KEY_BASE=$(elixir --eval 'IO.puts(:crypto.strong_rand_bytes(64) |> Base.encode64(padding: false))')
PHX_SERVER=true
UPLOADS_PATH="priv/static/uploads/"

# Docker
CONTAINER_PORT=5000
INTERNAL_PORT=5001

# Email configuration
[email protected]
SENDGRID_API_KEY=xxxxxxxxxxxxxxxxxxxxxxx

# Database
# Either use url or the standard db vars
# DATABASE_URL=ecto://postgres:postgres@postgres/db_local
DB_USERNAME=postgres
DB_PASSWORD=postgres
DB_DATABASE=livedj_local
DB_HOSTNAME=postgres

# Cache service
REDIS_HOST=redis
REDIS_PASS=123456

# Youtube API Key
YOUTUBE_API_KEY="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
18 changes: 16 additions & 2 deletions .github/actions/dev/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ inputs:
POSTGRES_USER:
description: Postgres database user
required: true
REDIS_HOST:
description: The Redis host
required: true
REDIS_PASS:
description: The Redis host
required: true
STAGE:
default: local
description: The production environment
required: false
UPLOADS_PATH:
description: The uploads path
required: true
Expand Down Expand Up @@ -83,9 +93,13 @@ runs:
- name: Setup Database
shell: bash
run: |
mix reset.ecto
mix ecto.reset
env:
STAGE: ${{ inputs.STAGE }}
MIX_ENV: ${{ inputs.MIX_ENV }}
# Postgres env
POSTGRES_USER: ${{ inputs.POSTGRES_USER }}
POSTGRES_PASSWORD: ${{ inputs.POSTGRES_PASSWORD }}

# Setup the Phoenix Client -------------------------------------------------
- name: Setup web client
Expand Down Expand Up @@ -121,4 +135,4 @@ runs:
# Misc env
UPLOADS_PATH: ${{ inputs.UPLOADS_PATH }}
run: |
mix check.credo
mix check.dialyzer
11 changes: 9 additions & 2 deletions .github/actions/prod/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ inputs:
POSTGRES_USER:
description: Postgres database user
required: true
REDIS_HOST:
description: The Redis host
required: true
REDIS_PASS:
description: The Redis pass
required: true
STAGE:
default: local
description: The production environment
Expand Down Expand Up @@ -117,7 +123,7 @@ runs:
./entrypoint.sh setup_db
env:
# Application env
APP_HOST: 0.0.0.0
PHX_HOST: 0.0.0.0
SECRET_KEY_BASE: some-secret-key
STAGE: local
# Postgres env
Expand All @@ -128,6 +134,7 @@ runs:
# Sendgrid env
LIVEDJ_FROM_EMAIL: [email protected]
SENDGRID_API_KEY: some-sendgrid-api-key
MIX_ENV: ${{ inputs.MIX_ENV }}

# Test release -------------------------------------------------------------
- name: Test release
Expand All @@ -138,7 +145,7 @@ runs:
id: test-application
env:
# Application env
APP_HOST: 0.0.0.0
PHX_HOST: 0.0.0.0
SECRET_KEY_BASE: some-secret-key
STAGE: local
# Postgres env
Expand Down
17 changes: 16 additions & 1 deletion .github/actions/test/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ inputs:
POSTGRES_USER:
description: Postgres database user
required: true
REDIS_HOST:
description: The Redis host
required: true
REDIS_PASS:
description: The Redis pass
required: true
STAGE:
default: local
description: The production environment
required: false
UPLOADS_PATH:
description: The uploads path
required: true
Expand Down Expand Up @@ -83,9 +93,13 @@ runs:
- name: Setup Database
shell: bash
run: |
mix reset.ecto
mix ecto.reset
env:
STAGE: ${{ inputs.STAGE }}
MIX_ENV: ${{ inputs.MIX_ENV }}
# Postgres env
POSTGRES_USER: ${{ inputs.POSTGRES_USER }}
POSTGRES_PASSWORD: ${{ inputs.POSTGRES_PASSWORD }}

# Setup the Phoenix Client -------------------------------------------------
- name: Setup web client
Expand All @@ -107,6 +121,7 @@ runs:
POSTGRES_PASSWORD: ${{ inputs.POSTGRES_PASSWORD }}
# Misc env
UPLOADS_PATH: ${{ inputs.UPLOADS_PATH }}
STAGE: ${{ inputs.STAGE }}
run: >
GITHUB_TOKEN=${GITHUB_TOKEN}
mix coveralls.github -u
32 changes: 32 additions & 0 deletions .github/workflows/cd-dev.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: CD-dev

on:
workflow_run:
workflows: ["CI"]
branches: [dev]
types:
- completed

jobs:
on-success:
runs-on: ubuntu-22.04
if: ${{ github.event.workflow_run.conclusion == 'success' }}
steps:
- name: Cloning repo
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Push to dokku
uses: dokku/github-action@master
with:
# specify the `main` branch as the remote branch to push to
branch: 'main'
git_remote_url: ${{ secrets.SSH_REMOTE_DEV_URL }}
ssh_private_key: ${{ secrets.SSH_PRIVATE_KEY }}

on-failure:
runs-on: ubuntu-20.04
if: ${{ github.event.workflow_run.conclusion == 'failure' }}
steps:
- run: echo 'The triggering workflow failed'
66 changes: 55 additions & 11 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ name: CI

on:
# This action is triggered on any branch that opens a pull request.
push:
branches:
- "*"
# This action is not triggered when markdown files are modified.
paths-ignore:
- '**.md'
pull_request:
branches:
- "*"
Expand All @@ -22,6 +28,10 @@ env:
POSTGRES_PASSWORD: ${{ secrets.POSTGRES_PASSWORD }}
POSTGRES_USER: ${{ secrets.POSTGRES_USER }}
POSTGRES_HOSTNAME: localhost
# Redis env
REDIS_HOST: localhost
REDIS_PORT: 6379
REDIS_PASS: ${{ secrets.REDIS_PASS }}
# Misc env
UPLOADS_PATH: "priv/static/uploads/"

Expand All @@ -33,15 +43,29 @@ jobs:
image: postgres
ports: ['5432:5432']
env:
POSTGRES_USER: ${{ env.POSTGRES_USER }}
POSTGRES_PASSWORD: ${{ env.POSTGRES_PASSWORD }}
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
# Setup & run Redis -----------------------------------------------------
- name: Run redis with a password
shell: bash
run: |
docker run \
--rm \
--name redis \
-p ${{ env.REDIS_PORT }}:6379 \
-d redis:6-alpine redis-server \
--appendonly yes \
--requirepass ${{ env.REDIS_PASS }} \
--port ${{ env.REDIS_PORT }}
# Run the dev action ----------------------------------------------------
- uses: actions/[email protected]
- name: Run Development Checks
uses: ./.github/actions/dev
Expand All @@ -50,9 +74,11 @@ jobs:
GITHUB_TOKEN: ${{ env.GITHUB_TOKEN }}
MIX_ENV: dev
OTP_VERSION: ${{ env.OTP_VERSION }}
POSTGRES_PASSWORD: ${{ env.POSTGRES_PASSWORD }}
POSTGRES_USER: ${{ env.POSTGRES_USER }}
UPLOADS_PATH: priv/static/uploads/
POSTGRES_PASSWORD: postgres
POSTGRES_USER: postgres
REDIS_HOST: ${{ env.REDIS_HOST }}
REDIS_PASS: ${{ env.REDIS_PASS }}
UPLOADS_PATH: ${{ env.UPLOADS_PATH }}

test:
runs-on: ubuntu-22.04
Expand All @@ -61,15 +87,29 @@ jobs:
image: postgres
ports: ['5432:5432']
env:
POSTGRES_USER: ${{ env.POSTGRES_USER }}
POSTGRES_PASSWORD: ${{ env.POSTGRES_PASSWORD }}
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
# Setup & run Redis -----------------------------------------------------
- name: Run redis with a password
shell: bash
run: |
docker run \
--rm \
--name redis \
-p ${{ env.REDIS_PORT }}:6379 \
-d redis:6-alpine redis-server \
--appendonly yes \
--requirepass ${{ env.REDIS_PASS }} \
--port ${{ env.REDIS_PORT }}
# Run the test action ---------------------------------------------------
- uses: actions/[email protected]
- name: Run Unit Tests
uses: ./.github/actions/test
Expand All @@ -78,9 +118,11 @@ jobs:
GITHUB_TOKEN: ${{ env.GITHUB_TOKEN }}
MIX_ENV: test
OTP_VERSION: ${{ env.OTP_VERSION }}
POSTGRES_PASSWORD: ${{ env.POSTGRES_PASSWORD }}
POSTGRES_USER: ${{ env.POSTGRES_USER }}
UPLOADS_PATH: priv/static/uploads/
POSTGRES_PASSWORD: postgres
POSTGRES_USER: postgres
REDIS_HOST: ${{ env.REDIS_HOST }}
REDIS_PASS: ${{ env.REDIS_PASS }}
UPLOADS_PATH: ${{ env.UPLOADS_PATH }}

prod:
runs-on: ubuntu-22.04
Expand Down Expand Up @@ -109,4 +151,6 @@ jobs:
POSTGRES_HOSTNAME: ${{ env.POSTGRES_HOSTNAME }}
POSTGRES_PASSWORD: ${{ env.POSTGRES_PASSWORD }}
POSTGRES_USER: ${{ env.POSTGRES_USER }}
UPLOADS_PATH: priv/static/uploads/
REDIS_HOST: ${{ env.REDIS_HOST }}
REDIS_PASS: ${{ env.REDIS_PASS }}
UPLOADS_PATH: ${{ env.UPLOADS_PATH }}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,4 @@ npm-debug.log

# Env
.env
.env.prod
12 changes: 6 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -63,22 +63,22 @@ docker.logs:
@docker logs $(CONTAINER_NAME) -f

#🐳 docker.release: @ Re-create a docker image and run it
docker.release: PORT:=5000
docker.release: CONTAINER_PORT:=5000
docker.release: INTERNAL_PORT:=5001
docker.release: docker.stop docker.delete docker.build docker.run

#🐳 docker.rerun: @ Stops and deletes old container to re-run a fresh new container
docker.rerun: PORT:=5000
docker.rerun: CONTAINER_PORT:=5000
docker.rerun: INTERNAL_PORT:=5001
docker.rerun: docker.stop docker.delete docker.run

#🐳 docker.run: @ Run the docker container
docker.run: PORT:=5000
docker.run: CONTAINER_PORT:=5000
docker.run: INTERNAL_PORT:=5001
docker.run: CONTAINER_NAME:=$(CONTAINER_NAME)
docker.run: IMAGE_NAME:=$(IMAGE_NAME)
docker.run:
@docker run --detach --name $(CONTAINER_NAME) --network devops_livedj_storage -p $(PORT):$(INTERNAL_PORT) --env PORT=$(INTERNAL_PORT) --env-file .env.prod $(IMAGE_NAME)
@docker run --detach --name $(CONTAINER_NAME) --network devops_livedj_storage -p $(CONTAINER_PORT):$(INTERNAL_PORT) --env PORT=$(INTERNAL_PORT) --env-file .env.prod $(IMAGE_NAME)

#🐳 docker.stop: @ Stop the docker container
docker.stop: CONTAINER_NAME:=$(CONTAINER_NAME)
Expand Down Expand Up @@ -139,14 +139,14 @@ reset.ecto.dev: SHELL:=/bin/bash
reset.ecto.dev: MIX_ENV=dev
reset.ecto.dev:
@echo "🧹 Cleaning db for dev env..."
@mix reset.ecto
@mix ecto.reset

#💣 reset.ecto.test: @ Resets database for test env
reset.ecto.test: SHELL:=/bin/bash
reset.ecto.test: MIX_ENV=test
reset.ecto.test:
@echo "🧹 Cleaning db for test env..."
@mix reset.ecto
@mix ecto.reset

#📦 setup: @ Installs dependencies and set up database for dev and test envs
setup: SHELL:=/bin/bash
Expand Down
Loading

0 comments on commit 3c50613

Please sign in to comment.