|
| 1 | +# Name of the GitHub Actions workflow. |
| 2 | +name: Build & Test WordPress Plugin |
| 3 | + |
| 4 | +# Controls when the action will run. |
| 5 | +# It runs on pushes to the 'main' branch and on any pull request. |
| 6 | +on: |
| 7 | + push: |
| 8 | + branches: ["main"] |
| 9 | + pull_request: |
| 10 | + branches: ["main"] |
| 11 | + |
| 12 | +# A workflow run is made up of one or more jobs that can run sequentially or in parallel. |
| 13 | +jobs: |
| 14 | + ################################# |
| 15 | + # 1. LINTING JOB: Check code quality |
| 16 | + ################################# |
| 17 | + lint: |
| 18 | + name: "PHP Linting" |
| 19 | + # The type of runner that the job will run on. |
| 20 | + runs-on: ubuntu-latest |
| 21 | + |
| 22 | + steps: |
| 23 | + # Step 1: Check out the repository code so the workflow can access it. |
| 24 | + - name: Checkout code |
| 25 | + uses: actions/checkout@v4 |
| 26 | + |
| 27 | + # Step 2: Set up PHP. We specify a version to ensure consistency. |
| 28 | + - name: Setup PHP |
| 29 | + uses: shivammathur/setup-php@v2 |
| 30 | + with: |
| 31 | + php-version: "8.1" # Change to your plugin's minimum required PHP version. |
| 32 | + extensions: mbstring, xml, ctype, iconv, intl, json |
| 33 | + tools: composer |
| 34 | + |
| 35 | + # Step 3: Install PHP dependencies using Composer. |
| 36 | + - name: Install Composer dependencies |
| 37 | + run: composer install --prefer-dist --no-progress |
| 38 | + |
| 39 | + # Step 4: Run the PHP linter. |
| 40 | + # This command is defined in your `composer.json` file. |
| 41 | + # Example `scripts` section in composer.json: |
| 42 | + # "scripts": { |
| 43 | + # "lint": "phpcs --standard=WordPress ./" |
| 44 | + # } |
| 45 | + - name: Run PHP Code Sniffer |
| 46 | + run: composer run lint |
| 47 | + |
| 48 | + #################################### |
| 49 | + # 2. TESTING JOB: Run unit/integration tests |
| 50 | + #################################### |
| 51 | + test: |
| 52 | + name: "Unit & Integration Tests" |
| 53 | + # This job depends on the 'lint' job finishing successfully. |
| 54 | + needs: lint |
| 55 | + runs-on: ubuntu-latest |
| 56 | + |
| 57 | + # Use a strategy matrix to test across multiple WordPress and PHP versions. |
| 58 | + strategy: |
| 59 | + matrix: |
| 60 | + wordpress-version: ["latest", "6.7.2", "6.5"] # Test against the latest and a specific older version. |
| 61 | + php-version: ["8.1", "8.2"] # Test against multiple PHP versions. |
| 62 | + |
| 63 | + steps: |
| 64 | + # Step 1: Check out the repository code. |
| 65 | + - name: Checkout code |
| 66 | + uses: actions/checkout@v4 |
| 67 | + |
| 68 | + # Step 2: Set up the WordPress test environment using a dedicated Action. |
| 69 | + # This action downloads WordPress, the unit test library, and sets up a test database. |
| 70 | + - name: Setup WordPress test environment |
| 71 | + uses: wordpress/wordpress-test-action@v1 |
| 72 | + with: |
| 73 | + wordpress-version: ${{ matrix.wordpress-version }} |
| 74 | + php-version: ${{ matrix.php-version }} |
| 75 | + |
| 76 | + # Step 3: Run the actual tests. |
| 77 | + # This command is typically defined in your `composer.json` or `package.json`. |
| 78 | + # It executes the PHPUnit test suite. |
| 79 | + - name: Run tests with PHPUnit |
| 80 | + run: vendor/bin/phpunit |
| 81 | + |
| 82 | + #################################### |
| 83 | + # 3. BUILD JOB: Create a distributable .zip file |
| 84 | + #################################### |
| 85 | + build: |
| 86 | + name: "Create Installable ZIP" |
| 87 | + # This job depends on the 'test' job finishing successfully. |
| 88 | + needs: test |
| 89 | + runs-on: ubuntu-latest |
| 90 | + |
| 91 | + steps: |
| 92 | + # Step 1: Check out the repository code. |
| 93 | + - name: Checkout code |
| 94 | + uses: actions/checkout@v4 |
| 95 | + |
| 96 | + # Step 2: Install front-end dependencies and build assets (if you have them). |
| 97 | + # Skip this if you don't use npm for CSS/JS builds. |
| 98 | + - name: Install npm dependencies and build assets |
| 99 | + run: | |
| 100 | + npm install |
| 101 | + npm run build |
| 102 | +
|
| 103 | + # Step 3: Use a dedicated WordPress plugin build Action to create the .zip file. |
| 104 | + # This action is smart: it excludes development files like .git, node_modules, etc. |
| 105 | + - name: Create WordPress plugin ZIP |
| 106 | + uses: wordpress-actions/zip@v1.0.1 |
| 107 | + with: |
| 108 | + # The output file name for the zip file. The slug is your plugin's directory name. |
| 109 | + zip-name: ${{ github.event.repository.name }}.zip |
| 110 | + |
| 111 | + # Step 4: Upload the generated ZIP file as a build artifact. |
| 112 | + # This makes the ZIP file available for download from the GitHub Actions run summary page. |
| 113 | + - name: Upload ZIP as a build artifact |
| 114 | + uses: actions/upload-artifact@v4 |
| 115 | + with: |
| 116 | + name: ${{ github.event.repository.name }} |
| 117 | + path: ${{ github.event.repository.name }}.zip |
0 commit comments