Skip to content

Commit

Permalink
chore: setup fly.io for deplyments
Browse files Browse the repository at this point in the history
  • Loading branch information
Adriana Cardoso committed Nov 4, 2024
1 parent 4db503d commit 513db11
Show file tree
Hide file tree
Showing 9 changed files with 258 additions and 24 deletions.
19 changes: 19 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Dependencies and AdonisJS build
node_modules
build
tmp

# Secrets
*.env*
!.env.example

# Build tools specific
*.log

# Editors specific
.fleet
.idea
.vscode

# Platform specific
.DS_Store
18 changes: 18 additions & 0 deletions .github/workflows/fly-deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# See https://fly.io/docs/app-guides/continuous-deployment-with-github-actions/

name: Fly Deploy
on:
push:
branches:
- main
jobs:
deploy:
name: Deploy app
runs-on: ubuntu-latest
concurrency: deploy-group # optional: ensure only one action runs at a time
steps:
- uses: actions/checkout@v4
- uses: superfly/flyctl-actions/setup-flyctl@master
- run: flyctl deploy --remote-only
env:
FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }}
6 changes: 2 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ build
tmp

# Secrets
.env
.env.local
.env.production.local
.env.development.local
*.env*
!.env.example

# Frontend assets compiled code
public/assets
Expand Down
49 changes: 49 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# syntax = docker/dockerfile:1
ARG NODE_VERSION=21.6.1
FROM node:${NODE_VERSION}-slim AS base

LABEL fly_launch_runtime="AdonisJS"

# AdonisJS app lives here
WORKDIR /app

# Set production environment
ENV NODE_ENV="production"
ARG YARN_VERSION=1.22.19
RUN npm install -g yarn@$YARN_VERSION --force

# Throw-away build stage to reduce size of final image
FROM base AS build

# Install packages needed to build node modules
RUN apt-get update -qq && \
apt-get install --no-install-recommends -y build-essential node-gyp pkg-config python-is-python3

# Install node modules
COPY package.json yarn.lock ./
RUN yarn install --frozen-lockfile --production=false

# Copy application code
COPY . .

# Build application
RUN yarn run build

# Final stage for app image
FROM base

# Copy built application
COPY --from=build /app /app

# Entrypoint sets up the container.
ENTRYPOINT [ "/app/docker-entrypoint.js" ]

# Start the server by default, this can be overwritten at runtime
EXPOSE 3000
ENV CACHE_VIEWS="true" \
DB_CONNECTION="pg" \
DRIVE_DISK="local" \
HOST="0.0.0.0" \
PORT="3000" \
SESSION_DRIVER="cookie"
CMD [ "node", "build/bin/server.js" ]
1 change: 0 additions & 1 deletion config/inertia.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import User from '#models/user'
import { defineConfig } from '@adonisjs/inertia'
import type { InferSharedProps } from '@adonisjs/inertia/types'
import { ModelObject } from '@adonisjs/lucid/types/model'

const inertiaConfig = defineConfig({
/**
Expand Down
36 changes: 36 additions & 0 deletions docker-entrypoint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/env node

import { spawn } from 'node:child_process'

const env = { ...process.env }

if (process.env.DATABASE_URL) {
try {
const databaseUrl = new URL(process.env.DATABASE_URL)
env.PG_HOST = databaseUrl.hostname
env.PG_PORT = databaseUrl.port
env.PG_USER = databaseUrl.username
env.PG_PASSWORD = databaseUrl.password
env.PG_DB_NAME = databaseUrl.pathname.slice(1)
} catch (err) {
console.error('Invalid DATABASE_URL')
}
}

;(async() => {
// launch application
await exec(process.argv.slice(2).join(' '))
})()

function exec(command) {
const child = spawn(command, { shell: true, stdio: 'inherit', env })
return new Promise((resolve, reject) => {
child.on('exit', code => {
if (code === 0) {
resolve()
} else {
reject(new Error(`${command} failed rc=${code}`))
}
})
})
}
22 changes: 22 additions & 0 deletions fly.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# fly.toml app configuration file generated for social-adonis on 2024-11-04T11:14:11Z
#
# See https://fly.io/docs/reference/configuration/ for information about how to use this file.
#

app = 'social-adonis'
primary_region = 'cdg'

[build]

[http_service]
internal_port = 3000
force_https = true
auto_stop_machines = 'stop'
auto_start_machines = true
min_machines_running = 0
processes = ['app']

[[vm]]
memory = '1gb'
cpu_kind = 'shared'
cpus = 1
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"@adonisjs/prettier-config": "^1.3.0",
"@adonisjs/tsconfig": "^1.3.0",
"@faker-js/faker": "^9.0.0",
"@flydotio/dockerfile": "^0.5.9",
"@japa/assert": "^3.0.0",
"@japa/plugin-adonisjs": "^3.0.1",
"@japa/runner": "^3.1.4",
Expand Down
Loading

0 comments on commit 513db11

Please sign in to comment.