From 915c8733eafe3b2cea288451dabb1ade2766a6de Mon Sep 17 00:00:00 2001 From: LatansaBima Date: Thu, 12 Jun 2025 13:54:32 +0700 Subject: [PATCH] feat: add Dockerfile, docker-compose, nginx.conf, deploy-yml --- .github/workflows/deploy.yml | 103 +++++++++++++++++++++++++++++++++++ Dockerfile | 29 ++++++++++ docker-compose.yml | 51 +++++++++++++++++ nginx.conf | 18 ++++++ 4 files changed, 201 insertions(+) create mode 100644 .github/workflows/deploy.yml create mode 100644 Dockerfile create mode 100644 docker-compose.yml create mode 100644 nginx.conf diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml new file mode 100644 index 0000000..9831a29 --- /dev/null +++ b/.github/workflows/deploy.yml @@ -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 \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..826f5f6 --- /dev/null +++ b/Dockerfile @@ -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"] \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..bb8df10 --- /dev/null +++ b/docker-compose.yml @@ -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: \ No newline at end of file diff --git a/nginx.conf b/nginx.conf new file mode 100644 index 0000000..ca7f818 --- /dev/null +++ b/nginx.conf @@ -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; + } +} \ No newline at end of file