refactor cicd pipeline
Some checks failed
Build and Deploy Demo App / test (push) Successful in 11s
Build and Deploy Demo App / build (push) Successful in 20s
Build and Deploy Demo App / scan (push) Failing after 8s
Build and Deploy Demo App / deploy (push) Has been skipped

This commit is contained in:
2025-11-26 13:03:25 +03:30
parent 340d3ed95c
commit c473ffbd16

View File

@@ -6,7 +6,7 @@ on:
- main
jobs:
# 1) Run tests first
# ------------------ TEST ------------------
test:
runs-on: ubuntu-latest
steps:
@@ -23,48 +23,53 @@ jobs:
- name: Run tests
env:
DB_USER: '' # Empty to force SQLite fallback
DB_USER: '' # force SQLite fallback
DB_PASS: ''
DB_NAME: ''
run: pytest
# 2) Build image, scan it, and deploy (all on the same runner)
build_scan_deploy:
# ------------------ BUILD ------------------
build:
needs: test
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Cache Docker layers
uses: actions/cache@v4
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-buildx-${{ hashFiles('Dockerfile', 'requirements.txt') }}
restore-keys: ${{ runner.os }}-buildx-
- name: Build Docker image
uses: docker/build-push-action@v6
with:
load: true # load image into local Docker
tags: demo-app:latest
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,dest=/tmp/.buildx-cache-new,mode=max
run: docker build -t demo-app:latest .
# --- Security scan happens on the same runner as the build ---
# ------------------ SCAN ------------------
scan:
needs: build
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
# Rebuild image in this job so it's available locally
- name: Build Docker image for scan
run: docker build -t demo-app:latest .
# Use Trivy via Docker, no GitHub-specific action
- name: Scan image with Trivy
uses: aquasecurity/trivy-action@master
with:
image-ref: demo-app:latest
format: table
exit-code: 1
severity: CRITICAL,HIGH
run: |
docker run --rm \
-v /var/run/docker.sock:/var/run/docker.sock \
aquasecurity/trivy:latest \
image --exit-code 1 --severity CRITICAL,HIGH demo-app:latest
# If Trivy finds HIGH/CRITICAL vulns, the step above fails and
# nothing below (including deploy) will run.
# ------------------ DEPLOY ------------------
deploy:
needs: [build, scan]
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
# Rebuild image for deployment
- name: Build Docker image for deploy
run: docker build -t demo-app:latest .
- name: Save Docker image to tar
run: docker save demo-app:latest > demo-app.tar