feat: expose module version

This commit is contained in:
2025-12-04 22:07:41 +03:00
parent 44d2af3b30
commit f1a39eeb00
5 changed files with 86 additions and 59 deletions

View File

@@ -16,6 +16,45 @@ permissions:
contents: write
jobs:
version_meta:
name: Compute version metadata
runs-on: ubuntu-latest
outputs:
tag: ${{ steps.meta.outputs.tag }}
filename: ${{ steps.meta.outputs.filename }}
is_release: ${{ steps.meta.outputs.is_release }}
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Extract tag and set filename
id: meta
run: |
set -euo pipefail
RELEASE_TAG=$(git tag --points-at HEAD | grep -E '^v[0-9]+\.[0-9]+\.[0-9]+$' | head -n 1)
if [ -n "$RELEASE_TAG" ]; then
echo "Это полноценный релиз"
TAG="$RELEASE_TAG"
FILENAME="oc_telegram_shop_${TAG}.ocmod.zip"
IS_RELEASE=true
else
echo "Это dev-сборка"
LAST_TAG=$(git tag --sort=-v:refname | grep -E '^v[0-9]+\.[0-9]+\.[0-9]+$' | head -n 1)
[ -z "$LAST_TAG" ] && LAST_TAG="v0.0.0"
SHORT_SHA=$(git rev-parse --short=7 HEAD)
DATE=$(date +%Y%m%d%H%M)
TAG="${LAST_TAG}-dev.${DATE}+${SHORT_SHA}"
FILENAME="oc_telegram_shop_${TAG}.ocmod.zip"
IS_RELEASE=false
fi
echo "is_release=$IS_RELEASE" >> $GITHUB_OUTPUT
echo "tag=$TAG" >> $GITHUB_OUTPUT
echo "filename=$FILENAME" >> $GITHUB_OUTPUT
test_frontend:
name: Run Frontend tests
runs-on: ubuntu-latest
@@ -79,6 +118,7 @@ jobs:
module-build:
name: Build module.
runs-on: ubuntu-latest
needs: version_meta
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v6
@@ -89,6 +129,12 @@ jobs:
php-version: '7.4'
tools: composer
- name: Write version.txt
run: |
set -euo pipefail
MODULE_ROOT="module/oc_telegram_shop/upload/oc_telegram_shop"
echo "${{ needs.version_meta.outputs.tag }}" > "${MODULE_ROOT}/version.txt"
- name: Build module
run: |
bash scripts/ci/build.sh "${GITHUB_WORKSPACE}"
@@ -102,38 +148,10 @@ jobs:
release:
runs-on: ubuntu-latest
needs: [ test_frontend, test_backend, module-build ]
needs: [ version_meta, test_frontend, test_backend, module-build ]
if: github.ref == 'refs/heads/master'
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0 # to fetch tags
- name: Extract tag and set filename
id: meta
run: |
# Проверяем, указывает ли HEAD на тег (релиз)
RELEASE_TAG=$(git tag --points-at HEAD | grep -E '^v[0-9]+\.[0-9]+\.[0-9]+$' | head -n 1)
if [ -n "$RELEASE_TAG" ]; then
echo "Это полноценный релиз"
TAG="$RELEASE_TAG"
FILENAME="oc_telegram_shop_${TAG}.ocmod.zip"
IS_RELEASE=true
else
echo "Это dev-сборка"
LAST_TAG=$(git tag --sort=-v:refname | grep -E '^v[0-9]+\.[0-9]+\.[0-9]+$' | head -n 1)
[ -z "$LAST_TAG" ] && LAST_TAG="v0.0.0"
SHORT_SHA=$(git rev-parse --short=7 HEAD)
DATE=$(date +%Y%m%d%H%M)
TAG="${LAST_TAG}-dev.${DATE}+${SHORT_SHA}"
FILENAME="oc_telegram_shop_${TAG}.ocmod.zip"
IS_RELEASE=false
fi
echo "is_release=$IS_RELEASE" >> $GITHUB_OUTPUT
echo "tag=$TAG" >> $GITHUB_OUTPUT
echo "filename=$FILENAME" >> $GITHUB_OUTPUT
- name: Download build artifact
uses: actions/download-artifact@v4
@@ -142,13 +160,13 @@ jobs:
path: ./build
- name: Rename artifact file
run: mv ./build/oc_telegram_shop.ocmod.zip ./build/${{ steps.meta.outputs.filename }}
run: mv ./build/oc_telegram_shop.ocmod.zip ./build/${{ needs.version_meta.outputs.filename }}
- name: Delete existing GitHub release and tag
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
TAG=${{ steps.meta.outputs.tag }}
TAG=${{ needs.version_meta.outputs.tag }}
echo "⛔ Deleting existing release and tag (if any): $TAG"
gh release delete "$TAG" --cleanup-tag --yes || true
git push origin ":refs/tags/$TAG" || true
@@ -156,9 +174,9 @@ jobs:
- name: Create GitHub Release
uses: softprops/action-gh-release@v2
with:
draft: ${{ steps.meta.outputs.is_release == 'false' }}
tag_name: ${{ steps.meta.outputs.tag }}
files: ./build/${{ steps.meta.outputs.filename }}
draft: ${{ needs.version_meta.outputs.is_release == 'false' }}
tag_name: ${{ needs.version_meta.outputs.tag }}
files: ./build/${{ needs.version_meta.outputs.filename }}
generate_release_notes: true
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

