Skip to content

Pull Key Vault secrets into vertex .env - #3588

Closed
Codebmk wants to merge 2 commits into
stagingfrom
ft-vertex-template
Closed

Pull Key Vault secrets into vertex .env#3588
Codebmk wants to merge 2 commits into
stagingfrom
ft-vertex-template

Conversation

@Codebmk

@Codebmk Codebmk commented Jun 7, 2026

Copy link
Copy Markdown
Member

🐛 Description

This PR resolves a critical "split-brain" environment issue affecting the Vertex application on Staging and Production.

The Bug: When a user attempted to use Social Login (e.g., Google) on the deployed environments, the browser would incorrectly attempt to redirect them to http://localhost:3000/... instead of the correct staging-vertex/vertex API URL. Email login, however, continued to work.

🔍 Root Cause Analysis

Next.js requires client-side environment variables (prefixed with NEXT_PUBLIC_) to be present during the build process so they can be statically baked into the browser's JavaScript bundle.

Upon auditing our Azure CI/CD workflows, we discovered a missing step in the vertex Docker build job. While other services (like analytics-platform) correctly pulled their secrets from Azure Key Vault to generate a .env file before docker build, the vertex block completely skipped this step.

Because the Docker build environment lacked these secrets, Next.js fell back to its default development variables, permanently hardcoding the localhost:3000 API fallback into the frontend code. (Email login succeeded because NextAuth runs dynamically on the server at runtime, where variables were properly injected).

🛠️ Solution

Added the missing Azure Key Vault secret extraction step to the vertex deployment jobs in both Staging and Production Azure workflows.

Files Modified:

  • .github/workflows/deploy-frontend-to-azurestaging.yml
  • .github/workflows/deploy-frontend-to-azureprod.yml

Changes Made:
Inserted the following step immediately before docker build to ensure the container has access to the .env file during the Next.js compilation phase:

- name: Pull secrets from Key Vault
  run: |
    az keyvault secret show \
      --vault-name ${{ env.KEYVAULT }} \
      --name sta-env-vertex \ # (and prod-env-vertex)
      --query value -o tsv > secrets.json

    jq -r 'to_entries | .[] | "$.key)=$.value|tojson)"' secrets.json > src/vertex/.env

<!-- This is an auto-generated comment: release notes by coderabbit.ai -->

## Summary by CodeRabbit

* **Chores**
  * Optimized CI/CD deployment workflows for staging and production environments to improve secret handling efficiency and streamline the build process configuration.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->

Add a "Pull secrets from Key Vault" step to the prod and staging frontend deployment workflows. Each step uses az keyvault secret show to retrieve the environment JSON (prod-env-vertex / sta-env-vertex), writes it to secrets.json, and converts it with jq into KEY=VALUE lines at src/vertex/.env before building and pushing the Docker image. This injects the Vertex service environment variables at build time using the repository's KEYVAULT and az/jq tooling.
@Codebmk
Codebmk requested review from Baalmart and Psalmz777 June 7, 2026 18:06
@Codebmk Codebmk self-assigned this Jun 7, 2026
@coderabbitai

coderabbitai Bot commented Jun 7, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

Both production and staging deployment workflows standardize Key Vault secret materialization. Analytics-platform now pipes secrets directly to .env instead of using intermediate files. Vertex services gain new secret retrieval steps that fetch and write environment variables to .env files, eliminating manual secret management from the container build process.

Changes

Key Vault secret materialization workflow

Layer / File(s) Summary
Analytics-platform secret retrieval
.github/workflows/deploy-frontend-to-azureprod.yml, .github/workflows/deploy-frontend-to-azurestaging.yml
Key Vault secret fetch for analytics-platform is streamlined to pipe the JSON value directly through jq and write environment variables to src/platform/.env, removing the intermediate secrets.json file approach in both production and staging workflows.
Vertex secret retrieval
.github/workflows/deploy-frontend-to-azureprod.yml, .github/workflows/deploy-frontend-to-azurestaging.yml
New steps added to both workflows to pull vertex-specific secrets (prod-env-vertex and sta-env-vertex) from Key Vault, transform them with jq, and write the resulting key/value pairs directly to src/vertex/.env.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

  • airqo-platform/AirQo-frontend#3586: Vertex job now generates src/vertex/.env via Key Vault, which supplies the NEXT_PUBLIC_API_URL configuration consumed by the updated getApiBaseUrl and getDefaultApiUrl logic in that PR.

Suggested reviewers

  • Baalmart

Poem

🔐 Secrets now flow direct to .env files,
No intermediate JSON in the pile,
Production and staging, both aligned,
Key Vault pipelines refined,
Docker builds with secrets well-designed. 🚀

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The PR title directly addresses the main change: adding Key Vault secret extraction for the Vertex service's .env file in CI workflows.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch ft-vertex-template

