ci: replace 10up deploy with local ZIP creation for plugin artifact #17
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Name of the GitHub Actions workflow. | |
| name: Build & Test WordPress Plugin | |
| # Controls when the action will run. | |
| # It runs on pushes to the 'main' branch and on any pull request. | |
| on: | |
| push: | |
| branches: ["main"] | |
| pull_request: | |
| branches: ["main"] | |
| # A workflow run is made up of one or more jobs that can run sequentially or in parallel. | |
| jobs: | |
| ################################# | |
| # 1. LINTING JOB: Check code quality | |
| ################################# | |
| lint: | |
| name: "PHP Linting" | |
| # The type of runner that the job will run on. | |
| runs-on: ubuntu-latest | |
| steps: | |
| # Step 1: Check out the repository code so the workflow can access it. | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| # Step 2: Set up PHP. We specify a version to ensure consistency. | |
| - name: Setup PHP | |
| uses: shivammathur/setup-php@v2 | |
| with: | |
| php-version: "8.3" # Change to your plugin's minimum required PHP version. | |
| extensions: mbstring, xml, ctype, iconv, intl, json | |
| tools: composer | |
| # Step 3: Caching dependencies | |
| - name: Cache Composer dependencies | |
| uses: actions/cache@v4 | |
| with: | |
| path: vendor | |
| key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }} | |
| # Step 4: Install PHP dependencies using Composer. | |
| - name: Install Composer dependencies | |
| run: composer install --prefer-dist --no-progress | |
| # Step 5: Add depedency vulnerability scanning | |
| - name: Security audit | |
| run: composer audit | |
| # Step 6: Run the PHP linter. | |
| # This command is defined in `composer.json` file. | |
| # Example `scripts` section in composer.json: | |
| # "scripts": { | |
| # "lint": "phpcs --standard=WordPress ./" | |
| # } | |
| - name: Run PHP Code Sniffer | |
| run: composer run lint | |
| continue-on-error: false | |
| #################################### | |
| # 2. TESTING JOB: Run unit/integration tests | |
| #################################### | |
| test: | |
| name: "Unit & Integration Tests" | |
| # This job depends on the 'lint' job finishing successfully. | |
| needs: lint | |
| runs-on: ubuntu-latest | |
| timeout-minutes: 20 | |
| # Database Testing | |
| services: | |
| mysql: | |
| image: mysql:8.0 | |
| env: | |
| MYSQL_ALLOW_EMPTY_PASSWORD: yes | |
| MYSQL_DATABASE: wordpress_test | |
| ports: | |
| - 3306:3306 | |
| options: >- | |
| --health-cmd="mysqladmin ping --silent" | |
| --health-interval=10s | |
| --health-timeout=5s | |
| --health-retries=3 | |
| # Use a strategy matrix to test across multiple WordPress and PHP versions. | |
| strategy: | |
| matrix: | |
| wordpress-version: ["latest"] # Test against the latest and a specific older version. | |
| php-version: ["8.3", "8.4"] # Test against multiple PHP versions. | |
| env: | |
| WP_TESTS_DIR: /tmp/wordpress-tests/ | |
| WP_CORE_DIR: /tmp/wordpress/ | |
| steps: | |
| # Step 1: Check out the repository code. | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| # Install Subversion | |
| - name: Install SVN | |
| run: sudo apt-get update && sudo apt-get install -y subversion | |
| # Step 2: Set up the WordPress test environment using a dedicated Action. | |
| # This action downloads WordPress, the unit test library, and sets up a test database. | |
| # - name: Setup WordPress test environment | |
| # uses: wordpress/wordpress-test-action@v1 | |
| - name: Set up WordPress and Test Library | |
| uses: sjinks/[email protected] | |
| with: | |
| # wordpress-version: ${{ matrix.wordpress-version }} | |
| php-version: ${{ matrix.php-version }} | |
| - name: Install PHP dependencies | |
| run: composer install --prefer-dist --no-interaction --dev | |
| # Step 3: Install PHPUnit and run the actual tests. | |
| # This command is typically defined in your `composer.json` or `package.json`. | |
| # It executes the PHPUnit test suite. | |
| - name: Run tests with PHPUnit with coverage report | |
| run: vendor/bin/phpunit | |
| - name: Upload coverage to Codecov | |
| uses: codecov/codecov-action@v4 | |
| if: always() | |
| #################################### | |
| # 3. BUILD JOB: Create a distributable .zip file | |
| #################################### | |
| build: | |
| name: "Create Installable ZIP" | |
| # This job depends on the 'test' job finishing successfully. | |
| needs: test | |
| runs-on: ubuntu-latest | |
| if: github.event_name == 'push' && github.ref == 'refs/heads/main' | |
| steps: | |
| # Step 1: Check out the repository code. | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| # Step 2: Install front-end dependencies and build assets (if you have them). | |
| # Skip this if you don't use npm for CSS/JS builds. | |
| # Step 2a: Cache npm | |
| - name: Cache npm | |
| uses: actions/cache@v4 | |
| with: | |
| path: ~/.npm | |
| key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }} | |
| # Step 2b: Install npm dependencies and build assets | |
| - name: Install npm dependencies and build assets | |
| run: | | |
| npm install | |
| npm run build | |
| # Step 3: Use a dedicated WordPress plugin build Action to create the .zip file. | |
| # This action is smart: it excludes development files like .git, node_modules, etc. | |
| - name: Create WordPress plugin ZIP | |
| # uses: 10up/actions-wordpress@stable | |
| # with: | |
| # # The output file name for the zip file. The slug is your plugin's directory name. | |
| # zip-name: ${{ github.event.repository.name }}.zip | |
| run: | | |
| composer install --no-dev --optimize-autoloader | |
| zip -r latest-post-widget.zip . -x '*.git*' 'node_modules/*' 'tests/*' 'phpunit.xml' '*.md' | |
| # Step 4: Upload the generated ZIP file as a build artifact. | |
| # This makes the ZIP file available for download from the GitHub Actions run summary page. | |
| - name: Upload ZIP as a build artifact | |
| uses: actions/upload-artifact@v4 | |
| with: | |
| name: ${{ github.event.repository.name }} | |
| path: ${{ github.event.repository.name }}.zip |