Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ci: fix deployment for railway #14

Merged
merged 37 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
File renamed without changes.
1 change: 1 addition & 0 deletions .env.railway.encrypted
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
eyJpdiI6IjV3ZmRHa2ZUMDhUbDQ2UVVUc3VkY0E9PSIsInZhbHVlIjoiU0R5MzIzQmpBekFXbTVGb3JRdU9aNGhQZVAxbGZCaEhtNkFUeTlMaDZnNU1ZcEZkU1lwS0dEMENYbzZkeGU0eW93VDN2NytlamVRM1NTTllqUzI3VzBVUWxLVm1pRThxWTdWdTdGZnpYd3hkNk9mMFRXa0JTaUVva1BHbkdCTERndmZDTkdLMWprRURVWUk3R3lKbWQyRTRra3V3WG9YeURWTlpHQ0VpVDZ2aHQxTG5oRFEraEsydkJvalFiWmdFV3NiOHFGSVN5V1QyQVhOZzRRa1FDUGZSZXg3ZS9HRURUeVJzM2ZPQkNuNDZscXdtZmtUQkxXUWNYT3BsVFZzenMzamdWOHZpMWlwMWlzSDRtLytBUHdYN25OL2Y1M1dIU1lzWko5VlFPV0paYlJQekRVMWZzK0NJZ3hnNXVHREtseFBNc2ZWZWpKeU9JY2dlTGVBT0hMZ2pnSHhSTUxSWGZmeUlLMC9WN1l1YU5ZRTUwUmE5em9ZcTJuVzl2YmV4OGovR3NhMjFpWitIZ0ZsbTRHQzhJcWZsMkllQ2NsWitGVk5DakFESjFZbC9ZTFV0a2NlZDlLNHlhYTIvMnp2WHp2YmdLRGtsdFRQL1JVZHBRT0xQVjJaelpmKzlQRWNRR3ZTKzJJOG9BKzg1MWdRUVRzT1YrcHpKRHRDSFA0SWxTTmFITVFHT09pbTArRG01UUpDZkhnSTJhb1lUeHN0Nmk3bWRpZEV3ampuZkN1NGYvanlDdEdkOVBFR3BGMUpVam8xeHM2ekRQdFpuUmNOMkhGWFF6eWVQUlJUa1JsNDV0L3J1ZWxZdTltQmNXSVZYMWVUS0gwTE05RENCVzlrZVZON2R0ZlowdW94Sm5PbDV6SitYRkxGUXZNdG92YU1Pc1ZqSmlrMmx0U2M9IiwibWFjIjoiYWE3YTBkMzY5ZGE0OWIxYjk1MmUzN2Y0MjFjMTY0ODY3ZDI0NzdjMWUwZDhlNGRjZmU1MWMyZDRlYzhhN2YzZiIsInRhZyI6IiJ9
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
13 changes: 8 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=development
- LARAVEL_ENV_FILE=.env.development
ports:
- '80:80'
networks:
Expand All @@ -41,14 +44,14 @@ services:
- redis
- meilisearch
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"]
82 changes: 82 additions & 0 deletions dockerfiles/Dockerfile.website
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# 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 config:cache -n
RUN php artisan event:cache -n
RUN php artisan route:cache -n
RUN php artisan view:cache -n
RUN php artisan optimize -n

RUN rm .env.${LARAVEL_ENV}*
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
}
}
Loading