Automating Code Style Enforcement in CI/CD Pipelines
Integrate code formatting checks into CI/CD workflows to ensure consistent style adherence.
0 likes
11 views
Rule Content
# Automating Code Style Enforcement in CI/CD Pipelines ## Context - **When to Apply**: This rule is applicable during the Continuous Integration/Continuous Deployment (CI/CD) process to ensure that all code adheres to the project's defined style guidelines before being merged or deployed. - **Prerequisites**: A code formatter (e.g., Prettier for JavaScript, Black for Python) must be configured and accessible within the CI/CD environment. ## Requirements - **Integration of Code Formatter**: Configure the CI/CD pipeline to run the code formatter on all relevant code files during the build process. - **Enforcement of Formatting**: The pipeline should fail if any code does not conform to the defined style guidelines, preventing unformatted code from being merged or deployed. - **Consistent Configuration**: Ensure that the code formatter's configuration file (e.g., `.prettierrc`, `pyproject.toml`) is included in the repository and is consistent across all development environments. - **Automated Fixes**: Optionally, configure the pipeline to automatically apply formatting fixes and commit them back to the repository, or provide clear instructions for developers to do so locally. ## Examples # Example CI/CD configuration for a Node.js project using Prettier name: CI on: push: branches: - main pull_request: branches: - main jobs: format-check: runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v3 - name: Set up Node.js uses: actions/setup-node@v3 with: node-version: '16' - name: Install dependencies run: npm install - name: Run Prettier run: npx prettier --check . - name: Fail if unformatted code is found run: | if [ $? -ne 0 ]; then echo "Code formatting issues detected. Please run 'npx prettier --write .' locally and commit the changes." exit 1 fi # Example CI/CD configuration for a Python project using Black name: CI on: push: branches: - main pull_request: branches: - main jobs: format-check: runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v3 - name: Set up Python uses: actions/setup-python@v3 with: python-version: '3.10' - name: Install Black run: pip install black - name: Run Black run: black --check . - name: Fail if unformatted code is found run: | if [ $? -ne 0 ]; then echo "Code formatting issues detected. Please run 'black .' locally and commit the changes." exit 1 fi # Example CI/CD configuration for a Java project using Checkstyle name: CI on: push: branches: - main pull_request: branches: - main jobs: format-check: runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v3 - name: Set up JDK uses: actions/setup-java@v3 with: java-version: '17' distribution: 'temurin' - name: Install Checkstyle run: | wget -qO- https://github.com/checkstyle/checkstyle/releases/download/checkstyle-10.3.3/checkstyle-10.3.3-all.jar > checkstyle.jar - name: Run Checkstyle run: java -jar checkstyle.jar -c /google_checks.xml src/ - name: Fail if unformatted code is found run: | if [ $? -ne 0 ]; then echo "Code formatting issues detected. Please run Checkstyle locally and commit the changes." exit 1 fi # Example CI/CD configuration for a C# project using dotnet format name: CI on: push: branches: - main pull_request: branches: - main jobs: format-check: runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v3 - name: Set up .NET uses: actions/setup-dotnet@v3 with: dotnet-version: '6.0.x' - name: Install dotnet format run: dotnet tool install -g dotnet-format - name: Run dotnet format run: dotnet format --verify-no-changes - name: Fail if unformatted code is found run: | if [ $? -ne 0 ]; then echo "Code formatting issues detected. Please run 'dotnet format' locally and commit the changes." exit 1 fi # Example CI/CD configuration for a Go project using gofmt name: CI on: push: branches: - main pull_request: branches: - main jobs: format-check: runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v3 - name: Set up Go uses: actions/setup-go@v3 with: go-version: '1.18' - name: Run gofmt run: | unformatted=$(gofmt -l .) if [ -n "$unformatted" ]; then echo "Code formatting issues detected in the following files:" echo "$unformatted" echo "Please run 'gofmt -w .' locally and commit the changes." exit 1 fi # Example CI/CD configuration for a PHP project using PHP_CodeSniffer name: CI on: push: branches: - main pull_request: branches: - main jobs: format-check: runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v3 - name: Set up PHP uses: actions/setup-php@v3 with: php-version: '8.1' - name: Install PHP_CodeSniffer run: composer global require "squizlabs/php_codesniffer=*" - name: Run PHP_CodeSniffer run: ~/.composer/vendor/bin/phpcs --standard=PSR12 . - name: Fail if unformatted code is found run: | if [ $? -ne 0 ]; then echo "Code formatting issues detected. Please run 'phpcbf' locally and commit the changes." exit 1 fi # Example CI/CD configuration for a Ruby project using RuboCop name: CI on: push: branches: - main pull_request: branches: - main jobs: format-check: runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v3 - name: Set up Ruby uses: actions/setup-ruby@v3 with: ruby-version: '3.1' - name: Install RuboCop run: gem install rubocop - name: Run RuboCop run: rubocop --parallel - name: Fail if unformatted code is found run: | if [ $? -ne 0 ]; then echo "Code formatting issues detected. Please run 'rubocop -a' locally and commit the changes." exit 1 fi # Example CI/CD configuration for a Swift project using SwiftLint name: CI on: push: branches: - main pull_request: branches: - main jobs: format-check: runs-on: macos-latest steps: - name: Checkout code uses: actions/checkout@v3 - name: Install SwiftLint run: brew install swiftlint - name: Run SwiftLint run: swiftlint - name: Fail if unformatted code is found run: | if [ $? -ne 0 ]; then echo "Code formatting issues detected. Please run 'swiftlint autocorrect' locally and commit the changes." exit 1 fi # Example CI/CD configuration for a Kotlin project using ktlint name: CI on: push: branches: - main pull_request: branches: - main jobs: format-check: runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v3 - name: Set up JDK uses: actions/setup-java@v3 with: java-version: '17' distribution: 'temurin' - name: Install ktlint run: | curl -sSLO https://github.com/pinterest/ktlint/releases/download/0.45.2/ktlint chmod a+x ktlint sudo mv ktlint /usr/local/bin/ - name: Run ktlint run: ktlint --android --reporter=plain - name: Fail if unformatted code is found run: | if [ $? -ne 0 ]; then echo "Code formatting issues detected. Please run 'ktlint -F' locally and commit the changes." exit 1 fi # Example CI/CD configuration for a TypeScript project using ESLint and Prettier name: CI on: push: branches: - main pull_request: branches: - main jobs: format-check: runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v3 - name: Set up Node.js uses: actions/setup-node@v3 with: node-version: '16' - name: Install dependencies run: npm install - name: Run ESLint run: npx eslint . --ext .ts,.tsx - name: Run Prettier run: npx prettier --check . - name: Fail if unformatted code is found run: | if [ $? -ne