Warning

Review ran into problems

🔥 Problems

Errors were encountered while retrieving linked issues.

Errors (1)
  • JIRA integration encountered authorization issues. Please disconnect and reconnect the integration in the CodeRabbit UI.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates the Azure staging and production deployment workflows for the Vertex Next.js app to ensure client-side NEXT_PUBLIC_* environment variables are available at Docker/Next.js build time, preventing baked-in localhost:3000 redirects in deployed environments.

Changes:

  • Add an Azure Key Vault secret extraction step in the Vertex build job for staging.
  • Add an Azure Key Vault secret extraction step in the Vertex build job for production.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
.github/workflows/deploy-frontend-to-azurestaging.yml Adds a Key Vault pull step before building the Vertex Docker image on staging.
.github/workflows/deploy-frontend-to-azureprod.yml Adds a Key Vault pull step before building the Vertex Docker image on production.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +283 to +290
- name: Pull secrets from Key Vault
run: |
az keyvault secret show \
--vault-name ${{ env.KEYVAULT }} \
--name sta-env-vertex \
--query value -o tsv > secrets.json

jq -r 'to_entries | .[] | "\(.key)=\(.value|tojson)"' secrets.json > src/vertex/.env
Comment on lines +214 to +221
- name: Pull secrets from Key Vault
run: |
az keyvault secret show \
--vault-name ${{ env.KEYVAULT }} \
--name prod-env-vertex \
--query value -o tsv > secrets.json

jq -r 'to_entries | .[] | "\(.key)=\(.value|tojson)"' secrets.json > src/vertex/.env
Stream `az keyvault secret show --query value -o tsv` directly into `jq` instead of writing to a temporary `secrets.json` file in the Azure deploy workflows. Updated both prod and staging workflows for the next-platform and vertex secret steps so they write directly to `src/platform/.env` and `src/vertex/.env`. This simplifies the steps and removes unnecessary file I/O without changing behavior.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (3)
.github/workflows/deploy-frontend-to-azurestaging.yml (2)

236-242: ⚠️ Potential issue | 🟠 Major | ⚡ Quick win

This workflow-only fix will not deploy either staging image on merge.

