Skip to content

Commit

Permalink
Merge pull request #14 from NIAEFEUP/ci/fix-deployment-for-railway
Browse files Browse the repository at this point in the history
  • Loading branch information
limwa committed Jul 29, 2024
2 parents 1fc1daa + 8ce08ec commit 10d2a33
Show file tree
Hide file tree
Showing 14 changed files with 198 additions and 47 deletions.
2 changes: 1 addition & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
bootstrap/cache/services.php
bootstrap/cache/*

storage/app/*
storage/framework/cache/*
Expand Down
35 changes: 0 additions & 35 deletions .env

This file was deleted.

1 change: 1 addition & 0 deletions .env.railway.encrypted
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
eyJpdiI6IjM4ckdUdk1XazMxQjA4RnpTRUZhc3c9PSIsInZhbHVlIjoib1Q3UWtxdzdjSytwMGVRMW9MT2lERlVxclpTK05mUDk1dlhJd2NwbmtkRnFWZTExV2NZdEROMG8zb2N5ZEN1MW5tMk5JbHMxdDh4Z0NsSzYvdy9TdWNKQ3hBMVhncVMxZkFiSkJLR2VZa0JRMUZYbnF0YXJya0NsN2UwemVkMGpnaUw5Y1Z6enBtcnBPWkR3MW1mRENnU2cvcm9rSXNLMzllV3I0QitKL0xvR0J4MHhhWCtMZERYd3pNeHJiMnBINW42a1dCWkVBNmRhTU14T3FvVzgrMjh3ODd3LzMxcnUzcmRWTjZ2OW1qMis2dkVvRWtINlBmN3YzSXR4Mm12VnJFeGZyckdtL3dwQnhvRWIrTEl0WFJNU2ZEbE5pOTJEaW9LOHlLZ0ZEMjlYdTBBakJma3NvRDdidUZ0bVRkWnZUVmtCVG1oTFBZZEVOa0xyNVdnTExzdGxNL3JLcjJOQ2VqTEh4VGV0LzBJMm5WNm5VWlVRMU50bng3UzhkVnVzVkNGWWtYYTBjS1VEbXNLMk02RHhoQnFUbWVZZGREOEhPQURpejhBY3IwVHlGNGs5THZraUZ2NTZxdTB5L095bFk1d25Za3JiNU1mSUU1YUlMV0VHZzRNVVJOa0dQRXFjM3MvY0ZJSEh2eDZITFMrbDFzV1JaaUFHRWcrV1Fjb0lEVWNKaEFIVFAya1I2RUZZZi9odS82OVZmSTNhek40L2dub2IrYlkvM29rd01uRytiZEVJQ3cwaTZoaHRUeXNDTVEzd0hiTVNsc2NzMWRhOUJpSUJFQjg3dlRkdHE4WTkvdmVudFZ2YjZtY1Ryd1dNWjZ5SGxWZkhMeEhFR2MycSIsIm1hYyI6ImFiZTIyM2NkOTYxYmNkNDA4ZWViZWVlOGIyY2IzNGEwMGFlOGQ4NGM1MGYwNDY2ZWE4ODY5YjllN2ZiOWUxZTgiLCJ0YWciOiIifQ==
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
/storage/*.key
/vendor
.env.backup
.env.prod
.env.production
.env.railway
.phpunit.result.cache
Homestead.json
Homestead.yaml
Expand Down
3 changes: 0 additions & 3 deletions Dockerfile → Dockerfile.old
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ RUN cat ./supervisord.conf >> /etc/supervisor/conf.d/supervisord.conf

USER nobody

COPY --chown=nobody ./.env.prod ./.env

COPY --chown=nobody --from=dependencies /app/vendor ./vendor
COPY --chown=nobody --from=assets /app/public ./public
COPY --chown=nobody ./app ./app
Expand All @@ -67,7 +65,6 @@ COPY --chown=nobody ./resources ./resources
COPY --chown=nobody ./routes ./routes
COPY --chown=nobody ./storage ./storage

RUN php artisan key:generate --force
RUN php artisan storage:link
RUN php artisan config:cache
RUN php artisan event:cache
Expand Down
37 changes: 37 additions & 0 deletions app/Console/Commands/CreateDbSchema.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class CreateDbSchema extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'app:create-db-schema';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';

/**
* Execute the console command.
*/
public function handle()
{
$conn = DB::connectUsing("pgsql", [
...config('database.connections.pgsql'),
'search_path' => "public",
]);

$search_path = config('database.connections.pgsql.search_path');
$conn->statement("CREATE SCHEMA IF NOT EXISTS $search_path");
}
}
2 changes: 1 addition & 1 deletion config/database.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
'charset' => 'utf8',
'prefix' => '',
'prefix_indexes' => true,
'search_path' => 'public',
'search_path' => str_replace('-', '_', env('DB_SEARCH_PATH', 'public')),
'sslmode' => 'prefer',
],

Expand Down
15 changes: 10 additions & 5 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,11 @@ services:
prod:
build:
context: .
dockerfile: Dockerfile
target: production
dockerfile: ./dockerfiles/Dockerfile.website
target: deploy
args:
- LARAVEL_ENV=local
- LARAVEL_ENV_FILE=.env
ports:
- '80:80'
networks:
Expand All @@ -40,15 +43,17 @@ services:
- pgsql
- redis
- meilisearch
environment:
- APP_URL=http://localhost
pgsql:
image: 'postgres:15'
image: 'postgres:16'
ports:
- '${FORWARD_DB_PORT:-5432}:5432'
environment:
PGPASSWORD: '${DB_PASSWORD:-secret}'
PGPASSWORD: '${DB_PASSWORD}'
POSTGRES_DB: '${DB_DATABASE}'
POSTGRES_USER: '${DB_USERNAME}'
POSTGRES_PASSWORD: '${DB_PASSWORD:-secret}'
POSTGRES_PASSWORD: '${DB_PASSWORD}'
volumes:
- 'sail-pgsql:/var/lib/postgresql/data'
- './vendor/laravel/sail/database/pgsql/create-testing-database.sql:/docker-entrypoint-initdb.d/10-create-testing-database.sql'
Expand Down
35 changes: 35 additions & 0 deletions dockerfiles/Dockerfile.seeder
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# dependencies
FROM composer:2 AS dependencies
WORKDIR /app

COPY composer.json composer.lock artisan ./
COPY app/ ./app/
COPY bootstrap/ ./bootstrap/
COPY config/ ./config/
COPY database/ ./database/
COPY resources/ ./resources/
COPY routes/ ./routes/

RUN composer install --no-interaction

# migrate
FROM composer:2 AS migrate
WORKDIR /app

RUN install-php-extensions pdo_pgsql pgsql redis iconv zip

COPY --from=dependencies /app ./

ARG LARAVEL_ENV=production
ARG LARAVEL_ENV_FILE=.env.${LARAVEL_ENV}
ARG LARAVEL_ENV_FILE_KEY=""

ENV APP_ENV=${LARAVEL_ENV}

COPY --chown=nobody ${LARAVEL_ENV_FILE} ./.env.${LARAVEL_ENV}
RUN [ -z "${LARAVEL_ENV_FILE_KEY}" ] || ( mv .env.${LARAVEL_ENV} .env.${LARAVEL_ENV}.encrypted && php artisan env:decrypt -n --env=${LARAVEL_ENV} --key=${LARAVEL_ENV_FILE_KEY} )

COPY public/ ./public/
COPY storage/ ./storage/

CMD [ "php", "artisan", "migrate", "-n"]
81 changes: 81 additions & 0 deletions dockerfiles/Dockerfile.website
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# dev-dependencies
FROM composer:2 AS dev-dependencies
WORKDIR /app

COPY composer.json composer.lock ./
RUN composer install --no-interaction --no-autoloader

# assets-build
FROM node:20-alpine AS assets-build
WORKDIR /app

COPY package.json package-lock.json ./
RUN npm install

COPY ./resources/ ./resources
COPY ./vite.config.ts ./tsconfig.json ./tsconfig.app.json ./tsconfig.node.json ./tailwind.config.js ./postcss.config.js ./
COPY --from=dev-dependencies /app/vendor ./vendor

RUN npm run build

# laravel-build
FROM composer:2 AS laravel-build
WORKDIR /app

COPY composer.json composer.lock artisan ./
COPY app/ ./app/
COPY bootstrap/ ./bootstrap/
COPY config/ ./config/
COPY database/ ./database/
COPY resources/ ./resources/
COPY routes/ ./routes/

RUN composer install --no-interaction --optimize-autoloader --no-dev --prefer-dist

# deploy
FROM trafex/php-nginx:3.6.0 AS deploy
WORKDIR /var/www/html

USER root
RUN apk add --no-cache php83-pdo_pgsql php83-pgsql php83-pecl-redis php83-iconv php83-zip

## Install supercronic (cron alternative)
ENV SUPERCRONIC_URL=https://github.com/aptible/supercronic/releases/download/v0.2.26/supercronic-linux-amd64 \
SUPERCRONIC=supercronic-linux-amd64 \
SUPERCRONIC_SHA1SUM=7a79496cf8ad899b99a719355d4db27422396735

RUN curl -fsSLO "$SUPERCRONIC_URL" \
&& echo "${SUPERCRONIC_SHA1SUM} ${SUPERCRONIC}" | sha1sum -c - \
&& chmod +x "$SUPERCRONIC" \
&& mv "$SUPERCRONIC" "/usr/local/bin/${SUPERCRONIC}" \
&& ln -s "/usr/local/bin/${SUPERCRONIC}" /usr/local/bin/supercronic

COPY ./etc/nginx/default.conf /etc/nginx/conf.d/default.conf
COPY ./etc/php/php.ini ${PHP_INI_DIR}/conf.d/php.ini
COPY --chown=nobody ./etc/crontab ./crontab
COPY --chown=nobody ./etc/supervisord.conf ./supervisord.conf
RUN cat ./supervisord.conf >> /etc/supervisor/conf.d/supervisord.conf
RUN rm ./supervisord.conf

USER nobody

ARG LARAVEL_ENV=production
ARG LARAVEL_ENV_FILE=.env.${LARAVEL_ENV}
ARG LARAVEL_ENV_FILE_KEY=""

COPY --chown=nobody ${LARAVEL_ENV_FILE} ./.env.${LARAVEL_ENV}
COPY --chown=nobody public/ ./public/
COPY --chown=nobody storage/ ./storage/
COPY --chown=nobody --from=laravel-build /app/ ./
COPY --chown=nobody --from=assets-build /app/public/ ./public/

ENV APP_ENV=${LARAVEL_ENV}

RUN [ -z "${LARAVEL_ENV_FILE_KEY}" ] || ( mv .env.${LARAVEL_ENV} .env.${LARAVEL_ENV}.encrypted && php artisan env:decrypt -n --env=${LARAVEL_ENV} --key=${LARAVEL_ENV_FILE_KEY} )
RUN php artisan storage:link -n
RUN php artisan event:cache -n
RUN php artisan route:cache -n
RUN php artisan view:cache -n

COPY --chown=nobody ./etc/entrypoint.sh /entrypoint.sh
CMD [ "/entrypoint.sh" ]
9 changes: 9 additions & 0 deletions etc/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/sh

set -e

# Cache config and other stuff
php artisan optimize -n
rm .env.${LARAVEL_ENV}*

/usr/bin/supervisord -c /etc/supervisor/conf.d/supervisord.conf
1 change: 0 additions & 1 deletion etc/nginx/default.conf
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
server {
listen 80;
listen [::]:80;
server_name .sinf.pt;
root /var/www/html/public;

add_header X-Frame-Options "SAMEORIGIN";
Expand Down
11 changes: 11 additions & 0 deletions seeder.railway.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"$schema": "https://railway.app/railway.schema.json",
"build": {
"builder": "DOCKERFILE",
"dockerfilePath": "./dockerfiles/Dockerfile.seeder"
},
"deploy": {
"restartPolicyType": "NEVER",
"sleepApplication": false
}
}
10 changes: 10 additions & 0 deletions website.railway.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"$schema": "https://railway.app/railway.schema.json",
"build": {
"builder": "DOCKERFILE",
"dockerfilePath": "./dockerfiles/Dockerfile.website"
},
"deploy": {
"sleepApplication": true
}
}

0 comments on commit 10d2a33

Please sign in to comment.