Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cached deployment of Docker using github actions #2746

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from

Conversation

kshitijk4poor
Copy link

@kshitijk4poor kshitijk4poor commented Jan 17, 2025

This PR implements Docker build caching in our GitHub Actions workflow, resulting in significant performance improvements for the CI/CD pipeline.

Proposed Changes

  • Removed the local filesystem caching in favor of registry-based caching
  • Added registry-based cache configuration:
    • Cache is now pulled from both the buildcache and latest tags
    • Cache is pushed to a dedicated buildcache tag
  • Added BuildKit configuration to ensure latest buildkit image is used

Associated Issue

Merge Checklist

  • Tests added/fixed
  • Update docs in /docs
  • Linting Complete

Performance Improvements

  • Initial build time: 2m 31s
  • Cached build time: 45s
  • Total reduction: ~70% faster builds

can be confirmed here, . is the initial build and test-1 is the cached build.

Verification

Added Docker layer caching which can be verified by the CACHED flags in the build output. Build logs clearly show the cache being utilized across different stages (marked as CACHED in the build output), confirming proper cache implementation and hit rates.
Screenshot 2025-01-17 at 5 16 48 PM

Only PR's with test cases included and passing lint and test pipelines will be reviewed

@ohcnetwork/care-backend-maintainers @ohcnetwork/care-backend-admins

Summary by CodeRabbit

Summary by CodeRabbit

  • Chores
    • Updated deployment workflow configuration to allow manual execution.
    • Improved Docker build caching strategy using GitHub Container Registry.
    • Optimized build process with a revised remote caching mechanism.

@kshitijk4poor kshitijk4poor requested a review from a team as a code owner January 17, 2025 11:55
Copy link

coderabbitai bot commented Jan 17, 2025

📝 Walkthrough

Walkthrough

The pull request modifies the GitHub Actions deployment workflow configuration, introducing a manual trigger mechanism via workflow_dispatch. The deployment process has been refined with updated Docker image caching strategies, specifically leveraging the GitHub Container Registry for more efficient build caching. The changes streamline the build process by removing local layer caching and implementing registry-based caching with enhanced BuildKit configuration. It’s almost as if someone decided to tidy up the messy corners of the workflow.

Changes

File Change Summary
.github/workflows/deploy.yml - Added workflow_dispatch manual trigger
- Updated Docker caching to use GitHub Container Registry
- Modified BuildX setup with latest BuildKit image
- Removed local Docker layer caching steps

Suggested Reviewers

  • vigneshhari

Possibly related PRs

  • updated deploy workflow #2740: The changes in this PR also involve modifications to the .github/workflows/deploy.yml file, specifically related to the deployment workflow, which may impact the caching and task definition handling.
  • Remove Docker Hub image tags from deploy action #2744: This PR modifies the .github/workflows/deploy.yml file by removing Docker Hub image tags, which could relate to the overall image management and caching strategy in the main PR.
  • fixed deploy.yml #2749: This PR updates the .github/workflows/deploy.yml file with changes to environment variables and task definitions, which may interact with the caching and deployment processes described in the main PR.

Poem

🚀 Workflows dance, cache takes flight
GitHub Actions, oh so bright
Buildx whispers, registry's embrace
Deployment's magic, with elegant grace
Manual triggers, a developer's delight! 🔧


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (2)
.github/workflows/deploy.yml (2)

3-3: Remove this delightful little comment.

I'm sure we all appreciate the documentation, but "Test change to trigger workflow" seems a bit... unnecessary now that we have the actual changes in place.

-# Test change to trigger workflow

91-91: Consider adding cleanup steps.

While I'm sure we're all enjoying the accumulation of Docker caches in our registry, maybe we could add some housekeeping? Consider adding cleanup steps to remove old cache entries periodically.

Here's a suggested cleanup step to add after the build:

      - name: Cleanup old caches
        if: github.ref == 'refs/heads/develop'
        run: |
          REPOSITORY="${{ github.repository }}"
          # Keep only the last 5 buildcache tags
          gh api -X GET /user/packages/container/care/versions --jq '.[] | select(.metadata.container.tags[] | contains("buildcache"))' | \
          jq -r '.id' | sort -r | tail -n +6 | \
          xargs -I {} gh api -X DELETE /user/packages/container/care/versions/{}
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 9994737 and b4d5662.

📒 Files selected for processing (1)
  • .github/workflows/deploy.yml (3 hunks)
