103 lines
2.6 KiB
YAML
103 lines
2.6 KiB
YAML
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 migration
|
|
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 |