Skip to content

Commit

Permalink
deploy to fly
Browse files Browse the repository at this point in the history
Signed-off-by: Connor Rigby <[email protected]>
  • Loading branch information
ConnorRigby committed Jun 1, 2023
1 parent 5e4e92e commit 07e96ea
Show file tree
Hide file tree
Showing 24 changed files with 278 additions and 428 deletions.
2 changes: 2 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ priv/static
test
.env
.cache
backup
erl_crash.dump
47 changes: 3 additions & 44 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,49 +10,8 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

- name: Configure SSH
run: |
mkdir -p ~/.ssh/
echo "$SSH_KEY" > ~/.ssh/deploy.key
chmod 600 ~/.ssh/deploy.key
cat >>~/.ssh/config <<END
Host deploy
HostName sixtyeightplus.one
User www-data
IdentityFile ~/.ssh/deploy.key
StrictHostKeyChecking no
END
- uses: superfly/flyctl-actions/setup-flyctl@master
- run: flyctl deploy --remote-only
env:
SSH_KEY: ${{ secrets.SSH_KEY }}

- name: Set up QEMU
uses: docker/setup-qemu-action@v2

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2

- name: Login to Docker Hub
uses: docker/login-action@v2
with:
registry: registry.sixtyeightplus.one
username: ${{ secrets.DOCKER_REGISTRY_USERNAME }}
password: ${{ secrets.DOCKER_REGISTRY_TOKEN }}

- name: Build and push
uses: docker/build-push-action@v3
with:
push: true
tags: registry.sixtyeightplus.one/miata_bot
build-args: |
SECRET_KEY_BASE=${{ secrets.SECRET_KEY_BASE }}
DATABASE_URL=${{ secrets.DATABASE_URL }}
DISCORD_TOKEN=${{ secrets.DISCORD_TOKEN }}
DISCORD_CLIENT_ID=${{ secrets.DISCORD_CLIENT_ID }}
DISCORD_CLIENT_SECRET=${{ secrets.DISCORD_CLIENT_SECRET }}
PARTPICKER_API_TOKEN=${{ secrets.PARTPICKER_API_TOKEN }}
FREENODE_PASSWORD=${{ secrets.FREENODE_PASSWORD }}
- name: Update instance
run: |
ssh deploy "docker-compose pull miata-bot; docker-compose up -d --no-deps miata-bot"
FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }}

1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,4 @@ npm-debug.log

*.env
deploy.sh
backup
5 changes: 2 additions & 3 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
erlang 24.0.3
elixir 1.12.2-otp-24
nodejs 16.3.0
erlang 26.0
elixir 1.14.5-otp-26
127 changes: 75 additions & 52 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,68 +1,91 @@
# prepare release image
FROM alpine:3.14 AS app_base
WORKDIR /app
RUN apk add --no-cache libstdc++ openssl ncurses-libs bash

FROM erlang:24.0.3-alpine as build
ENV ELIXIR_VERSION="v1.12.2-otp-24"

# # install elixir
RUN wget https://repo.hex.pm/builds/elixir/$ELIXIR_VERSION.zip && \
mkdir -p /usr/local/elixir && \
unzip -q -d /usr/local/elixir $ELIXIR_VERSION.zip
ENV PATH=/usr/local/elixir/bin:$PATH
# Find eligible builder and runner images on Docker Hub. We use Ubuntu/Debian instead of
# Alpine to avoid DNS resolution issues in production.
#
# https://hub.docker.com/r/hexpm/elixir/tags?page=1&name=ubuntu
# https://hub.docker.com/_/ubuntu?tab=tags
#
#
# This file is based on these images:
#
# - https://hub.docker.com/r/hexpm/elixir/tags - for the build image
# - https://hub.docker.com/_/debian?tab=tags&page=1&name=bullseye-20221004-slim - for the release image
# - https://pkgs.org/ - resource for finding needed packages
# - Ex: hexpm/elixir:1.14.2-erlang-25.1.2-debian-bullseye-20221004-slim
#
ARG ELIXIR_VERSION=1.14.5
ARG OTP_VERSION=26.0
ARG DEBIAN_VERSION=bullseye-20230227-slim