🔇 Additional comments (1)
.github/workflows/deploy.yml (1)

88-91: Verify cache effectiveness with metrics.

The cache configuration looks promising, but it would be absolutely wonderful if we could verify the cache hit rates. Could you share some metrics from recent builds showing the improvement from ~2.5 minutes to 45 seconds?

Let's check the recent workflow runs for build times:

✅ Verification successful

Cache configuration is technically perfect, but...

The cache setup follows all the best practices for optimal performance:

  • Registry-based caching (faster than local)
  • Multiple cache sources with both buildcache and latest tags
  • Maximum layer caching with mode=max

While I'd love to see those promised build time improvements, the configuration itself couldn't be better. Just keep an eye on the actual build times to ensure they meet expectations.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Get build durations from recent workflow runs
gh api -X GET /repos/$GITHUB_REPOSITORY/actions/workflows/deploy.yml/runs \
  --jq '.workflow_runs[0:5] | .[] | select(.conclusion=="success") | {run_number: .run_number, created_at: .created_at, updated_at: .updated_at}'

Length of output: 315

.github/workflows/deploy.yml Show resolved Hide resolved
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
.github/workflows/deploy.yml (1)

88-91: Perhaps we could be a bit more... selective with our caching?

While the registry caching implementation is quite nice (and that 70% speed improvement is impressive), using mode=max might be a tad... excessive. It stores absolutely everything, which could lead to larger storage costs and slower push times.

Consider using mode=min to store only the minimum necessary layers:

           cache-from: |
             type=registry,ref=ghcr.io/${{ github.repository }}:buildcache
             type=registry,ref=ghcr.io/${{ github.repository }}:latest
-          cache-to: type=registry,ref=ghcr.io/${{ github.repository }}:buildcache,mode=max
+          cache-to: type=registry,ref=ghcr.io/${{ github.repository }}:buildcache,mode=min

But hey, if storage costs aren't a concern, feel free to ignore this suggestion. It's your cache party! 🎉

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between b4d5662 and 97e7030.

📒 Files selected for processing (1)
  • .github/workflows/deploy.yml (3 hunks)
🔇 Additional comments (2)
.github/workflows/deploy.yml (2)

Line range hint 4-11: LGTM! Nice touch with the manual trigger.

The addition of workflow_dispatch while preserving existing automation shows someone's been thinking ahead. How thoughtful.


65-67: Oh, we're still living dangerously with 'latest', aren't we?

Instead of the adventurous latest tag, perhaps we could use the stable version v0.18.2? Just a thought.

         with:
           driver-opts: |
-            image=moby/buildkit:latest
+            image=moby/buildkit:v0.18.2

@sainak
Copy link
Member

sainak commented Jan 21, 2025

Hi Kshitij, I went through your changes, I don't think this will make a difference because we are missing cache due to some different reason, and there is a limit on how much data we can push to the registry so using it as a cache will exhaust it quickly, instead of this can you try https://docs.docker.com/build/ci/github-actions/multi-platform/#distribute-build-across-multiple-runners and see if this improves the caching behavior

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (2)
.github/workflows/deploy.yml (2)

90-92: Consider optimizing cache sources.

While using both buildcache and latest tags for cache sources is clever, the latest tag might be a bit... unpredictable. Consider using only the buildcache tag for more stable and predictable builds.

cache-from: |
  type=registry,ref=ghcr.io/${{ github.repository }}:buildcache

93-93: Watch out for registry space consumption.

Using mode=max with registry caching is like trying to fit your entire wardrobe into a carry-on bag - it might work initially, but you'll hit limits soon. Consider:

  1. Implementing a cache cleanup strategy
  2. Using mode=min to store only explicitly cached layers
cache-to: type=registry,ref=ghcr.io/${{ github.repository }}:buildcache,mode=min

Would you like me to help create a workflow for periodic cache cleanup?

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 97e7030 and b2db2af.

📒 Files selected for processing (1)
  • .github/workflows/deploy.yml (3 hunks)
🔇 Additional comments (2)
.github/workflows/deploy.yml (2)

4-4: Nice addition of manual trigger!

The workflow_dispatch trigger will be quite handy for those... special moments when we need manual deployments.


67-69: Oh, we're still living dangerously with 'latest', I see...

Using moby/buildkit:latest in CI/CD is like playing Russian roulette with our builds. Perhaps we could be a bit more... conservative?

with:
  driver-opts: |
    image=moby/buildkit:v0.18.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants