diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 3104eca..9e46201 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,306 +1,76 @@ -variables: - # Configure mysql service (https://hub.docker.com/_/mysql/) - MYSQL_DATABASE: wordpress_tests - MYSQL_ROOT_PASSWORD: mysql - stages: - - verify - - test - - release - -PHPCS: - stage: verify - image: wpunit/php:wpcs - script: - - phpcs - -Coverage: - stage: test - image: wpunit/php:7.3-extended - services: - - mysql:5.6 - script: - # Set up WordPress tests - - bash bin/install-wp-tests.sh wordpress_tests root mysql mysql latest true - - XDEBUG_MODE=coverage phpunit --coverage-text --colors=never --configuration phpunit.xml.dist - coverage: /^\s*Lines:\s*\d+.\d+\%/ - -WP-Latest:PHP8.0: - stage: test - image: wpunit/php:8.0 - services: - - mysql:5.6 - script: - # Set up WordPress tests - - bash bin/install-wp-tests.sh wordpress_tests root mysql mysql latest true - - phpunit --configuration phpunit.xml.dist - allow_failure: true - -WP-Latest:PHP7.4: - stage: test - image: wpunit/php:7.4 - services: - - mysql:5.6 - script: - # Set up WordPress tests - - bash bin/install-wp-tests.sh wordpress_tests root mysql mysql latest true - - phpunit --configuration phpunit.xml.dist - -WP-Latest:PHP7.3: - stage: test - image: wpunit/php:7.3 - services: - - mysql:5.6 - script: - # Set up WordPress tests - - bash bin/install-wp-tests.sh wordpress_tests root mysql mysql latest true - - phpunit --configuration phpunit.xml.dist - -WP-Latest:PHP5.6: - stage: test - image: wpunit/php:5.6 - services: - - mysql:5.6 - script: - # Set up WordPress tests - - bash bin/install-wp-tests.sh wordpress_tests root mysql mysql latest true - - phpunit --configuration phpunit.xml.dist - -WP-Nightly:PHP8.0: - stage: test - image: wpunit/php:8.0 - services: - - mysql:5.6 - script: - # Set up WordPress tests - - bash bin/install-wp-tests.sh wordpress_tests root mysql mysql nightly true - - phpunit --configuration phpunit.xml.dist - allow_failure: true - -WP-Nightly:PHP7.4: - stage: test - image: wpunit/php:7.4 - services: - - mysql:5.6 - script: - # Set up WordPress tests - - bash bin/install-wp-tests.sh wordpress_tests root mysql mysql nightly true - - phpunit --configuration phpunit.xml.dist - allow_failure: true - -WP-Nightly:PHP7.3: - stage: test - image: wpunit/php:7.3 - services: - - mysql:5.6 - script: - # Set up WordPress tests - - bash bin/install-wp-tests.sh wordpress_tests root mysql mysql nightly true - - phpunit --configuration phpunit.xml.dist - allow_failure: true - -WP-Nightly:PHP5.6: - stage: test - image: wpunit/php:5.6 - services: - - mysql:5.6 - script: - # Set up WordPress tests - - bash bin/install-wp-tests.sh wordpress_tests root mysql mysql nightly true - - phpunit --configuration phpunit.xml.dist - allow_failure: true - -WP5.4:PHP7.4: - stage: test - image: wpunit/php:7.4 - services: - - mysql:5.6 - script: - # Set up WordPress tests - - bash bin/install-wp-tests.sh wordpress_tests root mysql mysql 5.4 true - - phpunit --configuration phpunit.xml.dist - -WP5.3:PHP7.4: - stage: test - image: wpunit/php:7.4 - variables: - PHPUNIT_VERSION: 5 - services: - - mysql:5.6 - script: - # Set up WordPress tests - - bash bin/install-wp-tests.sh wordpress_tests root mysql mysql 5.3 true - - phpunit --configuration phpunit.xml.dist - -WP5.2:PHP7.4: - stage: test - image: wpunit/php:7.4 - variables: - PHPUNIT_VERSION: 5 - services: - - mysql:5.6 - script: - # Set up WordPress tests - - bash bin/install-wp-tests.sh wordpress_tests root mysql mysql 5.2 true - - phpunit --configuration phpunit.xml.dist + - verify + - test + - deploy -WP5.1:PHP7.2: - stage: test - image: wpunit/php:7.2 - variables: - PHPUNIT_VERSION: 5 - services: - - mysql:5.6 - script: - # Set up WordPress tests - - bash bin/install-wp-tests.sh wordpress_tests root mysql mysql 5.1 true - - phpunit --configuration phpunit.xml.dist - -WP5.0:PHP7.2: - stage: test - image: wpunit/php:7.2 - variables: - PHPUNIT_VERSION: 5 - services: - - mysql:5.6 - script: - # Set up WordPress tests - - bash bin/install-wp-tests.sh wordpress_tests root mysql mysql 5.0 true - - phpunit --configuration phpunit.xml.dist - -WP4.9:PHP7.2: - stage: test - image: wpunit/php:7.2 - variables: - PHPUNIT_VERSION: 5 - services: - - mysql:5.6 - script: - # Set up WordPress tests - - bash bin/install-wp-tests.sh wordpress_tests root mysql mysql 4.9 true - - phpunit --configuration phpunit.xml.dist - -WP4.8:PHP7.2: - stage: test - image: wpunit/php:7.2 - variables: - PHPUNIT_VERSION: 5 - services: - - mysql:5.6 - script: - # Set up WordPress tests - - bash bin/install-wp-tests.sh wordpress_tests root mysql mysql 4.8 true - - phpunit --configuration phpunit.xml.dist - -WP4.7:PHP7.2: - stage: test - image: wpunit/php:7.2 - variables: - PHPUNIT_VERSION: 5 - services: - - mysql:5.6 - script: - # Set up WordPress tests - - bash bin/install-wp-tests.sh wordpress_tests root mysql mysql 4.7 true - - phpunit --configuration phpunit.xml.dist - -WP4.6:PHP5.6: - stage: test - image: wpunit/php:5.6 - services: - - mysql:5.6 - script: - # Set up WordPress tests - - bash bin/install-wp-tests.sh wordpress_tests root mysql mysql 4.6 true - - phpunit --configuration phpunit.xml.dist - -WP4.5:PHP5.6: - stage: test - image: wpunit/php:5.6 - services: - - mysql:5.6 - script: - # Set up WordPress tests - - bash bin/install-wp-tests.sh wordpress_tests root mysql mysql 4.5 true - - phpunit --configuration phpunit.xml.dist - -WP4.4:PHP5.6: - stage: test - image: wpunit/php:5.6 - services: - - mysql:5.6 - script: - # Set up WordPress tests - - bash bin/install-wp-tests.sh wordpress_tests root mysql mysql 4.4 true - - phpunit --configuration phpunit.xml.dist - -WP4.3:PHP5.6: - stage: test - image: wpunit/php:5.6 - services: - - mysql:5.6 - script: - # Set up WordPress tests - - bash bin/install-wp-tests.sh wordpress_tests root mysql mysql 4.3 true - - phpunit --configuration phpunit.xml.dist - -WP4.2:PHP5.6: - stage: test - image: wpunit/php:5.6 - services: - - mysql:5.6 - script: - # Set up WordPress tests - - bash bin/install-wp-tests.sh wordpress_tests root mysql mysql 4.2 true - - phpunit --configuration phpunit.xml.dist - -WP4.1:PHP5.6: - stage: test - image: wpunit/php:5.6 - services: - - mysql:5.6 - script: - # Set up WordPress tests - - bash bin/install-wp-tests.sh wordpress_tests root mysql mysql 4.1 true - - phpunit --configuration phpunit.xml.dist - -WP4.0:PHP5.6: - stage: test - image: wpunit/php:5.6 - services: - - mysql:5.6 - script: - # Set up WordPress tests - - bash bin/install-wp-tests.sh wordpress_tests root mysql mysql 4.0 true - - phpunit --configuration phpunit.xml.dist - -WP3.9:PHP5.6: - stage: test - image: wpunit/php:5.6 - services: - - mysql:5.6 - script: - # Set up WordPress tests - - bash bin/install-wp-tests.sh wordpress_tests root mysql mysql 3.9 true - - phpunit --configuration phpunit.xml.dist - -WP3.8:PHP5.6: - stage: test - image: wpunit/php:5.6 - services: - - mysql:5.6 - script: - # Set up WordPress tests - - bash bin/install-wp-tests.sh wordpress_tests root mysql mysql 3.8 true - - phpunit --configuration phpunit.xml.dist - -Release: - stage: release - environment: - name: Release - image: wpunit/php:svn-release - when: manual - script: - - bash bin/gitlab-deploy.sh - only: - - tags \ No newline at end of file +variables: + # Configure mysql service + MYSQL_DATABASE: wordpress_test + MYSQL_ROOT_PASSWORD: root + # Cache vars + npm_config_cache: "$CI_PROJECT_DIR/.npm" + +cache: + key: "${CI_JOB_NAME}-${WP_VERSION}" + paths: + - vendor/ + - .npm/ + # Persist WordPress downloads to speed up builds + - /tmp/wordpress + - /tmp/wordpress-tests-lib + +# Job 1: Standards +standards: + stage: verify + image: wpunit/php:7.4 + script: + - composer install --prefer-dist --no-progress + - vendor/bin/phpcs + +# Job 2: Tests (Matrix) +tests: + stage: test + image: wpunit/php:${PHP_VERSION} + services: + - name: mysql:5.7 + alias: mysql + parallel: + matrix: + - PHP_VERSION: ['5.6', '7.4', '8.0'] + WP_VERSION: ['latest', 'nightly'] + script: + # Install system dependencies + # We use || true for apt-get update to avoid failing on EOL distros if the image is old + # but we try to install necessary packages. + - apt-get update || true + - apt-get install -y git subversion mysql-client zip unzip || true + + # Install dependencies + - composer install --prefer-dist --no-progress + + # Handle PHPUnit version for newer PHP versions + - | + if [[ "$PHP_VERSION" == "7.4" ]] || [[ "$PHP_VERSION" == "8.0" ]]; then + echo "Upgrading PHPUnit for PHP $PHP_VERSION..." + composer remove phpunit/phpunit --dev --no-update + composer config --unset platform.php + composer require phpunit/phpunit:"^9.0" --dev --update-with-dependencies --ignore-platform-reqs + fi + + # Setup WordPress Test Suite + # Arguments: db-name db-user db-pass db-host wp-version skip-db-create + - bash bin/install-wp-tests.sh wordpress_test root root mysql $WP_VERSION + + # Run tests + - vendor/bin/phpunit + +# Job 3: Deploy +deploy: + stage: deploy + image: alpine:latest + only: + - tags + before_script: + - apk add --no-cache bash git svn rsync findutils + script: + - bash bin/gitlab-deploy.sh diff --git a/bin/gitlab-deploy.sh b/bin/gitlab-deploy.sh old mode 100644 new mode 100755 index 5dffefe..b887396 --- a/bin/gitlab-deploy.sh +++ b/bin/gitlab-deploy.sh @@ -1,163 +1,80 @@ #!/bin/bash -# First some ArtWork, code is poetry -echo ' '; -echo ' ___ _________ ______________ _____ '; -echo ' __ | / /__ __ \ ___ __ \__ /___ ________ ___(_)______ '; -echo ' __ | /| / /__ /_/ / __ /_/ /_ /_ / / /_ __ `/_ /__ __ \ '; -echo ' __ |/ |/ / _ ____/ _ ____/_ / / /_/ /_ /_/ /_ / _ / / / '; -echo ' ____/|__/ /_/ /_/ /_/ \__,_/ _\__, / /_/ /_/ /_/ '; -echo ' /____/ '; -echo '_______ _____ ________ ______ '; -echo '___ |___ ___ /______ ___ __ \_______ /__________ ____________ '; -echo '__ /| | / / / __/ __ \ __ /_/ / _ \_ /_ _ \ __ `/_ ___/ _ \ '; -echo '_ ___ / /_/ // /_ / /_/ / _ _, _// __/ / / __/ /_/ /_(__ )/ __/ '; -echo '/_/ |_\__,_/ \__/ \____/ /_/ |_| \___//_/ \___/\__,_/ /____/ \___/ '; -echo ' '; - -# Check if global parameters are sent correctly -if [ ! -n "$SVN_USERNAME" ]; then - echo "Environment Variable SVN_USERNAME not defined..."; - export EXITSTATUS=1; -fi -if [ ! -n "$SVN_PASSWORD" ]; then - echo "Environment Variable SVN_PASSWORD not defined..."; - export EXITSTATUS=1; -fi -if [ ! -n "$SVN_REPO_URL" ]; then - echo "Environment Variable SVN_REPO_URL not defined..."; - export EXITSTATUS=1; +# Exit on error +set -e + +# Configuration +SVN_URL="https://plugins.svn.wordpress.org/shortcode-widget/" +SVN_DIR="/tmp/svn-repo" +SRC_DIR="${CI_PROJECT_DIR:-.}" # Defaults to current dir if not in CI + +# Check credentials +if [[ -z "$SVN_USERNAME" ]] || [[ -z "$SVN_PASSWORD" ]]; then + echo "Error: SVN_USERNAME and SVN_PASSWORD must be set." + exit 1 fi -if [ ! -n "$MAINFILE" ]; then - echo "Environment Variable MAINFILE (The main file of plugin) not defined..."; - export EXITSTATUS=1; -fi -if [ -n "$EXITSTATUS" ]; then - echo "Please define the above mentioned environment variables and try again..."; - exit 1; -fi - -# Defining all custom parameters -export DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )"/.. && pwd )"; -export PLUGINSLUG="$(basename $DIR)"; -export GITPATH="$DIR"; -export SVNPATH="/tmp/$PLUGINSLUG"; -export SVNTRUNK="$SVNPATH/trunk"; -export SVNTAGS="$SVNPATH/tags"; -export SVNASSETS="$SVNPATH/assets"; -# Let's begin... -echo "Preparing to deploy wordpress plugin..."; +echo "🚀 Starting deployment for $SVN_URL..." -# Check version in readme.txt is the same as plugin file -export NEWVERSION1=`grep "^Stable tag" $GITPATH/readme.txt | awk -F' ' '{print $3}'`; -echo "readme.txt version: $NEWVERSION1"; -export NEWVERSION2=`grep -i "Version" $GITPATH/$MAINFILE | head -n1 | awk -F':' '{print $2}' | awk -F' ' '{print $1}'`; -echo "$MAINFILE version: $NEWVERSION2"; +# 1. Checkout SVN +echo "🔄 Checking out SVN repository..." +rm -rf "$SVN_DIR" +svn co "$SVN_URL" "$SVN_DIR" --depth immediates --quiet --username "$SVN_USERNAME" --password "$SVN_PASSWORD" --no-auth-cache -# Exit if versions don't match -if [ "$NEWVERSION1" != "$NEWVERSION2" ]; then - echo "Versions don't match. Exiting..."; - exit 1; +# 2. Sync Assets +echo "🎨 Syncing assets..." +if [ -d "$SRC_DIR/assets" ]; then + mkdir -p "$SVN_DIR/assets" + rsync -rc --delete "$SRC_DIR/assets/" "$SVN_DIR/assets/" + # Add new assets to SVN + svn add --force "$SVN_DIR/assets" 2>/dev/null || true +else + echo "⚠️ No assets directory found." fi -echo "Versions match in readme.txt and $MAINFILE file. Let's proceed..."; - -echo "Creating local copy of SVN repo..."; -yes yes | svn co $SVN_REPO_URL $SVNPATH --quiet --username=$SVN_USERNAME --password=$SVN_PASSWORD; - -# Exit if svn checkout failed -if [ ! -d "$SVNPATH" ]; then - echo "Could not checkout from SVN. Please check above errors for help. Exiting..."; - exit 1; -fi +# 3. Sync Trunk +echo "📂 Syncing plugin files to trunk..." +mkdir -p "$SVN_DIR/trunk" +# Sync files using excludes +rsync -rc --delete --exclude-from="$SRC_DIR/bin/rsync-excludes.txt" "$SRC_DIR/" "$SVN_DIR/trunk/" -# Now check if required folders (trunk, tags and assets) are there in SVN repo -cd "$SVNPATH" -if [ ! -d "$SVNTRUNK" ]; then - echo "Creating and committing trunk directory..."; - mkdir "$SVNTRUNK"; - svn add "$SVNTRUNK"; - yes yes | svn commit -m "Trunk directory added" --username=$SVN_USERNAME --password=$SVN_PASSWORD; - echo "done"; -fi +# 4. SVN Operations +echo "⚙️ Processing SVN changes..." +cd "$SVN_DIR" -if [ ! -d "$SVNTAGS" ]; then - echo "Creating and committing tags directory..."; - mkdir "$SVNTAGS"; - svn add "$SVNTAGS"; - yes yes | svn commit -m "Tags directory added" --username=$SVN_USERNAME --password=$SVN_PASSWORD; - echo "done"; +# Add new files (force is needed for nested unversioned files) +# We look at status, filter for ? (unversioned), remove the status char, and add +if svn status | grep -q '^\?'; then + svn status | grep '^\?' | sed 's/^? *//' | xargs -d '\n' -I {} svn add --force "{}" fi -if [ ! -d "$SVNASSETS" ]; then - echo "Creating and committing assets directory..."; - mkdir "$SVNASSETS"; - svn add "$SVNASSETS"; - yes yes | svn commit -m "Adding assets directory" --username=$SVN_USERNAME --password=$SVN_PASSWORD; - echo "done"; +# Remove deleted files +# We look at status, filter for ! (missing), remove the status char, and delete +if svn status | grep -q '^\!'; then + svn status | grep '^\!' | sed 's/^! *//' | xargs -d '\n' -I {} svn rm "{}" fi -# Change directory to git repo -cd "$GITPATH"; - - -# If assets directory is there in git repo, try to create assets in SVN -if [ -d "$GITPATH/assets" ]; then - echo "Assets directory found, syncing assets locally..."; - # Sync assets from git repo to svn repo - rsync -av --delete "$GITPATH/assets/" "$SVNASSETS/"; - cd $SVNASSETS - # Check if there are any files to commit before running svn add - if [[ $(svn status) ]]; then - echo "Changes in assets detected, updating assets on SVN..."; - # Add only new files to svn if there are any - if [[ $(svn status | grep -v "^.[ \t]*\..*" | grep "^?" | awk '{print $2}') ]]; then - svn status | grep -v "^.[ \t]*\..*" | grep "^?" | awk '{print $2}' | xargs svn add; - fi - # Delete deleted files from svn also - if [[ $(svn status | grep -v "^.[ \t]*\..*" | grep "^!" | awk '{print $2}') ]]; then - svn status | grep -v "^.[ \t]*\..*" | grep "^!" | awk '{print $2}' | xargs svn delete; - fi - yes yes | svn commit -m "Assets updated $NEWVERSION1" --username=$SVN_USERNAME --password=$SVN_PASSWORD; - echo "done"; +# 5. Commit Trunk +echo "💾 Committing trunk changes..." +# We use the tag name in the commit message if available, otherwise HEAD +TAG_NAME="${CI_COMMIT_TAG:-HEAD}" +svn ci -m "Deploy from GitLab CI: $TAG_NAME" --username "$SVN_USERNAME" --password "$SVN_PASSWORD" --no-auth-cache + +# 6. Create SVN Tag (if running on a tag) +if [[ -n "$CI_COMMIT_TAG" ]]; then + echo "🏷️ Creating SVN Tag $CI_COMMIT_TAG..." + # Copy trunk to tags/TAG + # First update to make sure we have the latest revision number from the commit above + svn update --quiet + + # Check if tag already exists + if svn info "$SVN_URL/tags/$CI_COMMIT_TAG" >/dev/null 2>&1; then + echo "⚠️ Tag $CI_COMMIT_TAG already exists. Skipping tag creation." else - echo "No changes detected in assets..."; - fi -fi - -cd "$GITPATH"; -echo "Syncing local svn trunk with git repo..."; -rsync -av --delete --exclude-from "$GITPATH/bin/rsync-excludes.txt" "$GITPATH/" "$SVNTRUNK/"; - -cd $SVNTRUNK - -# Check if there are any files to commit before running svn add -if [[ $(svn status) ]]; then - echo "Changes in trunk detected, updating trunk..."; - # Add only new files to svn if there are any - if [[ $(svn status | grep -v "^.[ \t]*\..*" | grep "^?" | awk '{print $2}') ]]; then - svn status | grep -v "^.[ \t]*\..*" | grep "^?" | awk '{print $2}' | xargs svn add; + svn cp "$SVN_DIR/trunk" "$SVN_DIR/tags/$CI_COMMIT_TAG" + svn ci -m "Tagging version $CI_COMMIT_TAG" --username "$SVN_USERNAME" --password "$SVN_PASSWORD" --no-auth-cache + echo "✅ Tag $CI_COMMIT_TAG created." fi - # Delete deleted files from svn also - if [[ $(svn status | grep -v "^.[ \t]*\..*" | grep "^!" | awk '{print $2}') ]]; then - svn status | grep -v "^.[ \t]*\..*" | grep "^!" | awk '{print $2}' | xargs svn delete; - fi - yes yes | svn commit -m "Trunk updated $NEWVERSION1" --username=$SVN_USERNAME --password=$SVN_PASSWORD; - echo "done"; -else - echo "No changes in trunk, continuing..."; -fi - -cd "$SVNPATH"; -# Check if tag already exists in SVN, if not, then create new -if [ ! -d "$SVNTAGS/$NEWVERSION1" ]; then - echo "Creating new SVN tag..."; - svn copy "trunk/" "tags/$NEWVERSION1/"; - cd "$SVNPATH/tags/$NEWVERSION1"; - yes yes | svn commit -m "Tagging version $NEWVERSION1" --username=$SVN_USERNAME --password=$SVN_PASSWORD; -else - echo "Version $version tag already exists, skipping tag creation..."; fi -echo "Deployment Complete :) "; +echo "🎉 Deployment complete!" diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..8ac6f7d --- /dev/null +++ b/composer.json @@ -0,0 +1,20 @@ +{ + "name": "gagan0123/shortcode-widget", + "description": "Shortcode Widget WordPress Plugin", + "type": "wordpress-plugin", + "license": "GPL-2.0-or-later", + "config": { + "platform": { + "php": "5.6" + }, + "allow-plugins": { + "dealerdirect/phpcodesniffer-composer-installer": true + } + }, + "require-dev": { + "phpunit/phpunit": "^5.7", + "squizlabs/php_codesniffer": "^3.0", + "wp-coding-standards/wpcs": "^2.0", + "dealerdirect/phpcodesniffer-composer-installer": "*" + } +}