XCrawler – Github action

name: XCrawler - Build & Tests

on:
  pull_request:
    branches: [ develop ]

jobs:
  build:
    # Test on Workstation
    runs-on: [ Workstation ]
    services:
      mysql:
        image: mysql:8.0.26
        env:
          MYSQL_ROOT_PASSWORD: laravel
          MYSQL_USER: laravel
          MYSQL_PASSWORD: laravel
          MYSQL_DATABASE: laravel
        ports:
          - 3307:3306
        options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=5 -e MYSQL_ROOT_PASSWORD=laravel -e MYSQL_USER=laravel -e MYSQL_PASSWORD=laravel -e MYSQL_DATABASE=laravel --entrypoint sh mysql:8 -c "exec docker-entrypoint.sh mysqld --default-authentication-plugin=mysql_native_password"
      redis:
        image: redis
        ports:
          - 3308:6379
        options: --health-cmd="redis-cli ping" --health-interval=10s --health-timeout=5s --health-retries=3
    steps:
      - uses: actions/checkout@v2
        with:
          fetch-depth: 0

      - name: Setup PHP with coverage driver
        uses: shivammathur/setup-php@v2
        with:
          php-version: '8.0.9'
          coverage: pcov
          tools: php-cs-fixer, phpunit

      - name: Verify MySQL connection from host
        run: |
          mysql --host 127.0.0.1 --port 3307 -ularavel -plaravel -e "SHOW DATABASES"

      - name: Setup application
        if: success()
        run: |
          php -r "file_exists('.env') || copy('.env.example', '.env');"
          composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist
          php artisan key:generate
          chmod -R 777 storage bootstrap/cache
          php artisan config:clear
          php artisan migrate:fresh

      - name: Lint
        run: composer lint

      - name: Execute tests (Unit and Feature tests) via PHPUnit
        if: success()
        run: |
          composer test-coverage

      - name: Coverage
        uses: actions/upload-artifact@v2
        if: success()
        with:
          name: coverage
          path: coverage.xml

      - name: upload coverage to codecov.io
        uses: codecov/codecov-action@v1
        if: success()
        with:
          file: ./coverage.xml

Okay. ! Đây là build.yml mình sử dụng trong XCrawler. Giờ explain qua 1 chút

name: XCrawler - Build & Tests

Chỉ là define tên của em nó thôi

on:
  pull_request:
    branches: [ develop ]

https://docs.github.com/en/actions/learn-github-actions/workflow-syntax-for-github-actions#on

Trên build của mình sẽ catch trên Pull Request của branch develop

Em build của mình có con job tên là build

    runs-on: [ Workstation ]

và chạy trên Server Workstation của mình. Server này là 1 self-hosted Github runner

    services:
      mysql:
        image: mysql:8.0.26
        env:
          MYSQL_ROOT_PASSWORD: laravel
          MYSQL_USER: laravel
          MYSQL_PASSWORD: laravel
          MYSQL_DATABASE: laravel
        ports:
          - 3307:3306
        options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=5 -e MYSQL_ROOT_PASSWORD=laravel -e MYSQL_USER=laravel -e MYSQL_PASSWORD=laravel -e MYSQL_DATABASE=laravel --entrypoint sh mysql:8 -c "exec docker-entrypoint.sh mysqld --default-authentication-plugin=mysql_native_password"
      redis:
        image: redis
        ports:
          - 3308:6379
        options: --health-cmd="redis-cli ping" --health-interval=10s --health-timeout=5s --health-retries=3

Install các dockers cần thiết. Thật sự mà nói Docker là thứ mình chưa rành nên không dám nói nhiều

https://docs.github.com/en/actions/learn-github-actions/workflow-syntax-for-github-actions#jobsjob_idservices

Cuối cùng là các steps cần thiết mình cần xử lý

    steps:
      - uses: actions/checkout@v2
        with:
          fetch-depth: 0

      - name: Setup PHP with coverage driver
        uses: shivammathur/setup-php@v2
        with:
          php-version: '8.0.9'
          coverage: pcov
          tools: php-cs-fixer, phpunit

      - name: Verify MySQL connection from host
        run: |
          mysql --host 127.0.0.1 --port 3307 -ularavel -plaravel -e "SHOW DATABASES"

      - name: Setup application
        if: success()
        run: |
          php -r "file_exists('.env') || copy('.env.example', '.env');"
          composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist
          php artisan key:generate
          chmod -R 777 storage bootstrap/cache
          php artisan config:clear
          php artisan migrate:fresh

      - name: Lint
        run: composer lint

      - name: Execute tests (Unit and Feature tests) via PHPUnit
        if: success()
        run: |
          composer test-coverage

      - name: Coverage
        uses: actions/upload-artifact@v2
        if: success()
        with:
          name: coverage
          path: coverage.xml

      - name: upload coverage to codecov.io
        uses: codecov/codecov-action@v1
        if: success()
        with:
          file: ./coverage.xml

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Up ↑

%d bloggers like this: