Skip to content

Commit

Permalink
add BUILD_METADATA fallback when parsing (#2503)
Browse files Browse the repository at this point in the history
### Changes

This PR adds a fallback to empty build metadata when BUILD_METADATA
contains invalid JSON.

Example `warning` log for `BUILD_METADATA={...}`:

```
20:57:57.872 [warning] failed to parse $BUILD_METADATA, reason: ** (Jason.DecodeError) unexpected byte at position 1: 0x2E (".")
```

Fixes #2491

### Tests
- [x] This PR does not require tests

### Changelog
- [ ] Entry has been added to changelog

### Documentation
- [x] This change does not need a documentation update

### Dark mode
- [x] This PR does not change the UI
  • Loading branch information
ruslandoga authored Dec 5, 2022
1 parent f9ab265 commit 138e7c0
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions config/runtime.exs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Config
import Plausible.ConfigHelpers
require Logger

if config_env() in [:dev, :test] do
Envy.load(["config/.env.#{config_env()}"])
Expand Down Expand Up @@ -92,10 +93,26 @@ ch_db_url =

### Mandatory params End

build_metadata_raw = get_var_from_path_or_env(config_dir, "BUILD_METADATA", "{}")

build_metadata =
config_dir
|> get_var_from_path_or_env("BUILD_METADATA", "{}")
|> Jason.decode!()
case Jason.decode(build_metadata_raw) do
{:ok, build_metadata} ->
build_metadata

{:error, error} ->
error = Exception.format(:error, error)

Logger.warn("""
failed to parse $BUILD_METADATA: #{error}
$BUILD_METADATA is set to #{build_metadata_raw}\
""")

Logger.warn("falling back to empty build metadata, as if $BUILD_METADATA was set to {}")

_fallback = %{}
end

runtime_metadata = [
version: get_in(build_metadata, ["labels", "org.opencontainers.image.version"]),
Expand Down

0 comments on commit 138e7c0

Please sign in to comment.