Skip to content

Commit

Permalink
Merge pull request #2320 from alphagov/fail-fast-if-govuk-environment…
Browse files Browse the repository at this point in the history
…-name-is-missing-in-production-env

Fail fast if GOVUK_ENVIRONMENT_NAME env var is missing in production env
  • Loading branch information
floehopper authored Aug 23, 2023
2 parents 746b473 + ee23540 commit cac465c
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 10 deletions.
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ ARG builder_image=ghcr.io/alphagov/govuk-ruby-builder:$ruby_version
FROM $builder_image AS builder

ENV DEVISE_PEPPER=unused \
DEVISE_SECRET_KEY=unused
DEVISE_SECRET_KEY=unused \
GOVUK_ENVIRONMENT_NAME=unused

WORKDIR $APP_HOME
COPY Gemfile* .ruby-version ./
Expand Down
2 changes: 1 addition & 1 deletion app/models/govuk_environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ def self.name
if Rails.env.development? || Rails.env.test?
"development"
else
ENV["GOVUK_ENVIRONMENT_NAME"]
ENV.fetch("GOVUK_ENVIRONMENT_NAME")
end
end

Expand Down
2 changes: 1 addition & 1 deletion config/initializers/govuk_admin_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
c.disable_google_analytics = false
end

GovukAdminTemplate.environment_label = (GovukEnvironment.name || "development").titleize
GovukAdminTemplate.environment_label = GovukEnvironment.name.titleize
GovukAdminTemplate.environment_style = GovukEnvironment.production? ? "production" : "preview"
4 changes: 0 additions & 4 deletions docs/environment-variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,6 @@ Used to configure `GovukAdminTemplate` and in `Healthcheck::ApiTokens#expiring_t

* `GOVUK_ENVIRONMENT_NAME`

Used in various bits of logic to detect the production instance of the app and used to display various environment-specific strings.

* `INSTANCE_NAME`

## Splunk

Used to stream event logs to Splunk for analysis of Signon usage patterns and anomalies.
Expand Down
13 changes: 10 additions & 3 deletions test/models/govuk_environment_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,18 @@ class GovukEnvironmentTest < ActionMailer::TestCase
setup do
Rails.env.stubs(:development?).returns(false)
Rails.env.stubs(:test?).returns(false)
ENV.stubs(:[]).with("GOVUK_ENVIRONMENT_NAME").returns("govuk-environment-name")
end

should "return value of GOVUK_ENVIRONMENT_NAME env var" do
assert_equal "govuk-environment-name", GovukEnvironment.name
should "return value of GOVUK_ENVIRONMENT_NAME if it is set" do
ClimateControl.modify(GOVUK_ENVIRONMENT_NAME: "govuk-environment-name") do
assert_equal "govuk-environment-name", GovukEnvironment.name
end
end

should "fail fast if GOVUK_ENVIRONMENT_NAME is not set" do
ClimateControl.modify(GOVUK_ENVIRONMENT_NAME: nil) do
assert_raises(KeyError) { GovukEnvironment.name }
end
end
end
end
Expand Down

0 comments on commit cac465c

Please sign in to comment.