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:
Buster Neece 2023-02-03 14:46:35 -06:00
parent 757f082a4d
commit 29e61188fe
No known key found for this signature in database
GPG Key ID: F1D2E64A0005E80E
5 changed files with 72 additions and 35 deletions

View File

@ -1,3 +1,4 @@
.git
.devcontainer
.github
.run

View File

@ -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:

1
.gitignore vendored
View File

@ -10,6 +10,7 @@ tmp/cache/*---*
.idea
/ubuntu-*-console.log
.phplint-cache
.gitinfo
# Local development files.
/config/routes.dev.php

View File

@ -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 [];
}
/**

19
util/write_git_info.sh Normal file
View File

@ -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