|
| 1 | +# CI/CD Integration |
| 2 | + |
| 3 | +Complete CI/CD workflows for shell script quality assurance. |
| 4 | + |
| 5 | +## GitHub Actions |
| 6 | + |
| 7 | +**.github/workflows/shell-quality.yml**: |
| 8 | + |
| 9 | +```yaml |
| 10 | +name: Shell Script Quality |
| 11 | +on: |
| 12 | + push: |
| 13 | + branches: [main, master] |
| 14 | + pull_request: |
| 15 | + branches: [main, master] |
| 16 | + |
| 17 | +jobs: |
| 18 | + shellcheck: |
| 19 | + name: ShellCheck |
| 20 | + runs-on: ubuntu-latest |
| 21 | + steps: |
| 22 | + - uses: actions/checkout@v4 |
| 23 | + - name: Run ShellCheck |
| 24 | + uses: ludeeus/action-shellcheck@master |
| 25 | + with: |
| 26 | + scandir: './scripts' |
| 27 | + severity: warning |
| 28 | + additional_files: 'hooks tests/*.sh' |
| 29 | + |
| 30 | + bats: |
| 31 | + name: BATS Tests |
| 32 | + runs-on: ubuntu-latest |
| 33 | + steps: |
| 34 | + - uses: actions/checkout@v4 |
| 35 | + - name: Install BATS |
| 36 | + run: sudo apt-get install -y bats |
| 37 | + - name: Run Tests |
| 38 | + run: bats tests/ |
| 39 | + |
| 40 | + quality-gate: |
| 41 | + name: Complete Quality Check |
| 42 | + runs-on: ubuntu-latest |
| 43 | + needs: [shellcheck, bats] |
| 44 | + steps: |
| 45 | + - uses: actions/checkout@v4 |
| 46 | + - name: All checks passed |
| 47 | + run: echo "✅ Shell script quality gate passed!" |
| 48 | +``` |
| 49 | +
|
| 50 | +## GitLab CI |
| 51 | +
|
| 52 | +**.gitlab-ci.yml**: |
| 53 | +
|
| 54 | +```yaml |
| 55 | +stages: |
| 56 | + - lint |
| 57 | + - test |
| 58 | + |
| 59 | +shellcheck: |
| 60 | + stage: lint |
| 61 | + image: koalaman/shellcheck-alpine:stable |
| 62 | + script: |
| 63 | + - shellcheck scripts/*.sh hooks/*.sh tests/*.sh |
| 64 | + rules: |
| 65 | + - if: $CI_PIPELINE_SOURCE == "merge_request_event" |
| 66 | + - if: $CI_COMMIT_BRANCH == "main" |
| 67 | + |
| 68 | +bats: |
| 69 | + stage: test |
| 70 | + image: ubuntu:latest |
| 71 | + before_script: |
| 72 | + - apt-get update && apt-get install -y bats |
| 73 | + script: |
| 74 | + - bats tests/ |
| 75 | + rules: |
| 76 | + - if: $CI_PIPELINE_SOURCE == "merge_request_event" |
| 77 | + - if: $CI_COMMIT_BRANCH == "main" |
| 78 | +``` |
| 79 | +
|
| 80 | +## Pre-commit Hooks |
| 81 | +
|
| 82 | +**.git/hooks/pre-commit**: |
| 83 | +
|
| 84 | +```bash |
| 85 | +#!/bin/bash |
| 86 | +set -euo pipefail |
| 87 | + |
| 88 | +changed_scripts=$(git diff --cached --name-only | grep -E '\.(sh|bash)$' || true) |
| 89 | + |
| 90 | +if [[ -z "$changed_scripts" ]]; then |
| 91 | + exit 0 |
| 92 | +fi |
| 93 | + |
| 94 | +echo "🔍 Checking shell scripts..." |
| 95 | + |
| 96 | +# ShellCheck |
| 97 | +for script in $changed_scripts; do |
| 98 | + shellcheck "$script" || { |
| 99 | + echo "❌ ShellCheck failed: $script" |
| 100 | + exit 1 |
| 101 | + } |
| 102 | +done |
| 103 | + |
| 104 | +# BATS (if tests exist) |
| 105 | +if [[ -d tests ]]; then |
| 106 | + bats tests/ || { |
| 107 | + echo "❌ BATS tests failed" |
| 108 | + exit 1 |
| 109 | + } |
| 110 | +fi |
| 111 | + |
| 112 | +echo "✅ All checks passed!" |
| 113 | +``` |
| 114 | + |
| 115 | +Make executable: `chmod +x .git/hooks/pre-commit` |
| 116 | + |
| 117 | +## Quality Check Script |
| 118 | + |
| 119 | +**scripts/check-quality.sh**: |
| 120 | + |
| 121 | +```bash |
| 122 | +#!/bin/bash |
| 123 | +set -euo pipefail |
| 124 | + |
| 125 | +printf "=== Shell Script Quality Check ===\n\n" |
| 126 | + |
| 127 | +# ShellCheck |
| 128 | +printf "🔍 Running ShellCheck...\n" |
| 129 | +find scripts hooks tests -name "*.sh" -exec shellcheck {} + && \ |
| 130 | +printf "✅ ShellCheck: PASSED\n\n" || exit 1 |
| 131 | + |
| 132 | +# BATS |
| 133 | +printf "🧪 Running BATS tests...\n" |
| 134 | +bats tests/ && \ |
| 135 | +printf "✅ BATS: PASSED\n\n" || exit 1 |
| 136 | + |
| 137 | +# Permissions |
| 138 | +printf "🔐 Checking permissions...\n" |
| 139 | +find scripts hooks -name "*.sh" ! -perm -111 | \ |
| 140 | +while read -r file; do |
| 141 | + printf "⚠️ Not executable: %s\n" "$file" |
| 142 | +done |
| 143 | + |
| 144 | +printf "\n🎉 All quality checks PASSED!\n" |
| 145 | +``` |
| 146 | + |
| 147 | +## Multi-Stage Pipelines |
| 148 | + |
| 149 | +### Fast Lint (PR check) |
| 150 | +```yaml |
| 151 | +lint-fast: |
| 152 | + runs-on: ubuntu-latest |
| 153 | + steps: |
| 154 | + - uses: actions/checkout@v4 |
| 155 | + - uses: ludeeus/action-shellcheck@master |
| 156 | + with: |
| 157 | + scandir: 'scripts/' |
| 158 | +``` |
| 159 | +
|
| 160 | +### Full Test Suite (Main branch) |
| 161 | +```yaml |
| 162 | +full-test: |
| 163 | + if: github.ref == 'refs/heads/main' |
| 164 | + needs: lint-fast |
| 165 | + steps: |
| 166 | + - uses: actions/checkout@v4 |
| 167 | + - run: sudo apt-get install -y bats |
| 168 | + - run: bats tests/ |
| 169 | +``` |
| 170 | +
|
| 171 | +## Cache Optimization |
| 172 | +
|
| 173 | +```yaml |
| 174 | +- name: Cache BATS |
| 175 | + uses: actions/cache@v4 |
| 176 | + with: |
| 177 | + path: ~/.bats |
| 178 | + key: bats-${{ hashFiles('tests/**/*.bats') }} |
| 179 | +``` |
| 180 | +
|
| 181 | +## Quality Gates |
| 182 | +
|
| 183 | +```yaml |
| 184 | +quality-gate: |
| 185 | + needs: [lint, test] |
| 186 | + if: always() |
| 187 | + steps: |
| 188 | + - name: Check Results |
| 189 | + run: | |
| 190 | + if [[ "${{ needs.lint.result }}" != 'success' || \ |
| 191 | + "${{ needs.test.result }}" != 'success' ]]; then |
| 192 | + echo '❌ Quality gate failed' |
| 193 | + exit 1 |
| 194 | + fi |
| 195 | + echo '✅ All checks passed' |
| 196 | +``` |
| 197 | +
|
| 198 | +## VS Code Devcontainer |
| 199 | +
|
| 200 | +**.devcontainer/devcontainer.json**: |
| 201 | +
|
| 202 | +```json |
| 203 | +{ |
| 204 | + "features": { |
| 205 | + "ghcr.io/devcontainers/features/shellcheck:1": {}, |
| 206 | + "ghcr.io/devcontainers/features/bats-core:1": {} |
| 207 | + }, |
| 208 | + "postCreateCommand": "chmod +x scripts/check-quality.sh" |
| 209 | +} |
| 210 | +``` |
0 commit comments