diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 490346a..2a1c791 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -1,17 +1,28 @@ -name: Bump version and release +name: Release + +# Джерело версії — Init/module.json, а не теги. Саме його читає ядро: +# Installer пише версію в БД, і від неї залежить, які update_* спрацюють при +# оновленні. Тег і реліз — похідні: версію піднімають у PR разом із кодом, а +# після мерджу CI лише ставить тег на те, що вже у файлі. Тому CI нічого не +# пише в захищену гілку. +# +# Модель тегів: +# v — базовий реліз. Мусить працювати на обох рушіях. +# v-fork — збирається лише тоді, коли в репозиторії є гілка `fork`, +# тобто коли справді з'явилися форк-специфічні зміни. +# +# Рушії неможливо розрізнити за номером версії: і форк, і сток звуть себе +# 4.5.2 (Okay\Core\Config::$version). Тому кожна ціль нижче описана явно — +# своїм репозиторієм, своїм ref і своєю версією PHP. on: + # PR у main — лише перевірки. Мердж у main — перевірки й реліз, якщо версія + # у module.json ще не має тега. workflow_dispatch лишено для ручного повтору. + pull_request: + branches: [main] + push: + branches: [main] workflow_dispatch: - inputs: - bump: - description: "Select version bump type?" - required: true - default: "patch" - type: choice - options: - - major - - minor - - patch env: VENDOR: Sviat @@ -21,111 +32,353 @@ permissions: contents: write jobs: + lint: + name: PHP ${{ matrix.php }} syntax + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + # 7.4 — нижня межа стоку, 8.0 — верхня, 8.5 — форк. Код модуля + # мусить бути чистим на всіх трьох, бо їде в один архів. + php: ['7.4', '8.0', '8.5'] + steps: + - uses: actions/checkout@v4 + + - uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php }} + coverage: none + + - run: | + set -euo pipefail + # tests/ не лінтуються: в архів вони не їдуть і написані під PHP 8. + find . -path ./.git -prune -o -path ./tests -prune -o -name '*.php' -print0 \ + | xargs -0 -r -n1 -P4 php -l + + verify: + name: ${{ matrix.target }} (PHP ${{ matrix.php }}) + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + include: + - target: stock + repo: OkayCMS/OkayCMS + ref: '4.5.2' + # 8.0 — верхня версія, яку сток підтримує. Тести написані під + # PHP 8; синтаксис під 7.4 перевіряє окремий job lint. + php: '8.0' + - target: fork + repo: devSviat/OkayCMS + ref: main + php: '8.5' + steps: + - name: Check out the module + uses: actions/checkout@v4 + with: + path: module + + - name: Set up PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php }} + # redis: без нього тести кеш-модуля мовчки скіпаються, решті не заважає + extensions: simplexml, xmlreader, pdo, json, curl, mbstring, zip, gd, redis + coverage: none + tools: composer:v2 + + - name: Detect tests + id: tests + working-directory: module + run: | + set -euo pipefail + if [ -n "$(find tests -name '*Test.php' -print -quit 2>/dev/null)" ]; then + echo 'present=true' >> "$GITHUB_OUTPUT" + else + echo 'present=false' >> "$GITHUB_OUTPUT" + echo 'The module has no tests — skipping the run.' + fi + + # Тести модуля посилаються на класи ядра, тож виконуються всередині + # справжнього рушія. Розкладка — та сама, що у верстаку okay-lab. + - name: Check out the ${{ matrix.target }} engine + if: steps.tests.outputs.present == 'true' + uses: actions/checkout@v4 + with: + repository: ${{ matrix.repo }} + ref: ${{ matrix.ref }} + path: engine + + - name: Install engine dependencies + if: steps.tests.outputs.present == 'true' + working-directory: engine + run: composer install --no-interaction --prefer-dist --no-progress + + - name: Place the module into the engine + if: steps.tests.outputs.present == 'true' + run: | + set -euo pipefail + mkdir -p "engine/Okay/Modules/$VENDOR/$MODULE" "engine/tests/Modules/$VENDOR/$MODULE" + rsync -a --exclude=.git --exclude=.github --exclude=tests \ + module/ "engine/Okay/Modules/$VENDOR/$MODULE/" + rsync -a module/tests/ "engine/tests/Modules/$VENDOR/$MODULE/" + + - name: PHPUnit on ${{ matrix.target }} + if: steps.tests.outputs.present == 'true' + working-directory: engine + run: vendor/bin/phpunit "tests/Modules/$VENDOR/$MODULE" + release: + needs: [lint, verify] + if: github.event_name != 'pull_request' runs-on: ubuntu-latest + outputs: + version: ${{ steps.ver.outputs.version }} + tag: ${{ steps.ver.outputs.tag }} + needed: ${{ steps.ver.outputs.needed }} + commit: ${{ steps.tag.outputs.commit }} steps: - uses: actions/checkout@v4 with: fetch-depth: 0 - - name: Get latest tag - id: get_tag + # workflow_dispatch запускається з будь-якої гілки, а тег нижче ставиться + # на ту, з якої запущено. Без цієї перевірки реліз із випадкової гілки + # виглядав би як звичайний. + - name: Release from the default branch only run: | - last_tag=$(git describe --tags --abbrev=0 || echo "v0.0.0") - echo "last_tag=$last_tag" >> $GITHUB_OUTPUT + set -euo pipefail + default='${{ github.event.repository.default_branch }}' + if [ '${{ github.ref_name }}' != "$default" ]; then + echo "Releases run from $default only, not from ${{ github.ref_name }}." + exit 1 + fi - - name: Bump version - id: bump + - name: Read the version to release + id: ver run: | - last=${{ steps.get_tag.outputs.last_tag }} - bump=${{ github.event.inputs.bump }} - - ver=${last#v} - major=$(echo $ver | cut -d. -f1) - minor=$(echo $ver | cut -d. -f2) - patch=$(echo $ver | cut -d. -f3) - - case $bump in - major) - major=$((major+1)) - minor=0 - patch=0 - ;; - minor) - minor=$((minor+1)) - patch=0 - ;; - patch) - patch=$((patch+1)) - ;; - esac - - new_tag="v$major.$minor.$patch" - echo "new_tag=$new_tag" >> $GITHUB_OUTPUT - - - name: Set build variables - id: vars + set -euo pipefail + v=$(python3 -c 'import json; print(json.load(open("Init/module.json"))["version"])') + echo "$v" | grep -qE '^[0-9]+\.[0-9]+\.[0-9]+$' + { + echo "version=$v" + echo "tag=v$v" + } >> "$GITHUB_OUTPUT" + # Тег на цю версію вже є — виходимо зеленими: PR без підняття версії + # не має фарбувати main у червоне. + if git rev-parse -q --verify "refs/tags/v$v" >/dev/null; then + echo 'needed=false' >> "$GITHUB_OUTPUT" + echo "v$v is already tagged — nothing to release." + else + echo 'needed=true' >> "$GITHUB_OUTPUT" + fi + + - name: Create the tag + id: tag + if: steps.ver.outputs.needed == 'true' run: | - echo "module_path=${{ env.VENDOR }}/${{ env.MODULE }}" >> $GITHUB_OUTPUT - echo "zip_name=${{ env.MODULE }}-${{ steps.bump.outputs.new_tag }}.zip" >> $GITHUB_OUTPUT + set -euo pipefail + git tag '${{ steps.ver.outputs.tag }}' + git push origin '${{ steps.ver.outputs.tag }}' + echo "commit=$(git rev-parse HEAD)" >> "$GITHUB_OUTPUT" - - name: Check for changes since last tag + - name: Build the archive + if: steps.ver.outputs.needed == 'true' run: | - LAST_TAG=${{ steps.get_tag.outputs.last_tag }} - if [ "$LAST_TAG" != "v0.0.0" ] && [ "$(git rev-parse $LAST_TAG)" = "$(git rev-parse HEAD)" ]; then - echo "No new commits since $LAST_TAG. Aborting." + set -euo pipefail + mkdir -p dist "build/$VENDOR/$MODULE" + rsync -a --exclude=.git --exclude=.github --exclude=.gitignore \ + --exclude=README.md --exclude=tests --exclude=dist --exclude=build \ + ./ "build/$VENDOR/$MODULE/" + (cd build && zip -rq "../dist/$MODULE-${{ steps.ver.outputs.tag }}.zip" "$VENDOR") + + - name: Publish the release + if: steps.ver.outputs.needed == 'true' + uses: softprops/action-gh-release@v2 + with: + tag_name: ${{ steps.ver.outputs.tag }} + name: ${{ steps.ver.outputs.tag }} + generate_release_notes: true + files: dist/*.zip + + # Чи є взагалі форк-специфічні зміни. Окремим job'ом, бо результат потрібен + # у job-рівневих `if` наступних двох. + detect-fork: + needs: release + if: needs.release.outputs.needed == 'true' + runs-on: ubuntu-latest + outputs: + present: ${{ steps.check.outputs.present }} + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Detect the fork branch + id: check + run: | + set -euo pipefail + if git ls-remote --exit-code --heads origin fork >/dev/null 2>&1; then + echo 'present=true' >> "$GITHUB_OUTPUT" + else + echo 'present=false' >> "$GITHUB_OUTPUT" + echo 'No fork branch, so no fork-specific changes — the variant is not built.' + fi + + # Явна відмова замість тихого злиття: якщо fork не містить щойно + # випущеного коміту, його архів був би старішим за базовий і мовчки + # роз'їхався б з ним. + - name: Ensure the fork branch contains the base release + if: steps.check.outputs.present == 'true' + run: | + set -euo pipefail + base='${{ needs.release.outputs.commit }}' + git fetch origin fork + if ! git merge-base --is-ancestor "$base" origin/fork; then + echo "The fork branch does not contain release commit $base." + echo 'Merge the default branch into it and run the release again.' exit 1 fi - - name: Create new tag + # Дзеркало job'а lint для гілки fork: її код так само їде в архів. + lint-fork: + needs: detect-fork + if: needs.detect-fork.outputs.present == 'true' + name: fork branch / PHP ${{ matrix.php }} syntax + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + php: ['7.4', '8.0', '8.5'] + steps: + - uses: actions/checkout@v4 + with: + ref: fork + + - uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php }} + coverage: none + + - run: | + set -euo pipefail + # tests/ не лінтуються: в архів вони не їдуть і написані під PHP 8. + find . -path ./.git -prune -o -path ./tests -prune -o -name '*.php' -print0 \ + | xargs -0 -r -n1 -P4 php -l + + # Той самий набір перевірок, що й для базового релізу, але на гілці fork. + # Без цього job'а рушій-специфічний код — єдиний, де різниця рушіїв взагалі + # може зламатися, — виходив би в реліз узагалі неперевіреним. + verify-fork: + needs: detect-fork + if: needs.detect-fork.outputs.present == 'true' + name: fork branch / ${{ matrix.target }} (PHP ${{ matrix.php }}) + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + include: + - target: stock + repo: OkayCMS/OkayCMS + ref: '4.5.2' + # 8.0 — верхня версія, яку сток підтримує. Тести написані під + # PHP 8; синтаксис під 7.4 перевіряє окремий job lint. + php: '8.0' + - target: fork + repo: devSviat/OkayCMS + ref: main + php: '8.5' + steps: + - name: Check out the fork branch + uses: actions/checkout@v4 + with: + ref: fork + path: module + + - name: Set up PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php }} + # redis: без нього тести кеш-модуля мовчки скіпаються, решті не заважає + extensions: simplexml, xmlreader, pdo, json, curl, mbstring, zip, gd, redis + coverage: none + tools: composer:v2 + + - name: Detect tests + id: tests + working-directory: module run: | - git config user.name "github-actions[bot]" - git config user.email "github-actions[bot]@users.noreply.github.com" - NEW_TAG=${{ steps.bump.outputs.new_tag }} - if git rev-parse "$NEW_TAG" >/dev/null 2>&1; then - echo "Tag $NEW_TAG already exists. Deleting it." - git tag -d "$NEW_TAG" - git push origin --delete "$NEW_TAG" || true + set -euo pipefail + if [ -n "$(find tests -name '*Test.php' -print -quit 2>/dev/null)" ]; then + echo 'present=true' >> "$GITHUB_OUTPUT" + else + echo 'present=false' >> "$GITHUB_OUTPUT" + echo 'The module has no tests — skipping the run.' fi - git tag "$NEW_TAG" - git push origin "$NEW_TAG" - - name: Build module archive + - name: Check out the ${{ matrix.target }} engine + if: steps.tests.outputs.present == 'true' + uses: actions/checkout@v4 + with: + repository: ${{ matrix.repo }} + ref: ${{ matrix.ref }} + path: engine + + - name: Install engine dependencies + if: steps.tests.outputs.present == 'true' + working-directory: engine + run: composer install --no-interaction --prefer-dist --no-progress + + - name: Place the module into the engine + if: steps.tests.outputs.present == 'true' run: | - mkdir -p dist - mkdir -p temp_build/${{ steps.vars.outputs.module_path }} - - rsync -av \ - --exclude='.github' \ - --exclude='.gitignore' \ - --exclude='README.md' \ - --exclude='temp_build' \ - --exclude='dist' \ - --exclude='.git' \ - ./ temp_build/${{ steps.vars.outputs.module_path }}/ - - cd temp_build - zip -r ../dist/${{ steps.vars.outputs.zip_name }} ${{ steps.vars.outputs.module_path }} - cd .. - rm -rf temp_build - - - name: Delete existing release if it exists + set -euo pipefail + mkdir -p "engine/Okay/Modules/$VENDOR/$MODULE" "engine/tests/Modules/$VENDOR/$MODULE" + rsync -a --exclude=.git --exclude=.github --exclude=tests \ + module/ "engine/Okay/Modules/$VENDOR/$MODULE/" + rsync -a module/tests/ "engine/tests/Modules/$VENDOR/$MODULE/" + + - name: PHPUnit on ${{ matrix.target }} + if: steps.tests.outputs.present == 'true' + working-directory: engine + run: vendor/bin/phpunit "tests/Modules/$VENDOR/$MODULE" + + release-fork: + needs: [release, detect-fork, lint-fork, verify-fork] + if: needs.detect-fork.outputs.present == 'true' + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + with: + ref: fork + fetch-depth: 0 + + - name: Align the version and create the tag run: | - TAG=${{ steps.bump.outputs.new_tag }} - RELEASE_ID=$(curl -s -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \ - https://api.github.com/repos/${{ github.repository }}/releases/tags/$TAG | jq .id) - if [ "$RELEASE_ID" != "null" ]; then - curl -s -X DELETE -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \ - https://api.github.com/repos/${{ github.repository }}/releases/$RELEASE_ID - fi + set -euo pipefail + v='${{ needs.release.outputs.version }}' + sed -i -E 's/("version"[[:space:]]*:[[:space:]]*")[^"]*"/\1'"$v"'"/' Init/module.json + python3 -c 'import json; json.load(open("Init/module.json"))' + git config user.name 'github-actions[bot]' + git config user.email 'github-actions[bot]@users.noreply.github.com' + git diff --quiet || git commit -am "Version $v" + git push origin HEAD:fork + git tag '${{ needs.release.outputs.tag }}-fork' + git push origin '${{ needs.release.outputs.tag }}-fork' + + - name: Build the archive + run: | + set -euo pipefail + mkdir -p dist "build/$VENDOR/$MODULE" + rsync -a --exclude=.git --exclude=.github --exclude=.gitignore \ + --exclude=README.md --exclude=tests --exclude=dist --exclude=build \ + ./ "build/$VENDOR/$MODULE/" + (cd build && zip -rq "../dist/$MODULE-${{ needs.release.outputs.tag }}-fork.zip" "$VENDOR") - - name: Create GitHub release + - name: Publish the release uses: softprops/action-gh-release@v2 with: - tag_name: ${{ steps.bump.outputs.new_tag }} - name: Release ${{ steps.bump.outputs.new_tag }} - generate_release_notes: true - files: dist/${{ steps.vars.outputs.zip_name }} - fail_on_unmatched_files: false - draft: false - prerelease: false \ No newline at end of file + tag_name: ${{ needs.release.outputs.tag }}-fork + name: ${{ needs.release.outputs.tag }}-fork + body: "Variant for the OkayCMS fork (PHP 8.5, Smarty 5). Base release — ${{ needs.release.outputs.tag }}." + files: dist/*.zip