mirror of
https://github.com/sanjeed5/awesome-cursor-rules-mdc.git
synced 2026-11-30 23:04:59 +08:00
Regenerate all MDC rules with fresh Exa data and December 2025 best practices
- Refreshed all 241 library rules with current best practices - Updated Exa search results for all libraries with 2025 content - Fixed --regenerate-all flag logic to properly process all libraries - Removed duplicate astro entry in rules.json (merged tags) - Added missing docker.mdc and vim.mdc files - All rules now include modern patterns and anti-patterns - Updated with framework-specific recommendations for 2025 Technical improvements: - Fixed retry_failed_only logic in generate_mdc_files.py - Improved progress tracking for regeneration scenarios - Ensured proper handling of --refresh-exa flag
This commit is contained in:
+287
-229
@@ -1,231 +1,289 @@
|
||||
---
|
||||
description: This rule provides comprehensive best practices for configuring and optimizing CircleCI workflows, covering code organization, security, performance, and testing to ensure efficient and reliable CI/CD pipelines.
|
||||
globs: .circleci/config.yml
|
||||
description: This guide provides opinionated, actionable best practices for writing maintainable, secure, and performant CircleCI configurations and orbs.
|
||||
globs: **/*
|
||||
---
|
||||
# CircleCI Best Practices: A Comprehensive Guide
|
||||
|
||||
This document provides a comprehensive guide to CircleCI best practices, covering various aspects of workflow configuration, optimization, security, and testing.
|
||||
|
||||
## 1. Code Organization and Structure
|
||||
|
||||
While CircleCI primarily focuses on workflow orchestration rather than code structure, adhering to a well-organized project structure significantly benefits CI/CD pipeline maintainability and efficiency. Here are some key recommendations:
|
||||
|
||||
### 1.1 Directory Structure
|
||||
|
||||
* **Root Level:**
|
||||
* `.circleci/`: Contains the `config.yml` file defining the CircleCI workflow.
|
||||
* `scripts/`: (Optional) Houses custom scripts used within the CI/CD pipeline (e.g., deployment scripts, database setup).
|
||||
* `docker/`: (Optional) Contains Dockerfiles and related files for containerizing applications.
|
||||
* `README.md`: Project documentation.
|
||||
* `LICENSE`: Project license.
|
||||
* `.gitignore`: Specifies intentionally untracked files that Git should ignore.
|
||||
* **Application Code:** Follow a consistent structure based on the project's technology stack (e.g., `src/`, `app/`, `lib/`).
|
||||
|
||||
### 1.2 File Naming Conventions
|
||||
|
||||
* `config.yml`: The primary CircleCI configuration file. Use descriptive comments within this file.
|
||||
* Shell Scripts: Use `.sh` extension for shell scripts in the `scripts/` directory (e.g., `deploy.sh`, `setup_database.sh`).
|
||||
* Dockerfiles: Use `Dockerfile` (without extension) or a descriptive name like `Dockerfile.dev` or `Dockerfile.prod`.
|
||||
|
||||
### 1.3 Module Organization
|
||||
|
||||
* For larger projects, break down complex workflows into reusable modules or Orbs. Orbs allow you to encapsulate and share common configuration snippets across multiple projects.
|
||||
* Utilize private Orbs within your organization to share proprietary configurations securely.
|
||||
|
||||
### 1.4 Component Architecture
|
||||
|
||||
* Consider a modular design for your application, making it easier to test and deploy individual components. This approach aligns well with microservices architectures.
|
||||
* Use environment variables to configure different components based on the deployment environment (e.g., development, staging, production).
|
||||
|
||||
### 1.5 Code Splitting
|
||||
|
||||
* For large applications, consider splitting your code into smaller modules or packages to reduce build times and improve parallelism in your CI/CD pipeline.
|
||||
* Use caching strategies to avoid rebuilding unchanged modules.
|
||||
|
||||
## 2. Common Patterns and Anti-patterns
|
||||
|
||||
### 2.1 Design Patterns
|
||||
|
||||
* **Configuration as Code (CaC):** Manage CircleCI configurations (and application configurations) as code, stored in version control. This ensures traceability, auditability, and reproducibility.
|
||||
* **Infrastructure as Code (IaC):** Use tools like Terraform or CloudFormation to provision and manage infrastructure in a consistent and automated way.
|
||||
* **GitOps:** Use Git as the single source of truth for infrastructure and application configurations. Changes are applied through pull requests, ensuring a controlled and auditable process.
|
||||
|
||||
### 2.2 Recommended Approaches
|
||||
|
||||
* **Automated Testing:** Integrate comprehensive automated testing into your pipeline, including unit tests, integration tests, and end-to-end tests. Fail the build if any tests fail.
|
||||
* **Parallel Execution:** Leverage CircleCI's parallelism feature to run tests and other tasks concurrently, significantly reducing pipeline execution time.
|
||||
* **Caching:** Use caching to store dependencies and build artifacts between jobs, avoiding redundant downloads and builds.
|
||||
* **Orbs:** Utilize community or custom Orbs to simplify common tasks and promote code reuse.
|
||||
|
||||
### 2.3 Anti-patterns
|
||||
|
||||
* **Hardcoding Secrets:** Never hardcode sensitive information (API keys, passwords) directly in the `config.yml` file. Use environment variables or a secrets management tool.
|
||||
* **Ignoring Test Failures:** Do not proceed with deployment if tests fail. Fix the underlying issues first.
|
||||
* **Large, Monolithic Configurations:** Avoid creating a single, massive `config.yml` file. Break down the workflow into smaller, reusable jobs and Orbs.
|
||||
* **Lack of Caching:** Failing to utilize caching can significantly increase build times.
|
||||
* **Manual Deployments:** Avoid manual deployments. Automate the entire deployment process.
|
||||
|
||||
### 2.4 State Management
|
||||
|
||||
* CircleCI itself is stateless. For stateful applications, manage state using external databases, caches (e.g., Redis, Memcached), or cloud storage services (e.g., AWS S3, Azure Blob Storage).
|
||||
* Use environment variables to configure the connection to stateful services based on the environment.
|
||||
|
||||
### 2.5 Error Handling
|
||||
|
||||
* Implement robust error handling in your scripts and application code.
|
||||
* Use CircleCI's notification features to alert developers when builds fail or errors occur.
|
||||
* Log detailed error messages to aid in debugging.
|
||||
* Consider using tools like Sentry or Rollbar for centralized error tracking.
|
||||
|
||||
## 3. Performance Considerations
|
||||
|
||||
### 3.1 Optimization Techniques
|
||||
|
||||
* **Parallelism:** As mentioned earlier, parallelize jobs to reduce overall pipeline execution time.
|
||||
* **Caching:** Cache dependencies, build artifacts, and Docker layers.
|
||||
* **Resource Classes:** Use appropriate resource classes for your jobs. Larger resource classes provide more CPU and memory, but also consume more credits.
|
||||
* **Docker Layer Caching:** Optimize Dockerfiles to leverage layer caching effectively. Arrange commands from least to most frequently changed to maximize cache hits.
|
||||
* **Optimize Docker Image Size:** Reduce the size of Docker images by using multi-stage builds and removing unnecessary dependencies.
|
||||
|
||||
### 3.2 Memory Management
|
||||
|
||||
* Monitor memory usage during builds, especially when running resource-intensive tasks (e.g., large test suites).
|
||||
* Adjust resource classes if jobs are running out of memory.
|
||||
* Optimize your application code to reduce memory consumption.
|
||||
|
||||
### 3.3 Bundle Size Optimization (If Applicable)
|
||||
|
||||
* For web applications, optimize JavaScript bundle sizes using techniques like code splitting, tree shaking, and minification.
|
||||
* Use tools like Webpack Bundle Analyzer to identify large dependencies.
|
||||
|
||||
### 3.4 Lazy Loading (If Applicable)
|
||||
|
||||
* For web applications, implement lazy loading to load resources only when they are needed.
|
||||
* Use dynamic imports in JavaScript to load modules on demand.
|
||||
|
||||
## 4. Security Best Practices
|
||||
|
||||
### 4.1 Common Vulnerabilities
|
||||
|
||||
* **Exposed Secrets:** Storing sensitive information in the codebase or configuration files.
|
||||
* **Insecure Dependencies:** Using outdated or vulnerable dependencies.
|
||||
* **Lack of Access Control:** Granting excessive permissions to users or services.
|
||||
* **Man-in-the-Middle Attacks:** Failing to use HTTPS for secure communication.
|
||||
|
||||
### 4.2 Input Validation
|
||||
|
||||
* Validate all input data in your application to prevent injection attacks.
|
||||
* Use parameterized queries to prevent SQL injection.
|
||||
* Escape user-provided data when rendering HTML to prevent cross-site scripting (XSS) attacks.
|
||||
|
||||
### 4.3 Authentication and Authorization
|
||||
|
||||
* Use strong authentication mechanisms to protect your application.
|
||||
* Implement role-based access control (RBAC) to restrict access to sensitive resources.
|
||||
* Use secure tokens (e.g., JWT) for authentication and authorization.
|
||||
|
||||
### 4.4 Data Protection
|
||||
|
||||
* Encrypt sensitive data at rest and in transit.
|
||||
* Use HTTPS for all API communication.
|
||||
* Regularly back up your data.
|
||||
|
||||
### 4.5 Secure API Communication
|
||||
|
||||
* Use API keys or tokens to authenticate API requests.
|
||||
* Implement rate limiting to prevent abuse.
|
||||
* Use HTTPS for all API communication.
|
||||
* Validate API requests and responses.
|
||||
|
||||
## 5. Testing Approaches
|
||||
|
||||
### 5.1 Unit Testing
|
||||
|
||||
* Write unit tests for all critical components of your application.
|
||||
* Use a mocking framework to isolate components during unit testing.
|
||||
* Aim for high test coverage (ideally > 80%).
|
||||
|
||||
### 5.2 Integration Testing
|
||||
|
||||
* Write integration tests to verify that different components of your application work together correctly.
|
||||
* Test the interaction between your application and external services (e.g., databases, APIs).
|
||||
|
||||
### 5.3 End-to-End Testing
|
||||
|
||||
* Write end-to-end tests to simulate user interactions and verify that the entire application works as expected.
|
||||
* Use a browser automation tool like Selenium or Cypress for end-to-end testing.
|
||||
|
||||
### 5.4 Test Organization
|
||||
|
||||
* Organize your tests in a logical directory structure (e.g., `tests/unit/`, `tests/integration/`, `tests/e2e/`).
|
||||
* Use descriptive names for your test files and test cases.
|
||||
|
||||
### 5.5 Mocking and Stubbing
|
||||
|
||||
* Use mocking to replace external dependencies with controlled test doubles.
|
||||
* Use stubbing to provide pre-defined responses for specific function calls or API requests.
|
||||
* Choose a mocking framework that is appropriate for your programming language and testing framework.
|
||||
|
||||
## 6. Common Pitfalls and Gotchas
|
||||
|
||||
### 6.1 Frequent Mistakes
|
||||
|
||||
* **Incorrect YAML Syntax:** YAML is sensitive to indentation. Use a YAML linter to validate your `config.yml` file.
|
||||
* **Misconfigured Caching:** Using incorrect cache keys or not invalidating caches when dependencies change.
|
||||
* **Not Using Environment Variables:** Hardcoding secrets or configuration values instead of using environment variables.
|
||||
* **Ignoring Resource Limits:** Exceeding resource limits (CPU, memory, disk space) can lead to job failures.
|
||||
|
||||
### 6.2 Edge Cases
|
||||
|
||||
* **Network Connectivity Issues:** Handle transient network connectivity issues gracefully in your scripts.
|
||||
* **API Rate Limiting:** Implement retry logic with exponential backoff to handle API rate limiting.
|
||||
* **Concurrency Issues:** Be aware of potential concurrency issues when running parallel jobs.
|
||||
|
||||
### 6.3 Version-Specific Issues
|
||||
|
||||
* Be aware of breaking changes between CircleCI versions. Refer to the CircleCI documentation for upgrade instructions.
|
||||
|
||||
### 6.4 Compatibility Concerns
|
||||
|
||||
* Ensure that your CircleCI configuration is compatible with the versions of the tools and libraries you are using.
|
||||
|
||||
### 6.5 Debugging Strategies
|
||||
|
||||
* Use CircleCI's debugging features to inspect the state of your jobs.
|
||||
* Add logging statements to your scripts to track the execution flow and identify errors.
|
||||
* Use a remote debugger to step through your code interactively.
|
||||
|
||||
## 7. Tooling and Environment
|
||||
|
||||
### 7.1 Recommended Development Tools
|
||||
|
||||
* **IDE:** Visual Studio Code, IntelliJ IDEA, or other IDEs with YAML support.
|
||||
* **YAML Linter:** Use a YAML linter to validate your `config.yml` file.
|
||||
* **Docker:** Docker for local container development and testing.
|
||||
* **CircleCI CLI:** CircleCI command-line interface for interacting with the CircleCI API.
|
||||
|
||||
### 7.2 Build Configuration
|
||||
|
||||
* Use a consistent build process across all environments.
|
||||
* Automate build tasks using build tools like Make, Gradle, or npm.
|
||||
* Configure your build environment using environment variables.
|
||||
|
||||
### 7.3 Linting and Formatting
|
||||
|
||||
* Use a linter to enforce code style and identify potential errors.
|
||||
* Use a code formatter to automatically format your code.
|
||||
* Integrate linting and formatting into your CI/CD pipeline.
|
||||
|
||||
### 7.4 Deployment
|
||||
|
||||
* Automate the deployment process using deployment tools like Ansible, Chef, or Puppet.
|
||||
* Use a blue-green deployment strategy to minimize downtime during deployments.
|
||||
* Monitor your application after deployment to ensure that it is functioning correctly.
|
||||
|
||||
### 7.5 CI/CD Integration
|
||||
|
||||
* Integrate CircleCI with your version control system (e.g., GitHub, Bitbucket).
|
||||
* Configure CircleCI to trigger builds automatically when code is pushed to the repository.
|
||||
* Use CircleCI's notification features to alert developers when builds succeed or fail.
|
||||
|
||||
By following these best practices, you can ensure that your CircleCI workflows are efficient, reliable, and secure. This guide provides a strong foundation for building robust CI/CD pipelines and optimizing your software development process.
|
||||
# circleci Best Practices
|
||||
|
||||
CircleCI is the backbone of our CI/CD. Adhere to these guidelines to ensure our pipelines are efficient, secure, and maintainable.
|
||||
|
||||
## 1. Code Organization & Reusability
|
||||
|
||||
### 1.1. Author and Consume Orbs for Reusability
|
||||
Encapsulate common logic into orbs. This promotes DRY principles and consistent patterns across projects.
|
||||
|
||||
* **Descriptive Naming**: Use clear `namespace/orb-name` slugs.
|
||||
❌ BAD: `myorg/plugin`, `myorg/ci-tool`
|
||||
✅ GOOD: `myorg/docker-publish`, `myorg/aws-deploy`
|
||||
* **Semantic Versioning**: Lock to specific orb versions.
|
||||
❌ BAD: `myorg/aws-deploy@volatile`
|
||||
✅ GOOD: `myorg/aws-deploy@1.2.3`
|
||||
* **Comprehensive Descriptions**: Every orb component (commands, jobs, executors, parameters) *must* have a clear `description`.
|
||||
|
||||
```yaml
|
||||
# .circleci/orb.yml
|
||||
commands:
|
||||
my-command:
|
||||
description: "Utilize this command to echo Hello to a step in the UI."
|
||||
steps:
|
||||
- run: echo "Hello"
|
||||
```
|
||||
|
||||
### 1.2. Reuse Commands, Executors, and Jobs
|
||||
Within your `config.yml` or orbs, define reusable components to avoid duplication.
|
||||
|
||||
❌ BAD:
|
||||
```yaml
|
||||
jobs:
|
||||
build-and-test:
|
||||
docker:
|
||||
- image: cimg/node:18.17.1
|
||||
steps:
|
||||
- checkout
|
||||
- run: npm install
|
||||
- run: npm test
|
||||
another-job:
|
||||
docker:
|
||||
- image: cimg/node:18.17.1
|
||||
steps:
|
||||
- checkout
|
||||
- run: npm install # Duplication
|
||||
- run: npm run build
|
||||
```
|
||||
|
||||
✅ GOOD:
|
||||
```yaml
|
||||
# .circleci/config.yml
|
||||
version: 2.1
|
||||
commands:
|
||||
install_deps:
|
||||
description: "Install Node.js dependencies."
|
||||
steps:
|
||||
- run: npm install
|
||||
|
||||
jobs:
|
||||
build-and-test:
|
||||
docker:
|
||||
- image: cimg/node:18.17.1
|
||||
steps:
|
||||
- checkout
|
||||
- install_deps
|
||||
- run: npm test
|
||||
build-app:
|
||||
docker:
|
||||
- image: cimg/node:18.17.1
|
||||
steps:
|
||||
- checkout
|
||||
- install_deps
|
||||
- run: npm run build
|
||||
```
|
||||
|
||||
### 1.3. Use Pass-Through Parameters in Jobs
|
||||
When a job uses a command or executor that accepts parameters, expose those parameters at the job level for flexibility.
|
||||
|
||||
```yaml
|
||||
# .circleci/orb.yml (partial)
|
||||
executors:
|
||||
default:
|
||||
docker:
|
||||
- image: 'cimg/node:<<parameters.tag>>'
|
||||
parameters:
|
||||
tag:
|
||||
type: string
|
||||
default: '18.17'
|
||||
|
||||
jobs:
|
||||
test:
|
||||
description: "Simple job to test Node.js application."
|
||||
parameters:
|
||||
version: # Pass-through parameter for the executor's tag
|
||||
type: string
|
||||
default: '18.17'
|
||||
executor:
|
||||
name: default
|
||||
tag: << parameters.version >> # Pass parameter to executor
|
||||
steps:
|
||||
- checkout
|
||||
- run: npm test
|
||||
```
|
||||
|
||||
## 2. Security & Configuration Management
|
||||
|
||||
### 2.1. Store Secrets in Contexts
|
||||
**Never hard-code secrets or expose them directly in `config.yml`.** Use CircleCI Contexts for sensitive environment variables.
|
||||
|
||||
❌ BAD:
|
||||
```yaml
|
||||
jobs:
|
||||
deploy:
|
||||
steps:
|
||||
- run: echo "Deploying with API_KEY=$MY_API_KEY" # MY_API_KEY defined directly in config or as unmanaged env var
|
||||
```
|
||||
|
||||
✅ GOOD:
|
||||
```yaml
|
||||
# In CircleCI UI, define a Context named 'my-app-secrets' with MY_API_KEY
|
||||
jobs:
|
||||
deploy:
|
||||
docker:
|
||||
- image: cimg/base:stable
|
||||
steps:
|
||||
- run: echo "Deploying with API_KEY=$MY_API_KEY" # MY_API_KEY is loaded from 'my-app-secrets' context
|
||||
workflows:
|
||||
build-and-deploy:
|
||||
jobs:
|
||||
- deploy:
|
||||
context: my-app-secrets # Apply the context to the job
|
||||
```
|
||||
|
||||
## 3. Performance Optimization
|
||||
|
||||
### 3.1. Leverage Caching for Dependencies
|
||||
Cache dependencies and build artifacts to significantly speed up pipeline execution.
|
||||
|
||||
❌ BAD:
|
||||
```yaml
|
||||
jobs:
|
||||
build:
|
||||
docker:
|
||||
- image: cimg/node:18.17.1
|
||||
steps:
|
||||
- checkout
|
||||
- run: npm install # Downloads dependencies every time
|
||||
- run: npm test
|
||||
```
|
||||
|
||||
✅ GOOD:
|
||||
```yaml
|
||||
jobs:
|
||||
build:
|
||||
docker:
|
||||
- image: cimg/node:18.17.1
|
||||
steps:
|
||||
- checkout
|
||||
- restore_cache: # Restore cache based on package-lock.json
|
||||
keys:
|
||||
- v1-dependencies-{{ checksum "package-lock.json" }}
|
||||
- v1-dependencies- # Fallback to latest cache
|
||||
- run: npm install
|
||||
- save_cache: # Save cache if dependencies changed
|
||||
paths:
|
||||
- node_modules
|
||||
key: v1-dependencies-{{ checksum "package-lock.json" }}
|
||||
- run: npm test
|
||||
```
|
||||
|
||||
### 3.2. Parallelize Jobs and Tests
|
||||
Distribute work across multiple containers to reduce overall build time, especially for large test suites.
|
||||
|
||||
```yaml
|
||||
jobs:
|
||||
run-tests:
|
||||
docker:
|
||||
- image: cimg/node:18.17.1
|
||||
parallelism: 4 # Run 4 containers concurrently
|
||||
steps:
|
||||
- checkout
|
||||
- run:
|
||||
name: Run parallel tests
|
||||
command: |
|
||||
# Example: split tests using a tool like 'circleci tests split'
|
||||
npx jest --json --outputFile=test-results.json --coverage --testResultsProcessor="jest-junit" \
|
||||
$(circleci tests glob "src/**/*.test.js" | circleci tests split --split-by=timings)
|
||||
- store_test_results:
|
||||
path: test-results.json
|
||||
```
|
||||
|
||||
### 3.3. Consolidate `run` Steps
|
||||
Minimize UI noise and improve readability by combining related shell commands into a single `run` step.
|
||||
|
||||
❌ BAD:
|
||||
```yaml
|
||||
steps:
|
||||
- run: pip install example
|
||||
- run: example login $MY_TOKEN
|
||||
- run: example deploy my-app
|
||||
```
|
||||
|
||||
✅ GOOD:
|
||||
```yaml
|
||||
steps:
|
||||
- run:
|
||||
name: Deploy application
|
||||
command: |
|
||||
pip install example
|
||||
example login $MY_TOKEN
|
||||
example deploy my-app
|
||||
```
|
||||
|
||||
## 4. Robust Testing & Quality Gates
|
||||
|
||||
### 4.1. Implement Comprehensive Testing
|
||||
Automate unit, integration, and security (SAST/DAST) tests. Enforce these tests as gatekeepers before deployment.
|
||||
|
||||
```yaml
|
||||
workflows:
|
||||
build-test-deploy:
|
||||
jobs:
|
||||
- unit-test
|
||||
- integration-test:
|
||||
requires: [unit-test]
|
||||
- security-scan:
|
||||
requires: [integration-test]
|
||||
- deploy-staging:
|
||||
requires: [security-scan]
|
||||
- hold-for-prod:
|
||||
type: approval
|
||||
requires: [deploy-staging]
|
||||
- deploy-production:
|
||||
requires: [hold-for-prod]
|
||||
```
|
||||
|
||||
### 4.2. Serverless CI/CD Considerations
|
||||
For serverless applications, include specific steps for packaging, Infrastructure as Code (IaC) validation, and canary deployments.
|
||||
|
||||
```yaml
|
||||
jobs:
|
||||
serverless-deploy:
|
||||
docker:
|
||||
- image: cimg/python:3.9
|
||||
steps:
|
||||
- checkout
|
||||
- run: pip install serverless # Install Serverless Framework
|
||||
- run: serverless deploy --stage dev # Deploy to dev
|
||||
- run: serverless deploy --stage prod --type canary # Canary deployment
|
||||
```
|
||||
|
||||
## 5. Common Pitfalls & Best Practices
|
||||
|
||||
### 5.1. Conditionally Use `sudo`
|
||||
Check if the user is already root before adding `sudo` to commands.
|
||||
|
||||
❌ BAD:
|
||||
```yaml
|
||||
steps:
|
||||
- run: sudo apt-get update
|
||||
```
|
||||
|
||||
✅ GOOD:
|
||||
```yaml
|
||||
steps:
|
||||
- run:
|
||||
name: Update apt packages
|
||||
command: |
|
||||
if [[ $EUID == 0 ]]; then export SUDO=""; else export SUDO="sudo"; fi
|
||||
$SUDO apt-get update
|
||||
```
|
||||
|
||||
### 5.2. Pin Docker Image Tags
|
||||
Always use specific, stable Docker image tags, not `latest`.
|
||||
|
||||
❌ BAD: `cimg/node:latest`
|
||||
✅ GOOD: `cimg/node:18.17.1`
|
||||
|
||||
### 5.3. Use Admonitions in Documentation
|
||||
For orb descriptions or comments in `config.yml`, use CircleCI's admonition syntax for emphasis.
|
||||
|
||||
```yaml
|
||||
# NOTE: This command requires AWS credentials configured in a context.
|
||||
# CAUTION: Deploying to production will trigger a canary release.
|
||||
```
|
||||
Reference in New Issue
Block a user