Skip to content

Commit

Permalink
See about moving to fly
Browse files Browse the repository at this point in the history
Reintroduces dockerfiles etc
  • Loading branch information
tomouchuu committed Nov 3, 2023
1 parent 859694f commit 1d5ba2c
Show file tree
Hide file tree
Showing 10 changed files with 342 additions and 50 deletions.
6 changes: 6 additions & 0 deletions .changeset/ten-mangos-divide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@haishin/frontend': minor
'@haishin/backend': minor
---

Playing around with fly migration
240 changes: 200 additions & 40 deletions .github/workflows/deployments.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,61 +46,221 @@ jobs:
publishedPackages: ${{ steps.changesets.outputs.publishedPackages }}
publishedPackageNames: ${{ toJSON(fromJSON(steps.changesets.outputs.publishedPackages).*['name']) }}

publish-frontend:
needs: create-release-pr
name: Publish frontend
build-frontend:
name: Build frontend
runs-on: ubuntu-latest
if: |
needs.create-release-pr.outputs.hasChangesets == 'false' && (
contains(needs.create-release-pr.outputs.publishedPackageNames, '@haishin/tsconfig') == true ||
contains(needs.create-release-pr.outputs.publishedPackageNames, '@haishin/utils') == true ||
contains(needs.create-release-pr.outputs.publishedPackageNames, '@haishin/frontend') == true
)
if:
steps:
- name: Checkout repo
uses: actions/checkout@v3
- name: ⬇️ Checkout repo
uses: actions/checkout@v4

- name: Setup bun
uses: oven-sh/setup-bun@v1
- name: 👀 Read app name
uses: SebRollen/[email protected]
id: app_name
with:
file: 'apps/frontend/fly.toml'
field: 'app'

- name: Install dependencies
run: bun install
- name: 🐳 Set up Docker Buildx
uses: docker/setup-buildx-action@v2
with:
version: v0.9.1

- name: Install Railway
run: npm i -g @railway/cli
# Setup cache
- name: ⚡️ Cache Docker layers
uses: actions/cache@v3
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-buildx-${{ github.sha }}
restore-keys: |
${{ runner.os }}-buildx-
- name: Deploy
run: railway up --service ${{ secrets.RAILWAY_FRONTEND_SERVICE_ID }}
- name: 🔑 Fly Registry Auth
uses: docker/login-action@v2
with:
registry: registry.fly.io
username: x
password: ${{ secrets.FLY_API_TOKEN }}

- name: 🐳 Docker build
uses: docker/build-push-action@v4
with:
context: .
file: apps/frontend/Dockerfile
push: true
tags: registry.fly.io/${{ steps.app_name.outputs.value }}:${{
github.ref_name }}-${{ github.sha }}
build-args: |
BACKEND_URL=${{ secrets.BACKEND_URL }}
COMMIT_SHA=${{ github.sha }}
DEEPL_API_KEY=${{ secrets.DEEPL_API_KEY }}
OPENAI_API_KEY=${{ secrets.OPENAI_API_KEY }}
TURBO_TEAM=${{ secrets.TURBO_TEAM }}
TURBO_TOKEN=${{ secrets.TURBO_TOKEN }}
WS_URL=${{ secrets.WS_URL }}
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,mode=max,dest=/tmp/.buildx-cache-new

# This ugly bit is necessary if you don't want your cache to grow forever
# till it hits GitHub's limit of 5GB.
# Temp fix
# https://github.com/docker/build-push-action/issues/252
# https://github.com/moby/buildkit/issues/1896
- name: 🚚 Move cache
run: |
rm -rf /tmp/.buildx-cache
mv /tmp/.buildx-cache-new /tmp/.buildx-cache
deploy-frontend:
name: 🚀 Deploy Frontend
runs-on: ubuntu-latest
needs: [create-release-pr, build-frontend]
steps:
- name: 🛑 Cancel Previous Runs
uses: styfle/[email protected]

- name: ⬇️ Checkout repo
uses: actions/checkout@v4

- name: 👀 Read app name
uses: SebRollen/[email protected]
id: app_name
with:
file: 'apps/frontend/fly.toml'
field: 'app'

- name: 🚀 Deploy Staging
if: needs.create-release-pr.outputs.hasChangesets == 'true'
uses: superfly/[email protected]
with:
args:
'deploy --app ${{ steps.app_name.outputs.value }}-staging --config
./apps/frontend/fly.toml --image registry.fly.io/${{
steps.app_name.outputs.value }}:${{ github.ref_name }}-${{
github.sha }}'
env:
RAILWAY_TOKEN: ${{ secrets.RAILWAY_TOKEN }}
FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }}

publish-backend:
needs: create-release-pr
name: Publish backend
- name: 🚀 Deploy Production
if: |
needs.create-release-pr.outputs.hasChangesets == 'false' && (
contains(needs.create-release-pr.outputs.publishedPackageNames, '@haishin/tsconfig') == true ||
contains(needs.create-release-pr.outputs.publishedPackageNames, '@haishin/utils') == true ||
contains(needs.create-release-pr.outputs.publishedPackageNames, '@haishin/frontend') == true
)
uses: superfly/[email protected]
with:
args: 'deploy --config ./apps/frontend/fly.toml --image
registry.fly.io/${{ steps.app_name.outputs.value }}:${{
github.ref_name }}-${{ github.sha }}'
env:
FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }}

build-backend:
name: Build backend
runs-on: ubuntu-latest
if: |
needs.create-release-pr.outputs.hasChangesets == 'false' && (
contains(needs.create-release-pr.outputs.publishedPackageNames, '@haishin/tsconfig') == true ||
contains(needs.create-release-pr.outputs.publishedPackageNames, '@haishin/utils') == true ||
contains(needs.create-release-pr.outputs.publishedPackageNames, '@haishin/backend') == true
)
if:
steps:
- name: Checkout repo
uses: actions/checkout@v3
- name: ⬇️ Checkout repo
uses: actions/checkout@v4

