mirror of
https://github.com/tiennm99/coolify.git
synced 2026-07-25 16:21:12 +00:00
feat(deployments): track application configuration diffs (#10183)
This commit is contained in:
+90
-28
@@ -4,6 +4,9 @@ namespace App\Models;
|
||||
|
||||
use App\Enums\ApplicationDeploymentStatus;
|
||||
use App\Services\ConfigurationGenerator;
|
||||
use App\Services\DeploymentConfiguration\ApplicationConfigurationSnapshot;
|
||||
use App\Services\DeploymentConfiguration\ConfigurationDiff;
|
||||
use App\Services\DeploymentConfiguration\ConfigurationDiffer;
|
||||
use App\Traits\ClearsGlobalSearchCache;
|
||||
use App\Traits\HasConfiguration;
|
||||
use App\Traits\HasMetrics;
|
||||
@@ -720,14 +723,14 @@ class Application extends BaseModel
|
||||
return Attribute::make(
|
||||
set: function ($value) {
|
||||
if (is_null($value) || $value === '') {
|
||||
return '/Dockerfile';
|
||||
} else {
|
||||
if ($value !== '/') {
|
||||
return Str::start(Str::replaceEnd('/', '', $value), '/');
|
||||
}
|
||||
|
||||
return Str::start($value, '/');
|
||||
return $this->build_pack === 'dockerfile' ? '/Dockerfile' : null;
|
||||
}
|
||||
|
||||
if ($value !== '/') {
|
||||
return Str::start(Str::replaceEnd('/', '', $value), '/');
|
||||
}
|
||||
|
||||
return Str::start($value, '/');
|
||||
}
|
||||
);
|
||||
}
|
||||
@@ -1059,7 +1062,7 @@ class Application extends BaseModel
|
||||
|
||||
public function get_last_successful_deployment()
|
||||
{
|
||||
return ApplicationDeploymentQueue::where('application_id', $this->id)->where('status', ApplicationDeploymentStatus::FINISHED)->where('pull_request_id', 0)->orderBy('created_at', 'desc')->first();
|
||||
return ApplicationDeploymentQueue::where('application_id', $this->id)->where('status', ApplicationDeploymentStatus::FINISHED->value)->where('pull_request_id', 0)->orderBy('created_at', 'desc')->first();
|
||||
}
|
||||
|
||||
public function get_last_days_deployments()
|
||||
@@ -1170,33 +1173,92 @@ class Application extends BaseModel
|
||||
}
|
||||
|
||||
public function isConfigurationChanged(bool $save = false)
|
||||
{
|
||||
$configurationDiff = $this->pendingDeploymentConfigurationDiff();
|
||||
|
||||
if ($save) {
|
||||
$this->markDeploymentConfigurationApplied();
|
||||
}
|
||||
|
||||
return $configurationDiff->isChanged();
|
||||
}
|
||||
|
||||
public function pendingDeploymentConfigurationDiff(): ConfigurationDiff
|
||||
{
|
||||
$currentSnapshot = $this->deploymentConfigurationSnapshot();
|
||||
$lastDeployment = $this->get_last_successful_deployment();
|
||||
|
||||
if ($lastDeployment?->configuration_snapshot) {
|
||||
return app(ConfigurationDiffer::class)->diff($lastDeployment->configuration_snapshot, $currentSnapshot);
|
||||
}
|
||||
|
||||
$oldConfigHash = data_get($this, 'config_hash');
|
||||
|
||||
if ($oldConfigHash === null) {
|
||||
return ConfigurationDiff::legacy(true);
|
||||
}
|
||||
|
||||
return ConfigurationDiff::legacy($oldConfigHash !== $this->legacyConfigurationHash());
|
||||
}
|
||||
|
||||
public function hasPendingDeploymentConfigurationChanges(): bool
|
||||
{
|
||||
return $this->pendingDeploymentConfigurationDiff()->isChanged();
|
||||
}
|
||||
|
||||
public function deploymentConfigurationSnapshot(): array
|
||||
{
|
||||
return (new ApplicationConfigurationSnapshot($this))->toArray();
|
||||
}
|
||||
|
||||
public function deploymentConfigurationHash(): string
|
||||
{
|
||||
return ApplicationConfigurationSnapshot::hashSnapshot($this->deploymentConfigurationSnapshot());
|
||||
}
|
||||
|
||||
public function markDeploymentConfigurationApplied(?ApplicationDeploymentQueue $deployment = null): void
|
||||
{
|
||||
$this->refresh();
|
||||
|
||||
if (! $deployment) {
|
||||
$this->forceFill(['config_hash' => $this->legacyConfigurationHash()])->save();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$snapshot = $this->deploymentConfigurationSnapshot();
|
||||
$hash = ApplicationConfigurationSnapshot::hashSnapshot($snapshot);
|
||||
|
||||
$previousDeployment = ApplicationDeploymentQueue::query()
|
||||
->where('application_id', $this->id)
|
||||
->where('status', ApplicationDeploymentStatus::FINISHED->value)
|
||||
->where('pull_request_id', $deployment->pull_request_id ?? 0)
|
||||
->where('id', '!=', $deployment->id)
|
||||
->whereNotNull('configuration_snapshot')
|
||||
->latest()
|
||||
->first();
|
||||
|
||||
$deployment->update([
|
||||
'configuration_hash' => $hash,
|
||||
'configuration_snapshot' => $snapshot,
|
||||
'configuration_diff' => $previousDeployment?->configuration_snapshot
|
||||
? app(ConfigurationDiffer::class)->diff($previousDeployment->configuration_snapshot, $snapshot)->toArray()
|
||||
: null,
|
||||
]);
|
||||
|
||||
$this->forceFill(['config_hash' => $hash])->save();
|
||||
}
|
||||
|
||||
private function legacyConfigurationHash(): string
|
||||
{
|
||||
$newConfigHash = base64_encode($this->fqdn.$this->git_repository.$this->git_branch.$this->git_commit_sha.$this->build_pack.$this->static_image.$this->install_command.$this->build_command.$this->start_command.$this->ports_exposes.$this->ports_mappings.$this->custom_network_aliases.$this->base_directory.$this->publish_directory.$this->dockerfile.$this->dockerfile_location.$this->custom_labels.$this->custom_docker_run_options.$this->dockerfile_target_build.$this->redirect.$this->custom_nginx_configuration.$this->settings?->use_build_secrets.$this->settings?->inject_build_args_to_dockerfile.$this->settings?->include_source_commit_in_build);
|
||||
if ($this->pull_request_id === 0 || $this->pull_request_id === null) {
|
||||
$newConfigHash .= json_encode($this->environment_variables()->get(['value', 'is_multiline', 'is_literal', 'is_buildtime', 'is_runtime'])->sort());
|
||||
} else {
|
||||
$newConfigHash .= json_encode($this->environment_variables_preview->get(['value', 'is_multiline', 'is_literal', 'is_buildtime', 'is_runtime'])->sort());
|
||||
$newConfigHash .= json_encode($this->environment_variables_preview()->get(['value', 'is_multiline', 'is_literal', 'is_buildtime', 'is_runtime'])->sort());
|
||||
}
|
||||
$newConfigHash = md5($newConfigHash);
|
||||
$oldConfigHash = data_get($this, 'config_hash');
|
||||
if ($oldConfigHash === null) {
|
||||
if ($save) {
|
||||
$this->config_hash = $newConfigHash;
|
||||
$this->save();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
if ($oldConfigHash === $newConfigHash) {
|
||||
return false;
|
||||
} else {
|
||||
if ($save) {
|
||||
$this->config_hash = $newConfigHash;
|
||||
$this->save();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
return md5($newConfigHash);
|
||||
}
|
||||
|
||||
public function customRepository()
|
||||
|
||||
@@ -17,6 +17,9 @@ use OpenApi\Attributes as OA;
|
||||
'deployment_uuid' => ['type' => 'string'],
|
||||
'pull_request_id' => ['type' => 'integer'],
|
||||
'docker_registry_image_tag' => ['type' => 'string', 'nullable' => true],
|
||||
'configuration_hash' => ['type' => 'string', 'nullable' => true],
|
||||
'configuration_snapshot' => ['type' => 'object', 'nullable' => true],
|
||||
'configuration_diff' => ['type' => 'object', 'nullable' => true],
|
||||
'force_rebuild' => ['type' => 'boolean'],
|
||||
'commit' => ['type' => 'string'],
|
||||
'status' => ['type' => 'string'],
|
||||
@@ -45,6 +48,9 @@ class ApplicationDeploymentQueue extends Model
|
||||
'deployment_uuid',
|
||||
'pull_request_id',
|
||||
'docker_registry_image_tag',
|
||||
'configuration_hash',
|
||||
'configuration_snapshot',
|
||||
'configuration_diff',
|
||||
'force_rebuild',
|
||||
'commit',
|
||||
'status',
|
||||
@@ -71,6 +77,8 @@ class ApplicationDeploymentQueue extends Model
|
||||
protected $casts = [
|
||||
'pull_request_id' => 'integer',
|
||||
'finished_at' => 'datetime',
|
||||
'configuration_snapshot' => 'array',
|
||||
'configuration_diff' => 'array',
|
||||
];
|
||||
|
||||
public function application()
|
||||
|
||||
Reference in New Issue
Block a user