|
| 1 | +name: Create Release Archive |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - main |
| 7 | + release: |
| 8 | + types: [published] |
| 9 | + |
| 10 | +jobs: |
| 11 | + build-and-upload-archive: |
| 12 | + runs-on: ubuntu-latest |
| 13 | + steps: |
| 14 | + # Step 1: Checkout code |
| 15 | + - name: Checkout code |
| 16 | + uses: actions/checkout@v4 |
| 17 | + with: |
| 18 | + fetch-depth: 0 |
| 19 | + |
| 20 | + # Step 2: Install Node dependencies |
| 21 | + - name: Install Node dependencies |
| 22 | + run: npm ci |
| 23 | + |
| 24 | + # Step 3: Run build |
| 25 | + - name: Run npm build |
| 26 | + run: npm run build |
| 27 | + |
| 28 | + # Step 4: Install composer dependencies for production |
| 29 | + - name: Composer install |
| 30 | + run: composer install --no-dev --optimize-autoloader |
| 31 | + |
| 32 | + # Step 5: Remove unnecessary files and folders |
| 33 | + - name: Clean repo for release |
| 34 | + run: | |
| 35 | + rm -rf .github src node_modules |
| 36 | + rm -f .gitignore LICENSE README.md readme.txt .cursorrc.json |
| 37 | + rm -f *.lock *.js *.xml *.json |
| 38 | +
|
| 39 | + # Step 6: Generate manifest.json and extract $VERSION |
| 40 | + - name: Generate manifest.json |
| 41 | + id: generate_manifest |
| 42 | + run: | |
| 43 | + MAIN_FILE=$(grep -rl "Plugin Name:" --include="*.php" . | head -n 1) |
| 44 | + echo "Main plugin file: $MAIN_FILE" |
| 45 | +
|
| 46 | + parse_field() { |
| 47 | + grep -i "$1:" "$MAIN_FILE" | head -n1 \ |
| 48 | + | sed -E "s/^\s*\*\s*//" \ |
| 49 | + | sed -E "s|$1:[[:space:]]*||i" \ |
| 50 | + | xargs |
| 51 | + } |
| 52 | +
|
| 53 | + PLUGIN_NAME="$(parse_field 'Plugin Name')" |
| 54 | + PLUGIN_URI="$(parse_field 'Plugin URI')" |
| 55 | + DESCRIPTION="$(parse_field 'Description')" |
| 56 | + AUTHOR="$(parse_field 'Author')" |
| 57 | + AUTHOR_URI="$(parse_field 'Author URI')" |
| 58 | + VERSION="$(parse_field 'Version')" |
| 59 | + REQUIRES_PHP="$(parse_field 'Requires PHP')" |
| 60 | + REQUIRES_WP="$(parse_field 'Requires at least')" |
| 61 | + LICENSE="$(parse_field 'License')" |
| 62 | + LICENSE_URI="$(parse_field 'License URI')" |
| 63 | + TEXT_DOMAIN="$(parse_field 'Text Domain')" |
| 64 | + DOMAIN_PATH="$(parse_field 'Domain Path')" |
| 65 | + TESTED_UP_TO="$(parse_field 'Tested up to')" |
| 66 | +
|
| 67 | + echo "VERSION=$VERSION" >> $GITHUB_ENV |
| 68 | +
|
| 69 | + cat > manifest.json <<EOF |
| 70 | + { |
| 71 | + "plugin_name": "$PLUGIN_NAME", |
| 72 | + "plugin_uri": "$PLUGIN_URI", |
| 73 | + "description": "$DESCRIPTION", |
| 74 | + "author": "$AUTHOR", |
| 75 | + "author_uri": "$AUTHOR_URI", |
| 76 | + "version": "$VERSION", |
| 77 | + "requires_php": "$REQUIRES_PHP", |
| 78 | + "requires_wp": "$REQUIRES_WP", |
| 79 | + "license": "$LICENSE", |
| 80 | + "license_uri": "$LICENSE_URI", |
| 81 | + "text_domain": "$TEXT_DOMAIN", |
| 82 | + "domain_path": "$DOMAIN_PATH", |
| 83 | + "tested_up_to": "$TESTED_UP_TO" |
| 84 | + } |
| 85 | + EOF |
| 86 | +
|
| 87 | + # Step 7: Create custom zip archive |
| 88 | + - name: Create custom archive |
| 89 | + run: | |
| 90 | + REPO_NAME=$(echo "${{ github.repository }}" | cut -d'/' -f2) |
| 91 | + CUSTOM_ARCHIVE_NAME="${REPO_NAME}.zip" |
| 92 | +
|
| 93 | + # Move one level up and zip the repository folder |
| 94 | + cd .. |
| 95 | + zip -r "$CUSTOM_ARCHIVE_NAME" "$REPO_NAME" -x "$REPO_NAME/.git/*" "$REPO_NAME/.github/*" |
| 96 | +
|
| 97 | + # Move the zip back to the original workflow directory |
| 98 | + mv "$CUSTOM_ARCHIVE_NAME" "$GITHUB_WORKSPACE/" |
| 99 | +
|
| 100 | + # Set the zip file name as environment variable for later steps |
| 101 | + echo "CUSTOM_ARCHIVE_NAME=$CUSTOM_ARCHIVE_NAME" >> "$GITHUB_ENV" |
| 102 | +
|
| 103 | + # Step 8: Get last commit message (push events only) |
| 104 | + - name: Get last commit message |
| 105 | + if: github.event_name == 'push' |
| 106 | + id: last_commit |
| 107 | + run: | |
| 108 | + LAST_COMMIT_MESSAGE="$(git log -1 --pretty=%B)" |
| 109 | + { |
| 110 | + echo "LAST_COMMIT_MESSAGE<<EOF" |
| 111 | + echo "$LAST_COMMIT_MESSAGE" |
| 112 | + echo "EOF" |
| 113 | + } >> $GITHUB_ENV |
| 114 | +
|
| 115 | + # Step 9: Ensure tag exists on remote (push events only) |
| 116 | + - name: Ensure tag exists |
| 117 | + if: github.event_name == 'push' |
| 118 | + env: |
| 119 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 120 | + run: | |
| 121 | + TAG=v${{ env.VERSION }} |
| 122 | + git config user.name "github-actions[bot]" |
| 123 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 124 | + git tag "$TAG" |
| 125 | + git push https://x-access-token:${GITHUB_TOKEN}@github.com/${GITHUB_REPOSITORY}.git "$TAG" |
| 126 | +
|
| 127 | + # Step 10: Create GitHub release (push events) |
| 128 | + - name: Create GitHub release (push) |
| 129 | + if: github.event_name == 'push' |
| 130 | + id: create_release_push |
| 131 | + uses: softprops/action-gh-release@v2 |
| 132 | + with: |
| 133 | + tag_name: ${{ env.VERSION }} |
| 134 | + name: Version ${{ env.VERSION }} |
| 135 | + body: ${{ env.LAST_COMMIT_MESSAGE }} |
| 136 | + files: | |
| 137 | + ${{ env.CUSTOM_ARCHIVE_NAME }} |
| 138 | + manifest.json |
| 139 | + overwrite_files: true |
| 140 | + env: |
| 141 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 142 | + |
| 143 | + # Step 11: Create GitHub release (release events) |
| 144 | + - name: Create GitHub release (release) |
| 145 | + if: github.event_name == 'release' |
| 146 | + id: create_release_event |
| 147 | + uses: softprops/action-gh-release@v2 |
| 148 | + with: |
| 149 | + tag_name: ${{ github.event.release.tag_name }} |
| 150 | + name: Release ${{ github.event.release.tag_name }} |
| 151 | + body: ${{ github.event.release.body }} |
| 152 | + files: | |
| 153 | + ${{ env.CUSTOM_ARCHIVE_NAME }} |
| 154 | + manifest.json |
| 155 | + overwrite_files: true |
| 156 | + env: |
| 157 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
0 commit comments