Skip to content

Commit

Permalink
Merge pull request #25 from NickMous/feature/add-docker
Browse files Browse the repository at this point in the history
Add docker
  • Loading branch information
NickMous authored Aug 22, 2024
2 parents d3bcfb9 + 436ca58 commit 95bc47b
Show file tree
Hide file tree
Showing 18 changed files with 383 additions and 133 deletions.
42 changes: 42 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Include any files or directories that you don't want to be copied to your
# container here (e.g., local build artifacts, temporary files, etc.).
#
# For more help, visit the .dockerignore file reference guide at
# https://docs.docker.com/go/build-context-dockerignore/

**/.classpath
**/.ddev
**/.dockerignore
**/.git
**/.gitignore
**/.project
**/.settings
**/.toolstarget
**/.vs
**/.vscode
**/.next
**/.cache
**/*.*proj.user
**/*.dbmdl
**/*.jfm
**/charts
**/docker-compose*
**/compose.y*ml
!**/composer.json
!**/composer.lock
**/Dockerfile*
**/node_modules
**/npm-debug.log
**/obj
**/secrets.dev.yaml
**/values.dev.yaml
**/vendor
.editorconfig
.gitattributes
.prettierignore
.prettierrc
eslint.config.js
phpstan.neon
LICENSE
README.md
README.Docker.md
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ trim_trailing_whitespace = false
[*.{yml,yaml}]
indent_size = 2

[docker-compose.yml]
[{docker-compose.yml, docker-compose.*.yml}]
indent_size = 4
12 changes: 6 additions & 6 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
APP_NAME=Laravel
APP_NAME=CycleQuest
APP_ENV=local
APP_KEY=
APP_DEBUG=true
APP_TIMEZONE=UTC
APP_URL=http://localhost
APP_URL=http://localhost:1501

APP_LOCALE=en
APP_FALLBACK_LOCALE=en
APP_FAKER_LOCALE=en_US
APP_LOCALE=nl
APP_FALLBACK_LOCALE=nl
APP_FAKER_LOCALE=nl_NL

APP_MAINTENANCE_DRIVER=file
APP_MAINTENANCE_STORE=database
Expand All @@ -22,7 +22,7 @@ LOG_LEVEL=debug
DB_CONNECTION=mariadb
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=cyclequest_lvl11
DB_DATABASE=db
DB_USERNAME=root
DB_PASSWORD=

Expand Down
114 changes: 39 additions & 75 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,93 +14,57 @@ jobs:
runs-on: ubuntu-latest

services:
mariadb:
image: mariadb:10.11
env:
MYSQL_ROOT_PASSWORD: root_password
MYSQL_DATABASE: test
MYSQL_USER: test_user
MYSQL_PASSWORD: test_password
docker:
image: docker
options: --privileged
ports:
- 3306:3306
options: >-
--health-cmd="mysqladmin ping --silent"
--health-interval=10s
--health-timeout=5s
--health-retries=3

steps:
- uses: shivammathur/setup-php@15c43e89cdef867065b0213be354c2841860869e
- name: Checkout code
uses: actions/checkout@v3
- name: Login to Docker Hub
uses: docker/login-action@v3
with:
php-version: "8.3"
- uses: actions/checkout@v3
- name: Copy .env
run: php -r "file_exists('.env') || copy('.env.example', '.env');"
- name: Install Composer Dependencies
run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist
- name: Install NPM Dependencies
run: npm install
- name: Run build
run: npm run build
- name: Generate key
run: php artisan key:generate
- name: Directory Permissions
run: chmod -R 777 storage bootstrap/cache
- name: Create Database
username: ${{ vars.DOCKER_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Copy password file
run: cp database/password.txt.example database/password.txt
- name: Copy .env file
run: cp .env.example .env
- name: Set app key
run: sed -i "s|APP_KEY=|APP_KEY=base64:$(openssl rand -base64 32)|" .env
- name: Set up Docker Compose
run: |
mkdir -p database
touch database/database.sqlite
- name: Execute tests (Unit and Feature tests) via Pest
env:
DB_CONNECTION: mariadb
DB_DATABASE: test
DB_USERNAME: test_user
DB_PASSWORD: test_password
run: vendor/bin/pest
docker compose -f compose.testing.yaml up -d
- name: Run tests
run: |
docker compose -f compose.testing.yaml --env-file .env exec server-testing ./vendor/bin/pest tests
- name: Tear down Docker Compose
run: |
docker compose -f compose.testing.yaml down
deploy:
needs: laravel-tests

runs-on: ubuntu-latest

steps:
- name: Activate maintenance mode 🛠️
uses: appleboy/ssh-action@master
- name: Checkout code
uses: actions/checkout@v3
- name: Login to Docker Hub
uses: docker/login-action@v3
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
password: ${{ secrets.PASSWORD }}
port: ${{ secrets.SSHPORT }}
script: |
cd ${{ env.PUBLIC_PATH }}
(php artisan down --secret="bypass-nick" --refresh="15") || true
- name: Deploy code to production 🚀
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
password: ${{ secrets.PASSWORD }}
port: ${{ secrets.SSHPORT }}
script: |
export NVM_DIR=~/.nvm
source ~/.nvm/nvm.sh
cd ${{ env.PUBLIC_PATH }}
git pull
composer install --optimize-autoloader --no-dev --no-interaction --prefer-dist
php artisan clear-compiled
npm ci
php artisan migrate --force
npm run build
php artisan optimize
- name: Deactivate maintenance mode 🛠️
uses: appleboy/ssh-action@master
username: ${{ vars.DOCKER_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Build and push Docker image
uses: docker/build-push-action@v6
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
password: ${{ secrets.PASSWORD }}
port: ${{ secrets.SSHPORT }}
script: |
cd ${{ env.PUBLIC_PATH }}
php artisan up
platform: linux/amd64,linux/arm64
push: true
target: prod
tags: ${{ secrets.DOCKER_USERNAME }}/cyclequest:latest
64 changes: 26 additions & 38 deletions .github/workflows/laravel.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,41 @@ name: Laravel Pest Tests

on:
pull_request:
branches: ["main"]
branches: [ "main" ]

jobs:
laravel-tests:
runs-on: ubuntu-latest

services:
mariadb:
image: mariadb:10.11
env:
MYSQL_ROOT_PASSWORD: root_password
MYSQL_DATABASE: test
MYSQL_USER: test_user
MYSQL_PASSWORD: test_password
docker:
image: docker
options: --privileged
ports:
- 3306:3306
options: >-
--health-cmd="mysqladmin ping --silent"
--health-interval=10s
--health-timeout=5s
--health-retries=3

steps:
- uses: shivammathur/setup-php@15c43e89cdef867065b0213be354c2841860869e
- name: Checkout code
uses: actions/checkout@v3
- name: Login to Docker Hub
uses: docker/login-action@v3
with:
php-version: "8.3"
- uses: actions/checkout@v3
- name: Copy .env
run: php -r "file_exists('.env') || copy('.env.example', '.env');"
- name: Install Composer Dependencies
run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist
- name: Install NPM Dependencies
run: npm install
- name: Run build
run: npm run build
- name: Generate key
run: php artisan key:generate
- name: Directory Permissions
run: chmod -R 777 storage bootstrap/cache
- name: Create Database
username: ${{ vars.DOCKER_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Copy password file
run: cp database/password.txt.example database/password.txt
- name: Copy .env file
run: cp .env.example .env
- name: Set app key
run: sed -i "s|APP_KEY=|APP_KEY=base64:$(openssl rand -base64 32)|" .env
- name: Set up Docker Compose
run: |
mkdir -p database
touch database/database.sqlite
- name: Execute tests (Unit and Feature tests) via Pest
env:
DB_CONNECTION: mariadb
DB_DATABASE: test
DB_USERNAME: test_user
DB_PASSWORD: test_password
run: vendor/bin/pest
docker compose -f compose.testing.yaml up -d
- name: Run tests
run: |
docker compose -f compose.testing.yaml --env-file .env exec server-testing ./vendor/bin/pest tests
- name: Tear down Docker Compose
run: |
docker compose -f compose.testing.yaml down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@ yarn-error.log
/.idea
/.vscode
sitemap.xml
.DS_Store
/data
81 changes: 81 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# syntax=docker/dockerfile:1
FROM composer:lts as deps-composer-base
WORKDIR /app
COPY artisan /app/
COPY app /app/app/
COPY bootstrap /app/bootstrap/
COPY config /app/config/
COPY database /app/database/
COPY lang /app/lang/
COPY public /app/public/
COPY resources /app/resources/
COPY routes /app/routes/
COPY storage /app/storage/

FROM deps-composer-base as deps-composer-prod
RUN --mount=type=bind,source=composer.json,target=composer.json \
--mount=type=bind,source=composer.lock,target=composer.lock \
--mount=type=cache,target=/tmp/cache \
composer install --no-interaction

FROM deps-composer-base as deps-composer-dev
RUN --mount=type=bind,source=composer.json,target=composer.json \
--mount=type=bind,source=composer.lock,target=composer.lock \
--mount=type=cache,target=/tmp/cache \
composer install --no-interaction

FROM node:latest as deps-node-base
WORKDIR /app
COPY package.json /app/
COPY package-lock.json /app/
COPY vite.config.js /app/
COPY tailwind.config.js /app/
COPY postcss.config.js /app/

FROM deps-node-base as deps-node-prod
COPY --from=deps-composer-prod /app /app
RUN npm install --production
RUN npm run build

FROM deps-node-base as deps-node-dev
COPY --from=deps-composer-dev /app /app
RUN npm install
RUN npm run build

FROM php:8.3-apache as base
RUN docker-php-ext-install pdo_mysql
COPY .env /var/www/html/.env

# Look at the .env file if DB_PASSWORD is set, otherwise use the password in the secret
# The secret is mounted in /run/secrets/db-password
RUN --mount=type=secret,id=db-password \
sed -i "s/DB_PASSWORD=/DB_PASSWORD=\"$(cat /run/secrets/db-password)\"/" /var/www/html/.env

# This is for tinker
RUN mkdir /var/www/.config && chown -R www-data:www-data /var/www/.config
ENV APACHE_DOCUMENT_ROOT /var/www/html/public
RUN sed -ri -e 's!/var/www/html!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/sites-available/*.conf
RUN sed -ri -e 's!/var/www/!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/apache2.conf /etc/apache2/conf-available/*.conf
RUN a2enmod rewrite


FROM base as prod
RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini"
COPY --from=deps-composer-prod /app /var/www/html
COPY --from=deps-node-prod /app /var/www/html
COPY composer.json /var/www/html/composer.json
RUN chown -R www-data:www-data /var/www/html
USER www-data
RUN php /var/www/html/artisan optimize
RUN php /var/www/html/artisan config:cache

FROM base as dev
RUN mv "$PHP_INI_DIR/php.ini-development" "$PHP_INI_DIR/php.ini"
COPY --from=deps-composer-dev /app /var/www/html
COPY --from=deps-node-dev /app /var/www/html
COPY composer.json /var/www/html/composer.json
COPY tests /var/www/html/tests/
COPY phpunit.xml /var/www/html/phpunit.xml
RUN chown -R www-data:www-data /var/www/html

FROM prod as test
Loading

0 comments on commit 95bc47b

Please sign in to comment.