diff --git a/.dockerignore b/.dockerignore index 45d1b35f0..b58d42bdd 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,3 +1,4 @@ +.git .devcontainer .github .run diff --git a/.github/workflows/default.yml b/.github/workflows/default.yml index 08eb85e14..cbd81bd20 100644 --- a/.github/workflows/default.yml +++ b/.github/workflows/default.yml @@ -112,6 +112,11 @@ jobs: - name: Build OpenAPI Docs run: bin/console azuracast:api:docs + - name: Write .gitinfo File + run: | + bash util/write_git_info.sh + chmod 777 .gitinfo + - name: Set up Docker Buildx uses: docker/setup-buildx-action@v2 @@ -152,6 +157,7 @@ jobs: name: assets if-no-files-found: error path: | + .gitinfo translations web/static/dist web/static/webpack_dist @@ -167,10 +173,6 @@ jobs: steps: - uses: actions/checkout@master - - name: Reduce Git repository size. - run: | - git gc --prune=now --aggressive - - name: Download built static assets from previous step uses: actions/download-artifact@v3 with: diff --git a/.gitignore b/.gitignore index 50ca47467..dc4191f00 100644 --- a/.gitignore +++ b/.gitignore @@ -10,6 +10,7 @@ tmp/cache/*---* .idea /ubuntu-*-console.log .phplint-cache +.gitinfo # Local development files. /config/routes.dev.php diff --git a/src/Version.php b/src/Version.php index e2c646d6e..1988e9846 100644 --- a/src/Version.php +++ b/src/Version.php @@ -7,6 +7,7 @@ namespace App; use App\Enums\ReleaseChannel; use DateTime; use DateTimeZone; +use Dotenv\Dotenv; use Psr\SimpleCache\CacheInterface; use Symfony\Component\Process\Process; @@ -65,6 +66,24 @@ final class Version if (empty($details)) { $details = $this->getRawDetails(); + + $details['commit_short'] = substr($details['commit'] ?? '', 0, 7); + + if (!empty($details['commit_date_raw'])) { + $commit_date = new DateTime($details['commit_date_raw']); + $commit_date->setTimezone(new DateTimeZone('UTC')); + + $details['commit_timestamp'] = $commit_date->getTimestamp(); + $details['commit_date'] = $commit_date->format('Y-m-d G:i'); + } else { + $details['commit_timestamp'] = 0; + $details['commit_date'] = 'N/A'; + } + + if (empty($details['tag'])) { + $details['tag'] = self::FALLBACK_VERSION; + } + $ttl = $this->environment->isProduction() ? 86400 : 600; $this->cache->set('app_version_details', $details, $ttl); @@ -81,42 +100,37 @@ final class Version */ private function getRawDetails(): array { - if (!is_dir($this->repoDir . '/.git')) { - return []; + if (is_file($this->repoDir . '/.gitinfo')) { + $fileContents = file_get_contents($this->repoDir . '/.gitinfo'); + if (!empty($fileContents)) { + try { + $gitInfo = Dotenv::parse($fileContents); + return [ + 'commit' => $gitInfo['COMMIT_LONG'] ?? null, + 'commit_date_raw' => $gitInfo['COMMIT_DATE'] ?? null, + 'tag' => $gitInfo['TAG'] ?? null, + 'branch' => $gitInfo['BRANCH'] ?? null, + ]; + } catch (\Throwable) { + // Noop + } + } } - $details = []; + if (is_dir($this->repoDir . '/.git')) { + $last_tagged_commit = $this->runProcess(['git', 'rev-list', '--tags', '--max-count=1']); - // Get the long form of the latest commit's hash. - $latest_commit_hash = $this->runProcess(['git', 'log', '--pretty=%H', '-n1', 'HEAD']); - - $details['commit'] = $latest_commit_hash; - $details['commit_short'] = substr($latest_commit_hash, 0, 7); - - // Get the last commit's timestamp. - $latest_commit_date = $this->runProcess(['git', 'log', '-n1', '--pretty=%ci', 'HEAD']); - - if (!empty($latest_commit_date)) { - $commit_date = new DateTime($latest_commit_date); - $commit_date->setTimezone(new DateTimeZone('UTC')); - - $details['commit_timestamp'] = $commit_date->getTimestamp(); - $details['commit_date'] = $commit_date->format('Y-m-d G:i'); - } else { - $details['commit_timestamp'] = 0; - $details['commit_date'] = 'N/A'; + return [ + 'commit' => $this->runProcess(['git', 'log', '--pretty=%H', '-n1', 'HEAD']), + 'commit_date_raw' => $this->runProcess(['git', 'log', '-n1', '--pretty=%ci', 'HEAD']), + 'tag' => (!empty($last_tagged_commit)) + ? $this->runProcess(['git', 'describe', '--tags', $last_tagged_commit], 'N/A') + : null, + 'branch' => $this->runProcess(['git', 'rev-parse', '--abbrev-ref', 'HEAD'], 'main'), + ]; } - $last_tagged_commit = $this->runProcess(['git', 'rev-list', '--tags', '--max-count=1']); - if (!empty($last_tagged_commit)) { - $details['tag'] = $this->runProcess(['git', 'describe', '--tags', $last_tagged_commit], 'N/A'); - } else { - $details['tag'] = self::FALLBACK_VERSION; - } - - $details['branch'] = $this->runProcess(['git', 'rev-parse', '--abbrev-ref', 'HEAD'], 'main'); - - return $details; + return []; } /** diff --git a/util/write_git_info.sh b/util/write_git_info.sh new file mode 100644 index 000000000..eace98c4e --- /dev/null +++ b/util/write_git_info.sh @@ -0,0 +1,19 @@ +#!/usr/bin/env bash + +# Ensure we're in the same directory as this script. +cd "$( dirname "${BASH_SOURCE[0]}" )" || exit + +cd .. + +COMMIT_LONG=$(git log --pretty=%H -n1 HEAD) +COMMIT_DATE=$(git log -n1 --pretty=%ci HEAD) + +LAST_TAG_HASH=$(git rev-list --tags --max-count=1) +LAST_TAG=$(git describe --tags $LAST_TAG_HASH) + +BRANCH=$(git rev-parse --abbrev-ref HEAD main | head -n 1) + +printf "COMMIT_LONG=\"%s\"\n" "$COMMIT_LONG" > .gitinfo +printf "COMMIT_DATE=\"%s\"\n" "$COMMIT_DATE" >> .gitinfo +printf "LAST_TAG=\"%s\"\n" "$LAST_TAG" >> .gitinfo +printf "BRANCH=\"%s\"\n" "$BRANCH" >> .gitinfo