feat(release): Move Docker image to ECR and build it on EC2 - #141
Conversation
📝 WalkthroughWalkthroughProduction and staging workflows now build and push backend images to Amazon ECR. EC2 deployments pull those images through Docker Compose. Production triggers accept version-like tags, and SSM output retrieval is centralized across terminal states. CI and Guardrails installation now disable NLTK import security checks and retry validator installation. ChangesECR deployment flow
Guardrails installation setup
Estimated code review effort: 3 (Moderate) | ~25 minutes Sequence Diagram(s)sequenceDiagram
participant GitHubActions
participant ECR
participant SSM
participant EC2
GitHubActions->>ECR: Build and push backend image
GitHubActions->>SSM: Send deployment command
SSM->>EC2: Authenticate and pull backend image
EC2->>EC2: Run prestart profile and start Compose stack
GitHubActions->>SSM: Poll status and retrieve output
Possibly related PRs
Suggested reviewers: 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 3
🤖 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.
Inline comments:
In @.github/workflows/cd-production.yml:
- Around line 22-25: Disable persisted checkout credentials in both deploy
workflows: add persist-credentials: false to the existing with block for
actions/checkout in .github/workflows/cd-production.yml at lines 22-25, and add
a with block containing persist-credentials: false to the checkout step in
.github/workflows/cd-staging.yml at lines 21-22.
In @.github/workflows/cd-staging.yml:
- Around line 34-49: Update the “Build and Push Docker Image” and “Trigger
deploy on EC2 via SSM” steps to use github.sha as the Docker image tag instead
of latest. Ensure the image is both pushed and referenced through IMAGE_TAG
using that same commit SHA.
- Line 51: Update the DEPLOY_CMD deployment sequence to reset the working tree
to origin/main after git fetch origin, instead of the potentially stale local
main branch; preserve the existing hard reset and subsequent deployment
commands.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro Plus
Run ID: 39ba7ab5-0fa8-4f79-8299-04dfb213d0d0
📒 Files selected for processing (3)
.github/workflows/cd-production.yml.github/workflows/cd-staging.ymldocker-compose.yml
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
.github/workflows/cd-production.yml (1)
43-44: 🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick winAlign both standalone Docker builds with the Compose build secrets.
backend/DockerfilereadsHF_TOKENandGUARDRAILS_HUB_API_KEYvia BuildKit secret mounts, but both workflows build./backendwithout passing those secrets and neither workflow exposes them. Pass equivalent BuildKit secrets in production and staging, or validate that their absence is intentional.🤖 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/cd-production.yml around lines 43 - 44, Update the standalone Docker build commands in .github/workflows/cd-production.yml lines 43-44 and .github/workflows/cd-staging.yml lines 39-40 to pass BuildKit secrets for HF_TOKEN and GUARDRAILS_HUB_API_KEY, matching the Compose build configuration; if these secrets are intentionally unavailable, explicitly validate and document that absence instead.
🤖 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/cd-production.yml:
- Around line 43-44: Update the standalone Docker build commands in
.github/workflows/cd-production.yml lines 43-44 and
.github/workflows/cd-staging.yml lines 39-40 to pass BuildKit secrets for
HF_TOKEN and GUARDRAILS_HUB_API_KEY, matching the Compose build configuration;
if these secrets are intentionally unavailable, explicitly validate and document
that absence instead.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro Plus
Run ID: 12f583ed-45ff-4aae-adc3-e6cc9c9cc392
📒 Files selected for processing (2)
.github/workflows/cd-production.yml.github/workflows/cd-staging.yml
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 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.
Inline comments:
In @.github/workflows/continuous-integration.yml:
- Around line 12-13: Remove the global NLTK_DISABLE_IMPORT_SECURITY environment
setting from .github/workflows/continuous-integration.yml lines 12-13,
backend/Dockerfile lines 50-51, and
backend/scripts/install_guardrails_from_hub.sh lines 11-12; no direct changes
are needed elsewhere, and retain security controls unless a specific supported
NLTK call requires a scoped override.
In `@backend/scripts/install_guardrails_from_hub.sh`:
- Around line 42-57: Update the configuration flow around the Guardrails
installation script so GUARDRAILS_HUB_API_KEY is never persisted in the image
layer or `${HOME}/.guardrailsrc`; use a temporary HOME or tmpfs for build-time
setup, or defer configuration creation until container startup using a runtime
secret. Preserve the existing metrics, remote-inferencing, and anonymous-ID
settings while ensuring any build-time secret-backed files are removed before
the step completes.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro Plus
Run ID: c9c13111-9963-43d6-9b07-c759b86c7760
📒 Files selected for processing (5)
.github/workflows/cd-production.yml.github/workflows/cd-staging.yml.github/workflows/continuous-integration.ymlbackend/Dockerfilebackend/scripts/install_guardrails_from_hub.sh
🚧 Files skipped from review as they are similar to previous changes (2)
- .github/workflows/cd-staging.yml
- .github/workflows/cd-production.yml
Issue:
Closes ProjectTech4DevAI/kaapi-backend#1056
Summary
1.
guardrails configurecrashed on a Hub blipguardrails configurewrites~/.guardrailsrcand then calls the Hub/authendpoint only to validate the token. The Hub sits behind CloudFront and intermittently returns a non-JSON body; the CLI turns that into a hardsys.exit(1), which kills the step underset -euo pipefail.(
configureis actually written to tolerate auth failures, butfetch()exits the process before that handler can run.)Fix: write
~/.guardrailsrcdirectly instead of shelling out toguardrails configure— same file, same contents, minus a network round-trip we don't need. Verified the file round-trips through Guardrails' ownRC.load(). Also added retry-with-backoff aroundguardrails hub installso a genuine Hub hiccup doesn't fail the build.2.
nsfw_textpost-install blocked by nltk 3.10.1nltk3.10.1 (new release, pulled in unpinned by thensfw_textvalidator) added an import hook that blocks imports resolving under the current working directory, as anti-hijacking protection. Our venv is atbackend/.venv—inside the working directory — so every dependency nltk imports (
regexjoblib, …) trips the check and the post-install script dies.Checklist
Before submitting a pull request, please ensure that you mark these task.
fastapi run --reload app/main.pyordocker compose upin the repository root and test.