Write important git info to a cache file so as to avoid loading the entire git repo into the Docker image.
This commit is contained in:
parent
757f082a4d
commit
29e61188fe
|
@ -1,3 +1,4 @@
|
||||||
|
.git
|
||||||
.devcontainer
|
.devcontainer
|
||||||
.github
|
.github
|
||||||
.run
|
.run
|
||||||
|
|
|
@ -112,6 +112,11 @@ jobs:
|
||||||
- name: Build OpenAPI Docs
|
- name: Build OpenAPI Docs
|
||||||
run: bin/console azuracast:api: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
|
- name: Set up Docker Buildx
|
||||||
uses: docker/setup-buildx-action@v2
|
uses: docker/setup-buildx-action@v2
|
||||||
|
|
||||||
|
@ -152,6 +157,7 @@ jobs:
|
||||||
name: assets
|
name: assets
|
||||||
if-no-files-found: error
|
if-no-files-found: error
|
||||||
path: |
|
path: |
|
||||||
|
.gitinfo
|
||||||
translations
|
translations
|
||||||
web/static/dist
|
web/static/dist
|
||||||
web/static/webpack_dist
|
web/static/webpack_dist
|
||||||
|
@ -167,10 +173,6 @@ jobs:
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@master
|
- uses: actions/checkout@master
|
||||||
|
|
||||||
- name: Reduce Git repository size.
|
|
||||||
run: |
|
|
||||||
git gc --prune=now --aggressive
|
|
||||||
|
|
||||||
- name: Download built static assets from previous step
|
- name: Download built static assets from previous step
|
||||||
uses: actions/download-artifact@v3
|
uses: actions/download-artifact@v3
|
||||||
with:
|
with:
|
||||||
|
|
|
@ -10,6 +10,7 @@ tmp/cache/*---*
|
||||||
.idea
|
.idea
|
||||||
/ubuntu-*-console.log
|
/ubuntu-*-console.log
|
||||||
.phplint-cache
|
.phplint-cache
|
||||||
|
.gitinfo
|
||||||
|
|
||||||
# Local development files.
|
# Local development files.
|
||||||
/config/routes.dev.php
|
/config/routes.dev.php
|
||||||
|
|
|
@ -7,6 +7,7 @@ namespace App;
|
||||||
use App\Enums\ReleaseChannel;
|
use App\Enums\ReleaseChannel;
|
||||||
use DateTime;
|
use DateTime;
|
||||||
use DateTimeZone;
|
use DateTimeZone;
|
||||||
|
use Dotenv\Dotenv;
|
||||||
use Psr\SimpleCache\CacheInterface;
|
use Psr\SimpleCache\CacheInterface;
|
||||||
use Symfony\Component\Process\Process;
|
use Symfony\Component\Process\Process;
|
||||||
|
|
||||||
|
@ -65,6 +66,24 @@ final class Version
|
||||||
|
|
||||||
if (empty($details)) {
|
if (empty($details)) {
|
||||||
$details = $this->getRawDetails();
|
$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;
|
$ttl = $this->environment->isProduction() ? 86400 : 600;
|
||||||
|
|
||||||
$this->cache->set('app_version_details', $details, $ttl);
|
$this->cache->set('app_version_details', $details, $ttl);
|
||||||
|
@ -81,42 +100,37 @@ final class Version
|
||||||
*/
|
*/
|
||||||
private function getRawDetails(): array
|
private function getRawDetails(): array
|
||||||
{
|
{
|
||||||
if (!is_dir($this->repoDir . '/.git')) {
|
if (is_file($this->repoDir . '/.gitinfo')) {
|
||||||
return [];
|
$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.
|
return [
|
||||||
$latest_commit_hash = $this->runProcess(['git', 'log', '--pretty=%H', '-n1', 'HEAD']);
|
'commit' => $this->runProcess(['git', 'log', '--pretty=%H', '-n1', 'HEAD']),
|
||||||
|
'commit_date_raw' => $this->runProcess(['git', 'log', '-n1', '--pretty=%ci', 'HEAD']),
|
||||||
$details['commit'] = $latest_commit_hash;
|
'tag' => (!empty($last_tagged_commit))
|
||||||
$details['commit_short'] = substr($latest_commit_hash, 0, 7);
|
? $this->runProcess(['git', 'describe', '--tags', $last_tagged_commit], 'N/A')
|
||||||
|
: null,
|
||||||
// Get the last commit's timestamp.
|
'branch' => $this->runProcess(['git', 'rev-parse', '--abbrev-ref', 'HEAD'], 'main'),
|
||||||
$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';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$last_tagged_commit = $this->runProcess(['git', 'rev-list', '--tags', '--max-count=1']);
|
return [];
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -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
|
Loading…
Reference in New Issue