RUN apk add libstdc++ bash make alpine-sdk
ARG BUILDER_IMAGE="hexpm/elixir:${ELIXIR_VERSION}-erlang-${OTP_VERSION}-debian-${DEBIAN_VERSION}"
ARG RUNNER_IMAGE="debian:${DEBIAN_VERSION}"

RUN mix do local.hex --force, local.rebar --force
FROM ${BUILDER_IMAGE} as builder

FROM build AS deps
# install build dependencies
RUN apt-get update -y && apt-get install -y build-essential git nodejs npm\
&& apt-get clean && rm -f /var/lib/apt/lists/*_*

# prepare build dir
WORKDIR /app

ARG SECRET_KEY_BASE
ARG DATABASE_URL
ARG DISCORD_TOKEN
ARG DISCORD_CLIENT_ID
ARG DISCORD_CLIENT_SECRET
ARG PORT=4000
ARG COMMIT
ARG PARTPICKER_API_TOKEN
ARG FREENODE_PASSWORD
ARG TRACKER_GG_TOKEN

ENV MIX_ENV=prod
ENV SECRET_KEY_BASE=${SECRET_KEY_BASE}
ENV DATABASE_URL=${DATABASE_URL}
ENV DISCORD_TOKEN=${DISCORD_TOKEN}
ENV DISCORD_CLIENT_ID=${DISCORD_CLIENT_ID}
ENV DISCORD_CLIENT_SECRET=${DISCORD_CLIENT_SECRET}
ENV PARTPICKER_API_TOKEN=${PARTPICKER_API_TOKEN}
ENV TRACKER_GG_TOKEN=${TRACKER_GG_TOKEN}
ENV PORT=${PORT}
ENV COMMIT=${COMMIT}
# install hex + rebar
RUN mix local.hex --force && \
mix local.rebar --force

# set build ENV
ENV MIX_ENV="prod"

# install mix dependencies
COPY mix.exs mix.lock ./
COPY config config
RUN mix do deps.get, deps.compile
RUN mix deps.get --only $MIX_ENV
RUN mkdir config

FROM deps as compile
# copy compile-time config files before we compile dependencies
# to ensure any relevant config change will trigger the dependencies
# to be re-compiled.
COPY config/config.exs config/${MIX_ENV}.exs config/
RUN mix deps.compile

# compile and build release
COPY lib lib
COPY config config
COPY rel rel
COPY priv priv

COPY lib lib

# Compile the release
RUN mix compile

FROM compile as release
# Changes to config/runtime.exs don't require recompiling the code
COPY config/runtime.exs config/

COPY rel rel
RUN mix release

# start a new build stage so that the final image will only contain
# the compiled release and other runtime necessities
FROM ${RUNNER_IMAGE}

RUN apt-get update -y && apt-get install -y libstdc++6 openssl libncurses5 locales \
&& apt-get clean && rm -f /var/lib/apt/lists/*_*

# Set the locale
RUN sed -i '/en_US.UTF-8/s/^# //g' /etc/locale.gen && locale-gen

ENV LANG en_US.UTF-8
ENV LANGUAGE en_US:en
ENV LC_ALL en_US.UTF-8

WORKDIR "/app"
RUN chown nobody /app

# set runner ENV
ENV MIX_ENV="prod"

# Only copy the final release from the build stage
COPY --from=builder --chown=nobody:root /app/_build/${MIX_ENV}/rel/miata_bot ./

RUN mix release --overwrite
USER nobody

FROM app_base as app
COPY --from=release --chown=nobody:nobody /app/_build/prod/rel/miata_bot ./
RUN chown nobody:nobody /app
CMD ["/app/bin/miata_bot", "start"]

USER nobody:nobody
ENV HOME=/app
ENTRYPOINT ["bin/miata_bot", "start"]
# Appended by flyctl
ENV ECTO_IPV6 true
ENV ERL_AFLAGS "-proto_dist inet6_tcp"
5 changes: 0 additions & 5 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ config :miata_bot, MiataBot.Partpicker,
asset_url: System.get_env("PARTPICKER_ASSET_URL"),
gateway_url: System.get_env("PARTPICKER_GATEWAY_URL")

config :miata_bot, TrackerGG, api_token: System.get_env("TRACKER_GG_TOKEN")

config :miata_bot,
ecto_repos: [MiataBot.Repo, Quarrel.Repo]

Expand All @@ -29,13 +27,10 @@ config :quarrel, Quarrel.GuildSupervisor,
MiataBotDiscord.LookingForMiataListener,
MiataBotDiscord.MemesChannelListener,
MiataBotDiscord.SettingsListener,
MiataBotDiscord.SplitgateListener,
MiataBotDiscord.TCGListener,
MiataBotDiscord.TimeInteractionListener
]

config :logger, backends: [:console, RingLogger]

# Configures Elixir's Logger
config :logger, :console,
format: "$time $metadata[$level] $message\n",
Expand Down
2 changes: 1 addition & 1 deletion config/dev.exs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use Mix.Config
import Config

# Configure your database
config :miata_bot, MiataBot.Repo,
Expand Down
31 changes: 0 additions & 31 deletions config/prod.exs
Original file line number Diff line number Diff line change
@@ -1,32 +1 @@
import Config

partpicker_api_token =
System.get_env("PARTPICKER_API_TOKEN") ||
raise """
environment variable PARTPICKER_API_TOKEN is missing.
This must be generated by the partpicker admins for production environment
"""

config :miata_bot, MiataBot.Partpicker,
api_token: partpicker_api_token,
base_url: "https://miatapartpicker.gay/api"

database_url =
System.get_env("DATABASE_URL") ||
raise """
environment variable DATABASE_URL is missing.
For example: ecto://USER:PASS@HOST/DATABASE
"""

quarrel_database_url =
URI.parse(database_url)
|> Map.put(:path, "/miata_bot_quarrel")
|> to_string

config :miata_bot, MiataBot.Repo,
url: database_url,
pool_size: 10

config :quarrel, Quarrel.Repo,
url: quarrel_database_url,
pool_size: 10
43 changes: 43 additions & 0 deletions config/runtime.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import Config

config :tesla, adapter: Tesla.Adapter.Hackney

if System.get_env("RELEASE_MODE") do
partpicker_api_token =
System.get_env("PARTPICKER_API_TOKEN") ||
raise """
environment variable PARTPICKER_API_TOKEN is missing.
This must be generated by the partpicker admins for production environment
"""

config :miata_bot, MiataBot.Partpicker,
api_token: partpicker_api_token,
base_url: "https://partpicker.fly.dev/api"

database_url =
System.get_env("DATABASE_URL") ||
raise """
environment variable DATABASE_URL is missing.
For example: ecto://USER:PASS@HOST/DATABASE
"""

config :miata_bot, MiataBot.Repo,
url: database_url,
pool_size: 10,
socket_options: [:inet6]

quarrel_database_url =
URI.parse(database_url)
|> Map.put(:path, "/miata_bot_quarrel")
|> to_string

config :quarrel, Quarrel.Repo,
url: quarrel_database_url,
pool_size: 10,
socket_options: [:inet6]

config :nostrum,
token: System.get_env("DISCORD_TOKEN"),
gateway_intents: :all,
num_shards: :auto
end
23 changes: 0 additions & 23 deletions docker-compose.yml

This file was deleted.

19 changes: 19 additions & 0 deletions fly.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# fly.toml app configuration file generated for miata-bot on 2023-05-29T16:09:46-06:00
#
# See https://fly.io/docs/reference/configuration/ for information about how to use this file.
#

app = "miata-bot"
primary_region = "den"

[env]
PORT = "8080"
ECTO_IPV6=true
ERL_AFLAGS="-proto_dist inet6_tcp"

[http_service]
internal_port = 8080
force_https = true
auto_stop_machines = false
auto_start_machines = true
min_machines_running = 0
4 changes: 2 additions & 2 deletions lib/miata_bot/copy_pasta.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ defmodule MiataBot.CopyPasta do
import Ecto.Changeset

schema "copy_pastas" do
field :content, :string, null: false
field :created_by_discord_id, Snowflake, null: false
field :content, :string
field :created_by_discord_id, Snowflake
timestamps()
end

Expand Down
Loading

0 comments on commit 07e96ea

Please sign in to comment.