- name: Setup bun
uses: oven-sh/setup-bun@v1
- name: 👀 Read app name
uses: SebRollen/[email protected]
id: app_name
with:
file: 'apps/backend/fly.toml'
field: 'app'

- name: Install dependencies
run: bun install
- name: 🐳 Set up Docker Buildx
uses: docker/setup-buildx-action@v2
with:
version: v0.9.1

- name: Install Railway
run: npm i -g @railway/cli
# Setup cache
- name: ⚡️ Cache Docker layers
uses: actions/cache@v3
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-buildx-${{ github.sha }}
restore-keys: |
${{ runner.os }}-buildx-
- name: Deploy
run: railway up --service ${{ secrets.RAILWAY_BACKEND_SERVICE_ID }}
- name: 🔑 Fly Registry Auth
uses: docker/login-action@v2
with:
registry: registry.fly.io
username: x
password: ${{ secrets.FLY_API_TOKEN }}

- name: 🐳 Docker build
uses: docker/build-push-action@v4
with:
context: .
file: apps/backend/Dockerfile
push: true
tags: registry.fly.io/${{ steps.app_name.outputs.value }}:${{
github.ref_name }}-${{ github.sha }}
build-args: |
COMMIT_SHA=${{ github.sha }}
TURBO_TEAM=${{ secrets.TURBO_TEAM }}
TURBO_TOKEN=${{ secrets.TURBO_TOKEN }}
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,mode=max,dest=/tmp/.buildx-cache-new

# This ugly bit is necessary if you don't want your cache to grow forever
# till it hits GitHub's limit of 5GB.
# Temp fix
# https://github.com/docker/build-push-action/issues/252
# https://github.com/moby/buildkit/issues/1896
- name: 🚚 Move cache
run: |
rm -rf /tmp/.buildx-cache
mv /tmp/.buildx-cache-new /tmp/.buildx-cache
deploy-backend:
name: 🚀 Deploy backend
runs-on: ubuntu-latest
needs: [create-release-pr, build-backend]
steps:
- name: 🛑 Cancel Previous Runs
uses: styfle/[email protected]

- name: ⬇️ Checkout repo
uses: actions/checkout@v4

- name: 👀 Read app name
uses: SebRollen/[email protected]
id: app_name
with:
file: 'apps/backend/fly.toml'
field: 'app'

- name: 🚀 Deploy Staging
if: needs.create-release-pr.outputs.hasChangesets == 'true'
uses: superfly/[email protected]
with:
args:
'deploy --app ${{ steps.app_name.outputs.value }}-staging --config
./apps/backend/fly.toml --image registry.fly.io/${{
steps.app_name.outputs.value }}:${{ github.ref_name }}-${{
github.sha }}'
env:
FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }}

- name: 🚀 Deploy Production
if: |
needs.create-release-pr.outputs.hasChangesets == 'false' && (
contains(needs.create-release-pr.outputs.publishedPackageNames, '@haishin/tsconfig') == true ||
contains(needs.create-release-pr.outputs.publishedPackageNames, '@haishin/utils') == true ||
contains(needs.create-release-pr.outputs.publishedPackageNames, '@haishin/backend') == true
)
uses: superfly/[email protected]
with:
args: 'deploy --config ./apps/backend/fly.toml --image
registry.fly.io/${{ steps.app_name.outputs.value }}:${{
github.ref_name }}-${{ github.sha }}'
env:
RAILWAY_TOKEN: ${{ secrets.RAILWAY_TOKEN }}
FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }}

cleanup-old-release-tags:
needs: create-release-pr
Expand Down
44 changes: 44 additions & 0 deletions apps/backend/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
FROM node:18-alpine AS node
FROM oven/bun:alpine AS bun

FROM base AS builder
RUN apk add --no-cache libc6-compat
RUN apk update
RUN pip3 install streamlink
WORKDIR /app
RUN yarn global add turbo
COPY . .
RUN turbo prune --scope @haishin/backend --docker

# Add lockfile and package.json's of isolated subworkspace
FROM bun AS installer
WORKDIR /app

# First install the dependencies (as they change less often)
COPY .gitignore .gitignore
COPY --from=builder /app/out/json/ .
COPY --from=builder /app/out/bun.lockb ./bun.lockb
RUN bun install --production

# Use build args to enable remote caching
ARG TURBO_TEAM
ENV TURBO_TEAM=$TURBO_TEAM

ARG TURBO_TOKEN
ENV TURBO_TOKEN=$TURBO_TOKEN

# Build the project
COPY --from=builder /app/out/full/ .
RUN bunx turbo run build --filter=backend...

# Run the project
FROM bun AS runner
RUN apk add --no-cache g++ make py3-pip ffmpeg
RUN apk update
RUN pip3 install streamlink
WORKDIR /app
COPY --from=installer /app .

CMD ["bun", "apps/backend/src/index.ts"]

EXPOSE 3000
11 changes: 11 additions & 0 deletions apps/backend/fly.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
app = "personal-haishin-be"
primary_region = "lhr"
kill_signal = "SIGINT"
kill_timeout = "5s"

[http_service]
internal_port = 8080
auto_stop_machines = true
auto_start_machines = true
min_machines_running = 0
processes = ["app"]
3 changes: 0 additions & 3 deletions apps/backend/nixpacks.toml

This file was deleted.

2 changes: 0 additions & 2 deletions apps/backend/railway.toml

This file was deleted.

Loading

0 comments on commit 1d5ba2c

Please sign in to comment.