|
| 1 | +# This workflow runs continuous integration checks on the Simple WP Optimizer plugin. |
| 2 | +# It performs code linting for both PHP and JavaScript files and builds the plugin package. |
| 3 | +# The workflow is triggered on push to main branch and on pull requests to ensure code quality. |
| 4 | +# It creates and stores a plugin zip file as an artifact that can be used for testing. |
| 5 | + |
| 6 | +name: Continuous Integration |
| 7 | + |
| 8 | +on: |
| 9 | + push: |
| 10 | + branches: [ main ] |
| 11 | + pull_request: |
| 12 | + branches: [ main ] |
| 13 | + |
| 14 | +# Define explicit permissions to follow principle of least privilege |
| 15 | +permissions: |
| 16 | + contents: read # Allows read access to repository contents |
| 17 | + actions: read # Allows read access to GitHub Actions |
| 18 | + checks: write # Allows creating/updating checks on the workflow run |
| 19 | + pull-requests: write # Allows commenting on PRs for feedback |
| 20 | + packages: read # Allows reading packages |
| 21 | + |
| 22 | +jobs: |
| 23 | + lint: |
| 24 | + name: Lint PHP and JavaScript |
| 25 | + runs-on: ubuntu-latest |
| 26 | + steps: |
| 27 | + - name: Checkout code |
| 28 | + uses: actions/checkout@v4 |
| 29 | + |
| 30 | + - name: Setup PHP |
| 31 | + uses: shivammathur/setup-php@v2 |
| 32 | + with: |
| 33 | + php-version: '7.4' |
| 34 | + tools: composer:v2, phpcs |
| 35 | + |
| 36 | + - name: Check for package-lock.json |
| 37 | + id: check-lockfile |
| 38 | + run: | |
| 39 | + if [ -f "package-lock.json" ]; then |
| 40 | + echo "lockfile_exists=true" >> $GITHUB_OUTPUT |
| 41 | + else |
| 42 | + echo "lockfile_exists=false" >> $GITHUB_OUTPUT |
| 43 | + echo "package-lock.json not found. Caching will be skipped." |
| 44 | + fi |
| 45 | +
|
| 46 | + - name: Setup Node.js |
| 47 | + uses: actions/setup-node@v4 |
| 48 | + with: |
| 49 | + node-version: '16' |
| 50 | + # Use conditional caching based on package-lock.json existence |
| 51 | + cache: ${{ steps.check-lockfile.outputs.lockfile_exists == 'true' && 'npm' || '' }} |
| 52 | + cache-dependency-path: | |
| 53 | + **/package-lock.json |
| 54 | + !**/node_modules/ |
| 55 | + |
| 56 | + - name: Install PHP dependencies |
| 57 | + run: | |
| 58 | + if [ -f composer.json ]; then |
| 59 | + # Configure platform to ensure compatibility |
| 60 | + composer config platform.php 7.4 |
| 61 | + composer install --prefer-dist --no-progress || { |
| 62 | + echo "Standard install failed, trying with --ignore-platform-reqs" |
| 63 | + composer install --prefer-dist --no-progress --ignore-platform-reqs |
| 64 | + } |
| 65 | + else |
| 66 | + echo "No composer.json found. Skipping composer install." |
| 67 | + fi |
| 68 | + |
| 69 | + - name: Install JS dependencies |
| 70 | + run: | |
| 71 | + if [ -f package.json ]; then |
| 72 | + npm ci || npm install |
| 73 | + else |
| 74 | + echo "No package.json found. Skipping npm install." |
| 75 | + fi |
| 76 | + |
| 77 | + - name: Lint PHP |
| 78 | + run: | |
| 79 | + if [ -f composer.json ] && ([ -f .phpcs.xml ] || [ -f phpcs.xml.dist ]); then |
| 80 | + if grep -q "\"lint:php\"" composer.json; then |
| 81 | + composer run lint:php |
| 82 | + else |
| 83 | + echo "No lint:php script found in composer.json. Skipping." |
| 84 | + fi |
| 85 | + else |
| 86 | + echo "No PHP linting configuration found. Skipping." |
| 87 | + fi |
| 88 | + |
| 89 | + - name: Lint JavaScript |
| 90 | + run: | |
| 91 | + if [ -f package.json ] && ([ -f .eslintrc.js ] || [ -f .eslintrc.json ]); then |
| 92 | + npm run lint:js || echo "Lint script not found in package.json. Skipping." |
| 93 | + else |
| 94 | + echo "No JS linting configuration found. Skipping." |
| 95 | + fi |
| 96 | + |
| 97 | + build: |
| 98 | + name: Build Plugin |
| 99 | + runs-on: ubuntu-latest |
| 100 | + needs: lint |
| 101 | + steps: |
| 102 | + - name: Checkout code |
| 103 | + uses: actions/checkout@v4 |
| 104 | + |
| 105 | + - name: Check for package-lock.json in build |
| 106 | + id: check-lockfile-build |
| 107 | + run: | |
| 108 | + if [ -f "package-lock.json" ]; then |
| 109 | + echo "lockfile_exists=true" >> $GITHUB_OUTPUT |
| 110 | + else |
| 111 | + echo "lockfile_exists=false" >> $GITHUB_OUTPUT |
| 112 | + echo "package-lock.json not found. Caching will be skipped." |
| 113 | + fi |
| 114 | + |
| 115 | + - name: Setup Node.js |
| 116 | + uses: actions/setup-node@v4 |
| 117 | + with: |
| 118 | + node-version: '16' |
| 119 | + # Use conditional caching based on package-lock.json existence |
| 120 | + cache: ${{ steps.check-lockfile-build.outputs.lockfile_exists == 'true' && 'npm' || '' }} |
| 121 | + cache-dependency-path: | |
| 122 | + **/package-lock.json |
| 123 | + !**/node_modules/ |
| 124 | + |
| 125 | + - name: Setup PHP |
| 126 | + uses: shivammathur/setup-php@v2 |
| 127 | + with: |
| 128 | + php-version: '7.4' |
| 129 | + tools: composer:v2 |
| 130 | + |
| 131 | + - name: Install dependencies |
| 132 | + run: | |
| 133 | + if [ -f composer.json ]; then |
| 134 | + # Configure platform to ensure compatibility |
| 135 | + composer config platform.php 7.4 |
| 136 | + composer install --no-dev --prefer-dist --no-progress || { |
| 137 | + echo "Standard install failed, trying with --ignore-platform-reqs" |
| 138 | + composer install --no-dev --prefer-dist --no-progress --ignore-platform-reqs |
| 139 | + } |
| 140 | + else |
| 141 | + echo "No composer.json found. Skipping composer install." |
| 142 | + fi |
| 143 | + |
| 144 | + if [ -f package.json ]; then |
| 145 | + npm ci || npm install |
| 146 | + else |
| 147 | + echo "No package.json found. Skipping npm install." |
| 148 | + fi |
| 149 | + |
| 150 | + - name: Build frontend assets |
| 151 | + run: | |
| 152 | + if [ -f package.json ]; then |
| 153 | + if grep -q "\"build\"" package.json; then |
| 154 | + npm run build |
| 155 | + else |
| 156 | + echo "No build script found in package.json. Skipping." |
| 157 | + fi |
| 158 | + else |
| 159 | + echo "No package.json found. Skipping build." |
| 160 | + fi |
| 161 | + |
| 162 | + - name: Create plugin package |
| 163 | + run: | |
| 164 | + mkdir -p build/simple-wp-optimizer |
| 165 | + |
| 166 | + # Copy main plugin file |
| 167 | + cp simple-wp-optimizer.php build/simple-wp-optimizer/ |
| 168 | + |
| 169 | + # Copy directories if they exist |
| 170 | + [ -d assets ] && cp -r assets build/simple-wp-optimizer/ || echo "No assets directory found" |
| 171 | + [ -d includes ] && cp -r includes build/simple-wp-optimizer/ || echo "No includes directory found" |
| 172 | + [ -d languages ] && cp -r languages build/simple-wp-optimizer/ || echo "No languages directory found" |
| 173 | + [ -d templates ] && cp -r templates build/simple-wp-optimizer/ || echo "No templates directory found" |
| 174 | + |
| 175 | + # Copy additional files if they exist |
| 176 | + [ -f readme.txt ] && cp readme.txt build/simple-wp-optimizer/ || echo "No readme.txt found" |
| 177 | + [ -f LICENSE ] && cp LICENSE build/simple-wp-optimizer/ || echo "No LICENSE file found" |
| 178 | + |
| 179 | + # Create zip file |
| 180 | + cd build |
| 181 | + zip -r simple-wp-optimizer.zip simple-wp-optimizer |
| 182 | + |
| 183 | + - name: Upload build artifact |
| 184 | + uses: actions/upload-artifact@v4 |
| 185 | + with: |
| 186 | + name: simple-wp-optimizer |
| 187 | + path: build/simple-wp-optimizer.zip |
0 commit comments