Skip to content

Kubernetes-dashboard: refactor Dockerfile for Trivy installation method #9

Kubernetes-dashboard: refactor Dockerfile for Trivy installation method

Kubernetes-dashboard: refactor Dockerfile for Trivy installation method #9

Workflow file for this run

name: Build and Push to Docker Hub
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
env:
REGISTRY: docker.io
IMAGE_NAME: kubernetes-dashboard
jobs:
test:
runs-on: ubuntu-latest
name: Run Tests
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.9'
cache: 'pip'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
pip install pytest pytest-cov pytest-mock
- name: Run tests with coverage
run: |
pytest --cov=. --cov-report=xml --cov-report=term-missing --tb=short
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v4
with:
file: ./coverage.xml
flags: unittests
name: codecov-umbrella
build:
needs: test
runs-on: ubuntu-latest
name: Build Docker Image
if: github.event_name != 'pull_request'
outputs:
image-digest: ${{ steps.build.outputs.digest }}
image-tag: ${{ steps.meta.outputs.tags }}
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Log in to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Extract metadata
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ secrets.DOCKER_USERNAME }}/${{ env.IMAGE_NAME }}
tags: |
type=raw,value=latest,enable={{is_default_branch}}
- name: Build and push Docker image
id: build
uses: docker/build-push-action@v5
with:
context: .
platforms: linux/amd64
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
build-args: |
BUILD_DATE=${{ fromJSON(steps.meta.outputs.json).labels['org.opencontainers.image.created'] }}
VCS_REF=${{ github.sha }}
- name: Generate SBOM
uses: anchore/sbom-action@v0
with:
image: ${{ steps.meta.outputs.tags }}
format: spdx-json
output-file: sbom.spdx.json
- name: Upload SBOM artifact
uses: actions/upload-artifact@v4
with:
name: sbom
path: sbom.spdx.json
security-scan-image:
needs: build
runs-on: ubuntu-latest
name: Scan Docker Image
if: github.event_name != 'pull_request'
steps:
- name: Run Trivy vulnerability scanner
uses: aquasecurity/trivy-action@master
with:
image-ref: ${{ needs.build.outputs.image-tag }}
format: 'table'
- name: Generate SARIF report
uses: aquasecurity/trivy-action@master
with:
image-ref: ${{ needs.build.outputs.image-tag }}
format: 'sarif'
output: 'trivy-results.sarif'
- name: Upload Trivy scan results as artifact
uses: actions/upload-artifact@v4
with:
name: trivy-scan-results
path: trivy-results.sarif
notify:
needs: [test, build, security-scan-image]
runs-on: ubuntu-latest
name: Build Status
if: always()
steps:
- name: Notify on success
if: needs.test.result == 'success' && needs.build.result == 'success' && (needs.security-scan-image.result == 'success' || needs.security-scan-image.result == 'skipped')
run: |
echo "✅ All checks passed! Image ${{ needs.build.outputs.image-tag }} built and pushed successfully"
- name: Notify on failure
if: needs.test.result == 'failure' || needs.build.result == 'failure' || needs.security-scan-image.result == 'failure'
run: |
echo "❌ Some checks failed!"
exit 1