|
| 1 | +name: Build and Push to Docker Hub |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [ master ] |
| 6 | + pull_request: |
| 7 | + branches: [ master ] |
| 8 | + |
| 9 | +env: |
| 10 | + REGISTRY: docker.io |
| 11 | + IMAGE_NAME: kubernetes-dashboard |
| 12 | + |
| 13 | +jobs: |
| 14 | + lint: |
| 15 | + runs-on: ubuntu-latest |
| 16 | + name: Code Quality Checks |
| 17 | + |
| 18 | + steps: |
| 19 | + - name: Checkout code |
| 20 | + uses: actions/checkout@v4 |
| 21 | + |
| 22 | + - name: Set up Python |
| 23 | + uses: actions/setup-python@v4 |
| 24 | + with: |
| 25 | + python-version: '3.9' |
| 26 | + cache: 'pip' # Enable pip caching |
| 27 | + |
| 28 | + - name: Install dependencies |
| 29 | + run: | |
| 30 | + python -m pip install --upgrade pip |
| 31 | + pip install flake8 black isort mypy |
| 32 | + if [ -f requirements.txt ]; then pip install -r requirements.txt; fi |
| 33 | + |
| 34 | + - name: Lint with flake8 |
| 35 | + run: | |
| 36 | + flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics |
| 37 | + flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics |
| 38 | + |
| 39 | + - name: Check code formatting with black |
| 40 | + run: black --check --diff . |
| 41 | + |
| 42 | + - name: Check import sorting with isort |
| 43 | + run: isort --check-only --diff . |
| 44 | + |
| 45 | + - name: Type checking with mypy |
| 46 | + run: mypy --ignore-missing-imports . |
| 47 | + |
| 48 | + security-scan: |
| 49 | + runs-on: ubuntu-latest |
| 50 | + name: Security Scanning |
| 51 | + |
| 52 | + steps: |
| 53 | + - name: Checkout code |
| 54 | + uses: actions/checkout@v4 |
| 55 | + |
| 56 | + - name: Run Bandit Security Linter |
| 57 | + uses: securecodewarrior/github-action-bandit-scan@v1 |
| 58 | + with: |
| 59 | + path: '.' |
| 60 | + |
| 61 | + - name: Run Semgrep |
| 62 | + uses: returntocorp/semgrep-action@v1 |
| 63 | + with: |
| 64 | + config: >- |
| 65 | + p/security-audit |
| 66 | + p/secrets |
| 67 | + p/xss |
| 68 | + p/command-injection |
| 69 | + |
| 70 | + - name: Check dependencies for vulnerabilities |
| 71 | + run: | |
| 72 | + python -m pip install --upgrade pip |
| 73 | + pip install safety |
| 74 | + if [ -f requirements.txt ]; then pip install -r requirements.txt; fi |
| 75 | + safety check --json --output safety-report.json || true |
| 76 | + |
| 77 | + - name: Upload safety report |
| 78 | + uses: actions/upload-artifact@v3 |
| 79 | + with: |
| 80 | + name: safety-report |
| 81 | + path: safety-report.json |
| 82 | + |
| 83 | + test: |
| 84 | + runs-on: ubuntu-latest |
| 85 | + name: Run Tests |
| 86 | + strategy: |
| 87 | + matrix: |
| 88 | + python-version: ['3.9', '3.11'] # Reduced from 4 to 2 versions |
| 89 | + |
| 90 | + steps: |
| 91 | + - name: Checkout code |
| 92 | + uses: actions/checkout@v4 |
| 93 | + |
| 94 | + - name: Set up Python ${{ matrix.python-version }} |
| 95 | + uses: actions/setup-python@v4 |
| 96 | + with: |
| 97 | + python-version: ${{ matrix.python-version }} |
| 98 | + cache: 'pip' # Enable pip caching |
| 99 | + |
| 100 | + - name: Install dependencies |
| 101 | + run: | |
| 102 | + python -m pip install --upgrade pip |
| 103 | + pip install -r requirements.txt |
| 104 | + pip install pytest pytest-cov pytest-mock |
| 105 | + |
| 106 | + - name: Run tests with coverage |
| 107 | + run: | |
| 108 | + pytest --cov=. --cov-report=xml --cov-report=term-missing --tb=short |
| 109 | + |
| 110 | + - name: Upload coverage to Codecov |
| 111 | + uses: codecov/codecov-action@v3 |
| 112 | + with: |
| 113 | + file: ./coverage.xml |
| 114 | + flags: unittests |
| 115 | + name: codecov-umbrella |
| 116 | + |
| 117 | + validate-docker: |
| 118 | + runs-on: ubuntu-latest |
| 119 | + name: Validate Docker Configuration |
| 120 | + |
| 121 | + steps: |
| 122 | + - name: Checkout code |
| 123 | + uses: actions/checkout@v4 |
| 124 | + |
| 125 | + - name: Validate Dockerfile |
| 126 | + run: | |
| 127 | + test -f Dockerfile || (echo "Dockerfile not found" && exit 1) |
| 128 | + docker build --dry-run -f Dockerfile . |
| 129 | + |
| 130 | + - name: Build test image |
| 131 | + run: | |
| 132 | + docker build -t test-image . |
| 133 | + |
| 134 | + - name: Test container startup |
| 135 | + run: | |
| 136 | + docker run -d --name test-container -p 5000:5000 test-image |
| 137 | + sleep 10 |
| 138 | + curl -f http://localhost:5000/health || exit 1 |
| 139 | + docker stop test-container |
| 140 | + docker rm test-container |
| 141 | +
|
| 142 | + build: |
| 143 | + needs: [lint, security-scan, test, validate-docker] |
| 144 | + runs-on: ubuntu-latest |
| 145 | + name: Build Docker Image |
| 146 | + if: github.event_name != 'pull_request' |
| 147 | + |
| 148 | + outputs: |
| 149 | + image-digest: ${{ steps.build.outputs.digest }} |
| 150 | + image-tag: ${{ steps.meta.outputs.tags }} |
| 151 | + |
| 152 | + steps: |
| 153 | + - name: Checkout code |
| 154 | + uses: actions/checkout@v4 |
| 155 | + |
| 156 | + - name: Set up Docker Buildx |
| 157 | + uses: docker/setup-buildx-action@v3 |
| 158 | + |
| 159 | + - name: Log in to Docker Hub |
| 160 | + uses: docker/login-action@v3 |
| 161 | + with: |
| 162 | + username: ${{ secrets.DOCKER_USERNAME }} |
| 163 | + password: ${{ secrets.DOCKER_PASSWORD }} |
| 164 | + |
| 165 | + - name: Extract metadata |
| 166 | + id: meta |
| 167 | + uses: docker/metadata-action@v5 |
| 168 | + with: |
| 169 | + images: ${{ env.REGISTRY }}/${{ secrets.DOCKER_USERNAME }}/${{ env.IMAGE_NAME }} |
| 170 | + tags: | |
| 171 | + type=raw,value=latest,enable={{is_default_branch}} |
| 172 | + |
| 173 | + - name: Build and push Docker image |
| 174 | + id: build |
| 175 | + uses: docker/build-push-action@v5 |
| 176 | + with: |
| 177 | + context: . |
| 178 | + platforms: linux/amd64 # Reduced to single platform for faster builds |
| 179 | + push: true |
| 180 | + tags: ${{ steps.meta.outputs.tags }} |
| 181 | + labels: ${{ steps.meta.outputs.labels }} |
| 182 | + cache-from: type=gha |
| 183 | + cache-to: type=gha,mode=max |
| 184 | + build-args: | |
| 185 | + BUILD_DATE=${{ fromJSON(steps.meta.outputs.json).labels['org.opencontainers.image.created'] }} |
| 186 | + VCS_REF=${{ github.sha }} |
| 187 | + # Optimize build layers |
| 188 | + target: production |
| 189 | + |
| 190 | + - name: Generate SBOM |
| 191 | + uses: anchore/sbom-action@v0 |
| 192 | + with: |
| 193 | + image: ${{ steps.meta.outputs.tags }} |
| 194 | + format: spdx-json |
| 195 | + output-file: sbom.spdx.json |
| 196 | + |
| 197 | + - name: Upload SBOM artifact |
| 198 | + uses: actions/upload-artifact@v3 |
| 199 | + with: |
| 200 | + name: sbom |
| 201 | + path: sbom.spdx.json |
| 202 | + |
| 203 | + security-scan-image: |
| 204 | + needs: build |
| 205 | + runs-on: ubuntu-latest |
| 206 | + name: Scan Docker Image |
| 207 | + if: github.event_name != 'pull_request' |
| 208 | + |
| 209 | + steps: |
| 210 | + - name: Run Trivy vulnerability scanner |
| 211 | + uses: aquasecurity/trivy-action@master |
| 212 | + with: |
| 213 | + image-ref: ${{ needs.build.outputs.image-tag }} |
| 214 | + format: 'table' |
| 215 | + |
| 216 | + - name: Generate SARIF report |
| 217 | + uses: aquasecurity/trivy-action@master |
| 218 | + with: |
| 219 | + image-ref: ${{ needs.build.outputs.image-tag }} |
| 220 | + format: 'sarif' |
| 221 | + output: 'trivy-results.sarif' |
| 222 | + |
| 223 | + - name: Upload Trivy scan results as artifact |
| 224 | + uses: actions/upload-artifact@v3 |
| 225 | + with: |
| 226 | + name: trivy-scan-results |
| 227 | + path: trivy-results.sarif |
| 228 | + |
| 229 | + notify: |
| 230 | + needs: [lint, security-scan, test, validate-docker, build, security-scan-image] |
| 231 | + runs-on: ubuntu-latest |
| 232 | + name: Build Status |
| 233 | + if: always() |
| 234 | + |
| 235 | + steps: |
| 236 | + - name: Notify on success |
| 237 | + if: needs.lint.result == 'success' && needs.security-scan.result == 'success' && needs.test.result == 'success' && needs.validate-docker.result == 'success' && (needs.build.result == 'success' || needs.build.result == 'skipped') && (needs.security-scan-image.result == 'success' || needs.security-scan-image.result == 'skipped') |
| 238 | + run: | |
| 239 | + echo "β
All checks passed! Image ${{ needs.build.outputs.image-tag }} built and pushed successfully" |
| 240 | + |
| 241 | + - name: Notify on failure |
| 242 | + if: needs.lint.result == 'failure' || needs.security-scan.result == 'failure' || needs.test.result == 'failure' || needs.validate-docker.result == 'failure' || needs.build.result == 'failure' || needs.security-scan-image.result == 'failure' |
| 243 | + run: | |
| 244 | + echo "β Some checks failed!" |
| 245 | + exit 1 |
0 commit comments