Lines 236-242 and 282-288 add the missing .env generation, but the check_files gate only enables these jobs for src/platform/*, src/vertex/*, or workflow-trigger. Since this PR only changes .github/workflows/..., merging into staging skips both jobs and leaves the current staging images untouched.

Minimal fix in `check_files`
           while IFS= read -r file
           do
             echo $file
+            if [[ $file == .github/workflows/deploy-frontend-to-azurestaging.yml ]]; then
+              echo "run_analytics_platform=true" >>$GITHUB_OUTPUT
+              echo "run_vertex=true" >>$GITHUB_OUTPUT
+            fi
+
             if [[ $file == src/website/* ]]; then
               echo "run_website=true" >>$GITHUB_OUTPUT
             fi

Also applies to: 282-288

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In @.github/workflows/deploy-frontend-to-azurestaging.yml around lines 236 -
242, The workflow adds .env generation steps ("Pull secrets from Key Vault") but
the existing check_files gating logic (the check_files job/condition) only
allows changes in src/platform/*, src/vertex/*, or workflow-trigger to enable
the staging-image jobs, so merging only workflow changes will skip the jobs and
not deploy updated staging images; fix by updating the check_files patterns to
also include '.github/workflows/**' (or add a workflow-only pattern like
'**/.github/workflows/**' or a dedicated 'workflow-trigger' match) so the jobs
that run the "Pull secrets from Key Vault" steps are triggered on workflow-only
changes.

236-242: ⚠️ Potential issue | 🟠 Major

Harden the az | jq secret-to-.env pipeline (and note the staging gating behavior)

  • Lines 236-242 and 282-288: the workflow’s default shell behavior doesn’t enable pipefail, so a failing az keyvault secret show can still let the step succeed if jq exits 0—potentially writing an empty/partial .env. Add pipefail (e.g., defaults: run: shell: bash or shell: bash) and make jq fail on empty output (e.g., jq -e …) or explicitly validate the generated file is non-empty.
  • Staging rebuild gating: the check job only flips run_analytics_platform for src/platform/* and run_vertex for src/vertex/* (or workflow-trigger), so a PR that changes only this workflow file won’t run these staging rebuild jobs.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In @.github/workflows/deploy-frontend-to-azurestaging.yml around lines 236 -
242, The step that pipes az keyvault secret show into jq can succeed despite az
failing because the default shell lacks pipefail; update the job to use bash
with pipefail (e.g., set the run shell to bash or add set -o pipefail) and make
jq fail on empty input (use the jq -e form or otherwise assert non-empty output)
and/or add an explicit check that src/platform/.env is non-empty after
generation; additionally, adjust the check job gating logic so changes to the
workflow itself trigger the staging rebuild flags (include this workflow file in
the check job path filters or explicitly set run_analytics_platform and
run_vertex when workflow-trigger/this workflow is the changed file) to ensure
staging rebuilds run when only the workflow is modified.
.github/workflows/deploy-frontend-to-azureprod.yml (1)

168-174: ⚠️ Potential issue | 🟠 Major

Fail fast if Key Vault returns no/failed secret (both analytics-platform + vertex).

The workflow doesn’t set shell: on these run steps, so they run with bash -e {0} (no pipefail). If az keyvault secret show ... fails or returns nothing, the pipeline can still succeed because jq -r exits 0 on empty input—leaving an empty .env that Docker builds may proceed with.

Suggested hardening
       - name: Pull secrets from Key Vault
+        shell: bash
         run: |
+          set -euo pipefail
           az keyvault secret show \
             --vault-name ${{ env.KEYVAULT }} \
             --name prod-env-next-platform \
             --query value -o tsv | \
-          jq -r 'to_entries | .[] | "\(.key)=\(.value|tojson)"' > src/platform/.env        
+          jq -er 'to_entries | .[] | "\(.key)=\(.value|tojson)"' > src/platform/.env
+          test -s src/platform/.env

Apply the same changes to the vertex step (prod-env-vertex / src/vertex/.env) as well.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In @.github/workflows/deploy-frontend-to-azureprod.yml around lines 168 - 174,
The run step that pulls Key Vault secrets for prod-env-next-platform (writing to
src/platform/.env) can silently succeed with an empty file because jq exits 0 on
empty input and the step runs without pipefail; update the two secret steps
(prod-env-next-platform and prod-env-vertex which writes src/vertex/.env) to
fail fast by enabling pipefail (set shell to bash -eo pipefail or add set -o
pipefail at the top of the run block) and validate the az output before piping
to jq (e.g., capture az output, check it's non-empty / az succeeded, and exit
non-zero if empty) so the step errors when Key Vault returns nothing or az
fails.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Outside diff comments:
In @.github/workflows/deploy-frontend-to-azureprod.yml:
- Around line 168-174: The run step that pulls Key Vault secrets for
prod-env-next-platform (writing to src/platform/.env) can silently succeed with
an empty file because jq exits 0 on empty input and the step runs without
pipefail; update the two secret steps (prod-env-next-platform and
prod-env-vertex which writes src/vertex/.env) to fail fast by enabling pipefail
(set shell to bash -eo pipefail or add set -o pipefail at the top of the run
block) and validate the az output before piping to jq (e.g., capture az output,
check it's non-empty / az succeeded, and exit non-zero if empty) so the step
errors when Key Vault returns nothing or az fails.

In @.github/workflows/deploy-frontend-to-azurestaging.yml:
- Around line 236-242: The workflow adds .env generation steps ("Pull secrets
from Key Vault") but the existing check_files gating logic (the check_files
job/condition) only allows changes in src/platform/*, src/vertex/*, or
workflow-trigger to enable the staging-image jobs, so merging only workflow
changes will skip the jobs and not deploy updated staging images; fix by
updating the check_files patterns to also include '.github/workflows/**' (or add
a workflow-only pattern like '**/.github/workflows/**' or a dedicated
'workflow-trigger' match) so the jobs that run the "Pull secrets from Key Vault"
steps are triggered on workflow-only changes.
- Around line 236-242: The step that pipes az keyvault secret show into jq can
succeed despite az failing because the default shell lacks pipefail; update the
job to use bash with pipefail (e.g., set the run shell to bash or add set -o
pipefail) and make jq fail on empty input (use the jq -e form or otherwise
assert non-empty output) and/or add an explicit check that src/platform/.env is
non-empty after generation; additionally, adjust the check job gating logic so
changes to the workflow itself trigger the staging rebuild flags (include this
workflow file in the check job path filters or explicitly set
run_analytics_platform and run_vertex when workflow-trigger/this workflow is the
changed file) to ensure staging rebuilds run when only the workflow is modified.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 5696876f-dcd2-4bf6-8ee7-0d8eb88811f8

📥 Commits

Reviewing files that changed from the base of the PR and between 60920b3 and ee408e8.

📒 Files selected for processing (2)
  • .github/workflows/deploy-frontend-to-azureprod.yml
  • .github/workflows/deploy-frontend-to-azurestaging.yml

@Codebmk Codebmk closed this Jun 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants