feat: add Dockerfile, docker-compose, nginx.conf, deploy-yml
This commit is contained in:
parent
aad4368740
commit
915c8733ea
|
@ -0,0 +1,103 @@
|
|||
name: CI/CD Laravel Docker
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [main]
|
||||
pull_request:
|
||||
branches: [main]
|
||||
|
||||
jobs:
|
||||
build-and-test:
|
||||
runs-on: self-hosted
|
||||
|
||||
services:
|
||||
mysql:
|
||||
image: mysql:8.0
|
||||
env:
|
||||
MYSQL_DATABASE: laravel
|
||||
MYSQL_ROOT_PASSWORD: root
|
||||
ports:
|
||||
- 3306:3306
|
||||
options: >-
|
||||
--health-cmd="mysqladmin ping --silent"
|
||||
--health-interval=10s
|
||||
--health-timeout=5s
|
||||
--health-retries=3
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
|
||||
- name: Setup PHP
|
||||
uses: shivammathur/setup-php@v2
|
||||
with:
|
||||
php-version: '8.2'
|
||||
extensions: mbstring, pdo_mysql, xml, bcmath, curl, mysql
|
||||
coverage: none
|
||||
ini-values: post_max_size=256M, upload_max_filesize=256M
|
||||
|
||||
- name: Copy .env
|
||||
run: cp .env.example .env
|
||||
|
||||
- name: Install Composer dependencies
|
||||
run: composer install --prefer-dist --no-progress --no-suggest --no-interaction
|
||||
|
||||
- name: Generate application key
|
||||
run: php artisan key:generate
|
||||
|
||||
- name: Install NPM dependencies
|
||||
run: npm ci
|
||||
|
||||
- name: Build assets
|
||||
run: npm run build
|
||||
|
||||
- name: Run migrations
|
||||
env:
|
||||
DB_HOST: 127.0.0.1
|
||||
DB_PORT: 3306
|
||||
DB_DATABASE: laravel
|
||||
DB_USERNAME: root
|
||||
DB_PASSWORD: root
|
||||
run: php artisan migrate --force
|
||||
|
||||
- name: Run tests
|
||||
env:
|
||||
DB_HOST: 127.0.0.1
|
||||
DB_PORT: 3306
|
||||
DB_DATABASE: laravel
|
||||
DB_USERNAME: root
|
||||
DB_PASSWORD: root
|
||||
run: php artisan test --parallel
|
||||
|
||||
deploy:
|
||||
needs: build-and-test
|
||||
runs-on: sel-hosted
|
||||
if: github.ref == 'refs/heads/main'
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
|
||||
- name: Build Docker image
|
||||
run: docker build -t laravel-app:latest .
|
||||
|
||||
- name: Save Docker image as tar
|
||||
run: docker save laravel-app:latest -o laravel-app.tar
|
||||
|
||||
- name: Copy Docker image to server
|
||||
uses: appleboy/scp-action@v0.1.7
|
||||
with:
|
||||
host: ${{ secrets.VPS_HOST }}
|
||||
username: ${{ secrets.VPS_USER }}
|
||||
key: ${{ secrets.VPS_SSH_KEY }}
|
||||
source: "laravel-app.tar"
|
||||
target: "~/"
|
||||
|
||||
- name: Deploy on server via SSH
|
||||
uses: appleboy/ssh-action@v1.0.0
|
||||
with:
|
||||
host: ${{ secrets.VPS_HOST }}
|
||||
username: ${{ secrets.VPS_USER }}
|
||||
key: ${{ secrets.VPS_SSH_KEY }}
|
||||
script: |
|
||||
cd ~/smk4-web
|
||||
git pull origin main
|
||||
docker-compose down
|
||||
docker-compose up -d --build
|
|
@ -0,0 +1,29 @@
|
|||
FROM composer:2.7 as build
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
COPY . .
|
||||
|
||||
RUN composer install --no-dev --optimize-autoloader
|
||||
RUN npm ci && npm run build
|
||||
|
||||
FROM php:8.2-fpm
|
||||
|
||||
COPY --from=build /app /var/www
|
||||
|
||||
RUN apt-get update && apt-get install -y \
|
||||
libzip-dev \
|
||||
libpng-dev \
|
||||
libjpeg-dev \
|
||||
libfreetype6-dev \
|
||||
libonig-dev \
|
||||
libxml2-dev \
|
||||
zip \
|
||||
git \
|
||||
unzip
|
||||
|
||||
RUN docker-php-ext-install pdo_mysql mbstring exif pcntl bcmath gd
|
||||
|
||||
COPY --from=build /app /var/www
|
||||
|
||||
CMD ["php-fpm"]
|
|
@ -0,0 +1,51 @@
|
|||
version: '3.8'
|
||||
|
||||
services:
|
||||
app:
|
||||
build: .
|
||||
image: laravel-app:latest
|
||||
container_name: laravel-app
|
||||
restart: unless-stopped
|
||||
env_file: .env
|
||||
volumes:
|
||||
- .:/var/www
|
||||
depends_on:
|
||||
- db
|
||||
networks:
|
||||
- laravel
|
||||
|
||||
db:
|
||||
image: mysql:8.0
|
||||
container_name: laravel-db
|
||||
restart: unless-stopped
|
||||
environment:
|
||||
MYSQL_DATABASE: laravel
|
||||
MYSQL_ROOT_PASSWORD: root
|
||||
MYSQL_USER: laravel
|
||||
MYSQL_PASSWORD: laravel
|
||||
ports:
|
||||
- "3306:3306"
|
||||
volumes:
|
||||
- dbdata:/var/lib/mysql
|
||||
networks:
|
||||
- laravel
|
||||
|
||||
nginx:
|
||||
image: nginx:alpine
|
||||
container_name: laravel-nginx
|
||||
restart: unless-stopped
|
||||
ports:
|
||||
- "80:80"
|
||||
volumes:
|
||||
- .:/var/www
|
||||
- ./nginx.conf:/etc/nginx/conf.d/default.conf
|
||||
depends_on:
|
||||
- app
|
||||
networks:
|
||||
- laravel
|
||||
|
||||
volumes:
|
||||
dbdata:
|
||||
|
||||
networks:
|
||||
laravel:
|
|
@ -0,0 +1,18 @@
|
|||
server {
|
||||
listen 80;
|
||||
index index.html index.htm index.php;
|
||||
server_name _;
|
||||
|
||||
root /var/www/public;
|
||||
|
||||
location / {
|
||||
try_files $uri $uri/ /index.php?query_string;
|
||||
}
|
||||
|
||||
location ~ \.php$ {
|
||||
fastcgi_pass app:9000;
|
||||
fastcgi_index index.php;
|
||||
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
|
||||
include fastcgi_params;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue