EC-CUBE4用プラグインのCI/CD（GitHub Actions）

EC-CUBE4用のプラグインを作ってみた際に、テストとパッケージングを手動でするのが手間だったので、プラグインの CI/CD について調べてみました。

## 目的
- できればシンプルに CI/CD をしたい
- GitHubを使っていたので、そこで完結させたい

## 結果
EC-CUBE本体に GitHub Actions で CI/CD しているソースがありました。
https://github.com/EC-CUBE/ec-cube/blob/4.0/.github/workflows/action.yml

ということで、それを参考に下記のようになりました。プッシュ、プルリクエスト時にテスト、リリース時にパッケージングされます。
"SamplePlugin" 部分は適宜変更
（2020/03/16 14:54追記）"SamplePlugin" を ${{ matrix.plugin-code }} に置き換えました。
（2020/04/08 18:04追記）MySQLが未起動でエラーになったので、起動セットアップを追加しました。

```yaml:.github/workflows/action.yml
name: CI/CD
on:
  push:
    paths:
      - '**'
      - '!.editorconfig'
      - '!.gitignore'
      - '!LICENSE.txt'
      - '!*.md'
  pull_request:
    paths:
      - '**'
      - '!.editorconfig'
      - '!.gitignore'
      - '!LICENSE.txt'
      - '!*.md'
  release:
    types: [ published ]
jobs:
  phpunit:
    name: PHPUnit
    runs-on: ${{ matrix.operating-system }}
    strategy:
      fail-fast: false
      matrix:
        operating-system: [ ubuntu-18.04 ]
        php: [ 7.1, 7.2, 7.3, 7.4 ]
        db: [ mysql, pgsql, sqlite3 ]
        plugin-code: [ SamplePlugin ]
        include:
          - db: mysql
            database_url: mysql://root:root@localhost:3306/eccube_db
            database_server_version: 5
          - db: pgsql
            database_url: postgres://postgres:password@localhost:5432/eccube_db
            database_server_version: 11
          - db: sqlite3
            database_url: sqlite:///var/eccube.db
            database_server_version: 3

    services:
      postgres:
        image: postgres:11
        env:
          POSTGRES_USER: postgres
          POSTGRES_PASSWORD: password
          POSTGRES_DB: postgres
        ports:
          - 5432:5432
        # needed because the postgres container does not provide a healthcheck
        options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5

    steps:
    - name: Checkout EC-CUBE
      uses: actions/checkout@v2
      with:
        repository: EC-CUBE/ec-cube
        ref: '4.0'

    - name: Checkout EC-CUBE plugin
      uses: actions/checkout@v2
      with:
        path: app/Plugin/${{ matrix.plugin-code }}

    - name: Get Composer Cache Directory
      id: composer-cache
      run: |
        echo "::set-output name=dir::$(composer config cache-files-dir)"
    - uses: actions/cache@v1
      with:
        path: ${{ steps.composer-cache.outputs.dir }}
        key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
        restore-keys: |
          ${{ runner.os }}-composer-

    - name: Setup MySQL
      run: |
        sudo service mysql start

    - name: Setup PHP
      uses: nanasess/setup-php@master
      with:
        php-version: ${{ matrix.php }}

    - name: composer install
      run: composer install --dev --no-interaction -o --apcu-autoloader

    - name: Setup EC-CUBE and plugin
      env:
        DATABASE_URL: ${{ matrix.database_url }}
        DATABASE_SERVER_VERSION: ${{ matrix.database_server_version }}
        ECCUBE_LOCALE: ja
      run: |
        bin/console doctrine:database:create
        bin/console doctrine:schema:create
        bin/console eccube:fixtures:load
        bin/console eccube:plugin:install --code ${{ matrix.plugin-code }}
        bin/console eccube:plugin:enable --code ${{ matrix.plugin-code }}

    - name: PHPUnit for plugin
      env:
        APP_ENV: 'test'
        DATABASE_URL: ${{ matrix.database_url }}
        DATABASE_SERVER_VERSION: ${{ matrix.database_server_version }}
      run: |
        bin/phpunit app/Plugin/${{ matrix.plugin-code }}

  deploy:
    name: Deploy
    runs-on: ubuntu-18.04
    needs: [ phpunit ]
    steps:
    - name: Checkout
      if: github.event_name == 'release' && (github.event.action == 'published' || github.event.action == 'prereleased' )
      uses: actions/checkout@master

    - name: Packaging
      if: github.event_name == 'release' && (github.event.action == 'published' || github.event.action == 'prereleased' )
      working-directory: ../
      env:
        TAG_NAME: ${{ github.event.release.tag_name }}
        REPOSITORY_NAME: ${{ github.event.repository.name }}
      run: |
        rm -rf $GITHUB_WORKSPACE/.editorconfig
        rm -rf $GITHUB_WORKSPACE/.gitignore
        rm -rf $GITHUB_WORKSPACE/phpunit.xml.dist
        rm -rf $GITHUB_WORKSPACE/LICENSE.txt
        rm -rf $GITHUB_WORKSPACE/README.md
        rm -rf $GITHUB_WORKSPACE/Tests
        rm -rf $GITHUB_WORKSPACE/.github
        rm -rf $GITHUB_WORKSPACE/.git

        echo "set permissions..."
        chmod -R o+w $GITHUB_WORKSPACE

        echo "complession files..."
        cd $REPOSITORY_NAME
        tar czfp ../$REPOSITORY_NAME-$TAG_NAME.tar.gz *
        zip -ry ../$REPOSITORY_NAME-$TAG_NAME.zip * 1> /dev/null
        ls -al

    - name: Upload binaries to release of TGZ
      if: github.event_name == 'release' && (github.event.action == 'published' || github.event.action == 'prereleased' )
      uses: svenstaro/upload-release-action@v1-release
      with:
        repo_token: ${{ secrets.GITHUB_TOKEN }}
        file: ${{ runner.workspace }}/${{ github.event.repository.name }}-${{ github.event.release.tag_name }}.tar.gz
        asset_name: ${{ github.event.repository.name }}-${{ github.event.release.tag_name }}.tar.gz
        tag: ${{ github.ref }}
        overwrite: true
    - name: Upload binaries to release of ZIP
      if: github.event_name == 'release' && (github.event.action == 'published' || github.event.action == 'prereleased' )
      uses: svenstaro/upload-release-action@v1-release
      with:
        repo_token: ${{ secrets.GITHUB_TOKEN }}
        file: ${{ runner.workspace }}/${{ github.event.repository.name }}-${{ github.event.release.tag_name }}.zip
        asset_name: ${{ github.event.repository.name }}-${{ github.event.release.tag_name }}.zip
        tag: ${{ github.ref }}
        overwrite: true
```

ポイントは

```yaml
    - name: Checkout EC-CUBE
      uses: actions/checkout@v2
      with:
        repository: EC-CUBE/ec-cube
        ref: '4.0'
```
で EC-CUBE 本体をチェックアウトして

```yaml
    - name: Checkout EC-CUBE plugin
      uses: actions/checkout@v2
      with:
        path: app/Plugin/${{ matrix.plugin-code }}
```
で EC-CUBE のプラグイン格納場所にプラグインをチェックアウト

## 参考
[GitHub Actions で PHP の CI/CD をする](https://qiita.com/nanasess/items/09b0798eb878328b195f)