Skip to content

Commit 34d45f7

Browse files
committed
Display app version in the footer using the git tag
- Added a new Terraform variable `version` and passed it to the Rails app via task_envs to support version number display in deployments. - Updated the GitHub Actions `deploy-infrastructure.yml` workflow to dynamically extract the latest Git tag before running `terraform plan`. If no tag is found, the version defaults to "unknown" with a warning. - Modified the `bin/setup` script to detect the latest Git tag locally and set `APP_VERSION` dynamically for development. - Updated controller logic to only display version if it matches the `vX.Y.Z.x` format. - Added logic to conditionally render the version in the footer only when valid.
1 parent 9cd6f8c commit 34d45f7

7 files changed

Lines changed: 45 additions & 1 deletion

File tree

.github/workflows/deploy-infrastructure.yml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ run-name: Deploy infrastructure for ${{ inputs.environment }}
44
on:
55
workflow_dispatch:
66
inputs:
7+
git_ref_to_deploy:
8+
description: "Git ref string for displaying app version (e.g., tag, branch or sha)"
9+
required: false
10+
type: string
711
environment:
812
description: Deployment environment
913
required: true
@@ -22,6 +26,9 @@ on:
2226
type: string
2327
workflow_call:
2428
inputs:
29+
git_ref_to_deploy:
30+
required: false
31+
type: string
2532
environment:
2633
description: Deployment environment
2734
required: true
@@ -87,12 +94,16 @@ jobs:
8794
run: sudo snap install --classic aws-cli
8895
- name: Check if any deployments are running
8996
run: ../scripts/check-for-running-deployments.sh ${{ inputs.environment }}
97+
- name: Set Mavis app version
98+
run: |
99+
APP_VERSION="${{ inputs.git_ref_to_deploy }}"
100+
echo "APP_VERSION=$APP_VERSION" >> $GITHUB_ENV
90101
- name: Terraform Plan
91102
id: plan
92103
run: |
93104
set -e
94105
terraform init -backend-config="env/${{ inputs.environment }}-backend.hcl" -upgrade
95-
terraform plan -var="image_digest=$DIGEST" -var-file="env/${{ inputs.environment }}.tfvars" \
106+
terraform plan -var="image_digest=$DIGEST" -var="app_version=$APP_VERSION" -var-file="env/${{ inputs.environment }}.tfvars" \
96107
-out ${{ runner.temp }}/tfplan | tee ${{ runner.temp }}/tf_stdout
97108
- name: Validate the changes
98109
run: |

.github/workflows/deploy.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ jobs:
8585
with:
8686
environment: ${{ inputs.environment }}
8787
image_tag: ${{ needs.determine-git-sha.outputs.git-sha }}
88+
git_ref_to_deploy: ${{ inputs.git_ref_to_deploy }}
8889
deploy-application:
8990
permissions:
9091
id-token: write

app/controllers/application_controller.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class ApplicationController < ActionController::Base
1515
before_action :set_show_navigation
1616
before_action :set_privacy_policy_url
1717
before_action :authenticate_basic
18+
before_action :set_app_version
1819

1920
after_action :verify_policy_scoped, if: -> { Rails.env.local? }
2021

@@ -32,6 +33,10 @@ class UnprocessableEntity < StandardError
3233
rescue_from Pundit::NotAuthorizedError, with: :user_not_authorized
3334

3435
private
36+
37+
def set_app_version
38+
@app_version = APP_VERSION.match?(/\Av\d+(?:\.\d+)*/) ? APP_VERSION : nil
39+
end
3540

3641
def set_selected_organisation
3742
return if Settings.cis2.enabled

app/views/layouts/application.html.erb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@
6060
<%= link_to "#{t("service.guide.title")} (opens in a new tab)", @service_guide_url, classes: "nhsuk-footer__list-item-link", target: "_blank" %>
6161
</li>
6262
<% end %>
63+
<% if @app_version %>
64+
<li class="nhsuk-footer__list-item nhsuk-footer-default__list-item nhsuk-u-secondary-text-color">
65+
<%= @app_version %>
66+
</li>
67+
<% end %>
6368
</ul>
6469
<p class="nhsuk-footer__copyright">&copy; NHS England</p>
6570
</div>

bin/setup

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,16 @@ FileUtils.chdir APP_ROOT do
2828
puts "\n== Removing old logs and tempfiles =="
2929
system! "bin/rails log:clear tmp:clear"
3030

31+
puts "\n== Detecting app version from git tag =="
32+
33+
git_branch = `git rev-parse --abbrev-ref HEAD 2>/dev/null`.strip
34+
if git_branch == "HEAD"
35+
puts "Warning: Not on a branch. APP_VERSION not set."
36+
else
37+
ENV["APP_VERSION"] = git_branch unless git_branch.empty?
38+
puts "Detected APP_VERSION: #{ENV["APP_VERSION"]}"
39+
end
40+
3141
unless ARGV.include?("--skip-server")
3242
puts "\n== Starting development server =="
3343
STDOUT.flush # flush the output before exec(2) so that it displays

config/initializers/version.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
APP_VERSION = ENV.fetch('APP_VERSION', 'unknown')

terraform/app/variables.tf

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,12 @@ variable "enable_splunk" {
157157
nullable = false
158158
}
159159

160+
variable "app_version" {
161+
type = string
162+
default = "n/a"
163+
description = "The version of Mavis being deployed."
164+
}
165+
160166
locals {
161167
is_production = var.environment == "production"
162168
parameter_store_variables = tomap({
@@ -202,8 +208,13 @@ locals {
202208
{
203209
name = "MAVIS__SPLUNK__ENABLED"
204210
value = var.enable_splunk ? "true" : "false"
211+
},
212+
{
213+
name = "APP_VERSION"
214+
value = var.app_version
205215
}
206216
]
217+
207218
task_secrets = concat([
208219
{
209220
name = var.db_secret_arn == null ? "DB_CREDENTIALS" : "DB_SECRET"

0 commit comments

Comments
 (0)