add BUILD_METADATA fallback when parsing (#2503)

### 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 https://github.com/plausible/analytics/issues/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
pull/2504/head
ruslandoga 4 years ago committed by GitHub
parent f9ab2650b3
commit 138e7c06d6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,5 +1,6 @@
import Config
import Plausible.ConfigHelpers
require Logger
if config_env() in [:dev, :test] do
Envy.load(["config/.env.#{config_env()}"])
@ -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"]),

Loading…
Cancel
Save