View File

@@ -47,5 +47,6 @@ class JsonResponse extends Response
header('Access-Control-Allow-Methods: GET, POST');
header('Access-Control-Allow-Headers: Content-Type, Authorization');
header('Access-Control-Allow-Credentials: true');
header('X-TELECART-VERSION: ' . module_version());
}
}

View File

@@ -101,3 +101,32 @@ if (! function_exists('env')) {
return $value;
}
}
if (! function_exists('module_version')) {
function module_version(): string
{
static $version = null;
if ($version !== null) {
return $version;
}
$versionFile = BP_BASE_PATH . '/version.txt';
if (is_readable($versionFile)) {
$raw = trim((string) file_get_contents($versionFile));
if ($raw !== '') {
return $version = $raw;
}
}
$composerFile = BP_BASE_PATH . '/composer.json';
if (is_readable($composerFile)) {
$composer = json_decode((string) file_get_contents($composerFile), true);
if (is_array($composer) && ! empty($composer['version'])) {
return $version = (string) $composer['version'];
}
}
return $version = 'unknown';
}
}

View File

@@ -106,7 +106,7 @@ class TeleCartPulseService
$botName = $me['username'] ?? 'unknown';
$moduleVersion = $this->getModuleVersion();
$moduleVersion = module_version();
$payload = [
'event' => 'HEARTBEAT',
@@ -184,7 +184,7 @@ class TeleCartPulseService
'timeout' => env('PULSE_TIMEOUT', 5.0),
'headers' => [
'Authorization' => 'Bearer ' . $this->apiKey,
'X-TELECART-VERSION' => $this->getModuleVersion(),
'X-TELECART-VERSION' => module_version(),
],
]);
@@ -202,35 +202,13 @@ class TeleCartPulseService
'base_uri' => $baseUri,
'timeout' => env('PULSE_TIMEOUT', 2.0),
'headers' => [
'X-TELECART-VERSION' => $this->getModuleVersion(),
'X-TELECART-VERSION' => module_version(),
],
]);
$client->post('heartbeat', compact('json'));
}
private function getModuleVersion(): string
{
if ($this->moduleVersion !== null) {
return $this->moduleVersion;
}
$moduleVersion = 'unknown';
$composerPath = __DIR__ . '/../../composer.json';
if (file_exists($composerPath)) {
$composerRaw = @file_get_contents($composerPath);
if ($composerRaw !== false) {
$composer = json_decode($composerRaw, true);
if (is_array($composer) && isset($composer['version'])) {
$moduleVersion = (string)$composer['version'];
}
}
}
return $this->moduleVersion = $moduleVersion;
}
private function handleOrderCreated(array $data, array $deserialized): void
{
if (isset($deserialized['campaign_id'], $deserialized['tracking_id'])) {

View File

@@ -0,0 +1 @@
v2.0.0