update Workflows

This commit is contained in:
Mohamed Youssef
2026-08-02 09:35:04 +03:00
parent 28d4cb1daa
commit 0dca92fefa
+114 -104
View File
@@ -1,138 +1,148 @@
name: WP Plugin Version & Release name: Build WordPress Plugin
on: on:
push: push:
branches: tags:
- main - 'v*' # Trigger on version tags like v1.0.0, v2.1.3
# workflow_dispatch: # (Optional) Uncomment to allow manual trigger
jobs: jobs:
release: build:
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- name: Checkout - name: Checkout code
uses: actions/checkout@v4 uses: actions/checkout@v4
# ------------------------------------------------
# 1. SETUP PHP (for Composer, if used)
# ------------------------------------------------
- name: Setup PHP
uses: shivammathur/setup-php@v2
with: with:
fetch-depth: 0 php-version: '7.4'
tools: composer
- name: Install rsync # ------------------------------------------------
run: apt-get update && apt-get install -y rsync # 2. SETUP NODE (for NPM/Yarn, if used)
# ------------------------------------------------
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'npm' # or 'yarn'
- name: Locate main plugin file # ------------------------------------------------
id: locate # 3. INSTALL & BUILD ASSETS (optional)
# ------------------------------------------------
- name: Install Composer dependencies (production only)
run: | run: |
FILE=$(grep -rlE "^[[:space:]]*(\*|/\*\*)?[[:space:]]*Plugin Name:" \ if [ -f "composer.json" ]; then
--include="*.php" . \ composer install --no-dev --prefer-dist --optimize-autoloader
| grep -v -e '/vendor/' -e '/node_modules/' -e '/build/' \
| head -n1)
if [ -z "$FILE" ]; then
echo "::error::Could not find a PHP file with a 'Plugin Name:' header."
exit 1
fi fi
echo "Found main plugin file: $FILE" - name: Install NPM dependencies & build assets
echo "file=${FILE}" >> "$GITHUB_OUTPUT" run: |
if [ -f "package.json" ]; then
npm ci
fi
if [ -f "package.json" ] && grep -q '"build"' package.json; then
npm run build
fi
- name: Extract plugin name & version from header # ------------------------------------------------
# 4. EXTRACT PLUGIN VERSION FROM MAIN FILE
# ------------------------------------------------
- name: Get plugin version and slug
id: meta id: meta
run: | run: |
FILE="${{ steps.locate.outputs.file }}" # Find the main plugin file (the one with the WordPress header)
HEADER=$(sed -n '1,40p' "$FILE") MAIN_FILE=$(grep -l "Plugin Name:" *.php | head -n 1)
if [ -z "$MAIN_FILE" ]; then
PLUGIN_NAME=$(echo "$HEADER" | grep -im1 'Plugin Name:' \ echo "❌ No main plugin file (with 'Plugin Name:') found!" >&2
| sed -E 's/^[[:space:]]*(\*|\/\*\*)?[[:space:]]*Plugin Name:[[:space:]]*//' \
| sed -E 's/[[:space:]]*\*\/?[[:space:]]*$//')
VERSION=$(echo "$HEADER" | grep -im1 'Version:' \
| sed -E 's/^[[:space:]]*(\*|\/\*\*)?[[:space:]]*Version:[[:space:]]*//' \
| sed -E 's/[[:space:]]*\*\/?[[:space:]]*$//')
if [ -z "$PLUGIN_NAME" ] || [ -z "$VERSION" ]; then
echo "::error::Could not parse Plugin Name or Version from $FILE header."
exit 1 exit 1
fi fi
SLUG=$(echo "$PLUGIN_NAME" | tr '[:upper:]' '[:lower:]' \ # Extract version
| sed -E 's/[^a-z0-9]+/-/g; s/^-+//; s/-+$//') VERSION=$(grep "^Version:" "$MAIN_FILE" | awk -F' ' '{print $NF}' | tr -d '\r')
if [ -z "$VERSION" ]; then
echo "Plugin: $PLUGIN_NAME" echo "❌ Version header not found in $MAIN_FILE" >&2
echo "Slug: $SLUG" exit 1
echo "Version: $VERSION"
echo "name=${PLUGIN_NAME}" >> "$GITHUB_OUTPUT"
echo "slug=${SLUG}" >> "$GITHUB_OUTPUT"
echo "version=${VERSION}" >> "$GITHUB_OUTPUT"
- name: Check if this version already has a release
id: check
run: |
TAG="v${{ steps.meta.outputs.version }}"
git fetch --tags --quiet
if git rev-parse "$TAG" >/dev/null 2>&1; then
echo "Tag $TAG already exists — skipping release."
echo "should_release=false" >> "$GITHUB_OUTPUT"
else
echo "Tag $TAG is new — will release."
echo "should_release=true" >> "$GITHUB_OUTPUT"
fi
echo "tag=${TAG}" >> "$GITHUB_OUTPUT"
- name: Update Stable tag in readme.txt
if: steps.check.outputs.should_release == 'true' && hashFiles('readme.txt') != ''
run: |
sed -i -E "s/^(Stable tag:[[:space:]]*).*/\1${{ steps.meta.outputs.version }}/" readme.txt
if ! git diff --quiet -- readme.txt; then
git config user.name "gitea-actions"
git config user.email "actions@noreply.gitea"
git add readme.txt
git commit -m "chore: sync readme.txt stable tag to ${{ steps.meta.outputs.version }} [skip ci]"
git push origin HEAD:refs/heads/main
fi fi
- name: Tag release # Use repository name as slug, or override with a custom variable
if: steps.check.outputs.should_release == 'true' SLUG="${GITHUB_REPOSITORY##*/}"
run: |
git config user.name "gitea-actions"
git config user.email "actions@noreply.gitea"
git tag "${{ steps.check.outputs.tag }}"
git push origin "${{ steps.check.outputs.tag }}"
- name: Build plugin zip echo "PLUGIN_SLUG=$SLUG" >> $GITHUB_ENV
if: steps.check.outputs.should_release == 'true' echo "PLUGIN_VERSION=$VERSION" >> $GITHUB_ENV
echo "MAIN_FILE=$MAIN_FILE" >> $GITHUB_ENV
echo "FILENAME=${SLUG}-${VERSION}.zip" >> $GITHUB_ENV
echo "✅ Plugin: $SLUG, Version: $VERSION"
# ------------------------------------------------
# 5. PREPARE PACKAGE (Exclude dev files)
# ------------------------------------------------
- name: Prepare package directory
run: | run: |
SLUG="${{ steps.meta.outputs.slug }}" mkdir -p /tmp/build/${{ env.PLUGIN_SLUG }}
mkdir -p build/"${SLUG}"
rsync -a \ # Rsync all files, excluding common dev/dependency folders
rsync -av \
--exclude='.git' \ --exclude='.git' \
--exclude='.gitea' \
--exclude='.github' \ --exclude='.github' \
--exclude='.distignore' \
--exclude='.gitignore' \
--exclude='node_modules' \ --exclude='node_modules' \
--exclude='build' \ --exclude='vendor' \ # Composer dev dependencies (we already installed --no-dev, but safe to exclude if standalone)
--exclude='*.zip' \ --exclude='tests' \
./ build/"${SLUG}"/ --exclude='.env*' \
cd build --exclude='*.log' \
zip -r "../${SLUG}-${{ steps.meta.outputs.version }}.zip" "${SLUG}" --exclude='*.map' \
--exclude='*.lock' \ # package-lock.json / composer.lock (optional, sometimes kept)
--exclude='package.json' \ # Remove if you want to keep it in production
--exclude='composer.json' \ # Remove if you want to keep it
--exclude='webpack.config.js' \
--exclude='gulpfile.js' \
--exclude='Gruntfile.js' \
--exclude='phpunit.xml' \
--exclude='.wordpress-org' \
./ /tmp/build/${{ env.PLUGIN_SLUG }}/ 2>/dev/null || true
# NOTE: If your plugin needs composer.json/package.json in production,
# remove those specific --exclude lines above.
# ------------------------------------------------
# 6. CREATE ZIP ARCHIVE
# ------------------------------------------------
- name: Create plugin zip
run: |
cd /tmp/build
zip -r "$GITHUB_WORKSPACE/${{ env.FILENAME }}" ${{ env.PLUGIN_SLUG }}/
cd $GITHUB_WORKSPACE
echo "✅ Created: $FILENAME"
# ------------------------------------------------
# 7. UPLOAD TO GITHUB RELEASE
# ------------------------------------------------
- name: Install tea CLI - name: Install tea CLI
run: | run: |
TEA_VERSION="0.14.2" curl -L https://dl.gitea.io/tea/0.11.0/tea-0.11.0-linux-amd64 -o tea
curl -fL -o /usr/local/bin/tea \ chmod +x tea
"https://gitea.com/gitea/tea/releases/download/v${TEA_VERSION}/tea-${TEA_VERSION}-linux-amd64" sudo mv tea /usr/local/bin/
chmod +x /usr/local/bin/tea
tea --version
- name: Create Gitea Release (tea CLI) - name: Configure tea
if: steps.check.outputs.should_release == 'true'
run: | run: |
ZIP="${{ steps.meta.outputs.slug }}-${{ steps.meta.outputs.version }}.zip"
tea login add \ tea login add \
--name ci \ --name gitea \
--url "${{ gitea.server_url }}" \ --url "${{ vars.SERVER_URL }}" \
--token "${{ secrets.RELEASE_TOKEN }}" --token "${{ secrets.OC3_EXTENSION_RELEASE_TOKEN }}"
tea releases create \ - name: Create Gitea Release
--repo "${{ gitea.repository }}" \ run: |
--tag "${{ steps.check.outputs.tag }}" \ TAG="v${{ env.PLUGIN_VERSION }}"
--title "${{ steps.meta.outputs.name }} ${{ steps.meta.outputs.version }}" \ REPO="${{ vars.GITEA_REPO }}"
--asset "${ZIP}" tea release create "$TAG" \
--title "${{ env.PLUGIN_SLUG }} v${{ env.PLUGIN_VERSION }}" \
--asset "${{ env.FILENAME }}" \
--repo "$REPO"