name: Docker
on:
push:
branches: [main, develop]
paths:
- 'Dockerfile'
- '.github/workflows/docker.yml'
- 'src/**'
- 'Cargo.toml'
- 'Cargo.lock'
pull_request:
branches: [main, develop]
paths:
- 'Dockerfile'
- '.github/workflows/docker.yml'
- 'src/**'
- 'Cargo.toml'
- 'Cargo.lock'
schedule:
- cron: '0 0 * * 0'
env:
CARGO_TERM_COLOR: always
jobs:
build:
name: Build Docker Image
runs-on: ubuntu-latest
timeout-minutes: 30
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 1
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Extract metadata for Docker
id: meta
uses: docker/metadata-action@v5
with:
images: vecboost/vecboost
tags: |
type=ref,event=branch
type=ref,event=pr
type=sha
type=raw,value=latest,enable={{is_default_branch}}
- name: Build Docker image
uses: docker/build-push-action@v6
with:
context: .
push: false
load: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
platforms: linux/amd64,linux/arm64
- name: Verify image
run: |
docker images | grep vecboost
docker run --rm ${{ steps.meta.outputs.tags }} --version || true
scan:
name: Security Scan
runs-on: ubuntu-latest
needs: build
timeout-minutes: 20
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Build Docker image for scanning
uses: docker/build-push-action@v6
with:
context: .
push: false
load: true
tags: vecboost/scan:latest
platforms: linux/amd64
- name: Run Trivy vulnerability scanner
uses: aquasecurity/trivy-action@master
with:
image-ref: 'vecboost/scan:latest'
format: 'sarif'
output: 'trivy-results.sarif'
- name: Upload Trivy scan results to GitHub Security
uses: github/codeql-action/upload-sarif@v3
with:
sarif_file: 'trivy-results.sarif'
- name: Check for critical vulnerabilities
run: |
if grep -q '"severity": "CRITICAL"' trivy-results.sarif; then
echo "❌ Critical vulnerabilities found!"
exit 1
else
echo "✅ No critical vulnerabilities found"
fi
deploy:
name: Deploy to Registry
runs-on: ubuntu-latest
needs: [build, scan]
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
timeout-minutes: 20
permissions:
contents: read
packages: write
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 1
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_HUB_USERNAME }}
password: ${{ secrets.DOCKER_HUB_TOKEN }}
- name: Login to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract metadata for Docker
id: meta
uses: docker/metadata-action@v5
with:
images: |
vecboost/vecboost
ghcr.io/${{ github.repository }}
tags: |
type=sha
type=ref,event=branch
type=raw,value=latest,enable={{is_default_branch}}
- name: Build and push Docker images
uses: docker/build-push-action@v6
with:
context: .
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
platforms: linux/amd64,linux/arm64
- name: Digest verification
run: |
echo "Image digests:"
echo "${{ steps.meta.outputs.tags }}" | tr ',' '\n' | while read tag; do
echo -n "$tag: "
docker inspect --format='{{.Id}}' "$tag" 2>/dev/null || echo "not found"
done