Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 108 additions & 0 deletions workflow-templates/python-ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
name: CI Pipeline

on:
push:
branches: [$default-branch]
pull_request:
branches: [$default-branch]

env:
SONAR_SCANNER_VERSION: "6.1.0.4477"
SONAR_SCANNER_HOME: "${{ github.workspace }}/sonar-scanner"
PYTHON_VERSION: '3.11'
POETRY_VERSION: '1.8.0'

jobs:
run-tests:
name: Run Python Tests
runs-on: self-hosted
steps:
- uses: actions/checkout@v3

- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: ${{ env.PYTHON_VERSION }}
cache: 'poetry'

- name: Install Poetry
uses: snok/install-poetry@v1
with:
version: ${{ env.POETRY_VERSION }}
virtualenvs-create: true
virtualenvs-in-project: true

- name: Cache Poetry dependencies
id: poetry-cache
uses: actions/cache@v3
with:
path: |
.venv
~/.cache/pypoetry
key: ${{ runner.os }}-poetry-${{ hashFiles('**/poetry.lock') }}
restore-keys: |
${{ runner.os }}-poetry-

- name: Install dependencies
if: steps.poetry-cache.outputs.cache-hit != 'true'
run: poetry install --no-interaction --no-ansi

- name: Run tests
run: |
source .env
poetry run pytest

- name: Download and install SonarQube Scanner
run: |
mkdir -p ${{ env.SONAR_SCANNER_HOME }}
curl -L -o sonar-scanner-cli.zip https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-${{ env.SONAR_SCANNER_VERSION }}-linux-x64.zip
unzip sonar-scanner-cli.zip -d ${{ env.SONAR_SCANNER_HOME }}
echo "${{ env.SONAR_SCANNER_HOME }}/sonar-scanner-${{ env.SONAR_SCANNER_VERSION }}-linux-x64/bin" >> $GITHUB_PATH

- name: Run SonarQube Scanner
env:
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
SONAR_HOST_URL: ${{ secrets.SONAR_HOST_URL }}
SONAR_PROJECT_KEY: ${{ secrets.SONAR_PROJECT_KEY }}
run: |
COMMON_ARGS="-Dsonar.projectKey=${SONAR_PROJECT_KEY} -Dsonar.sources=. -Dsonar.host.url=${SONAR_HOST_URL} -Dsonar.token=${SONAR_TOKEN} -Dsonar.qualitygate.wait=false"
sonar-scanner ${COMMON_ARGS} -Dsonar.analysis.branch=${GITHUB_HEAD_REF} -Dsonar.analysis.pullRequest=${PR_NUMBER} -Dsonar.buildString=${PR_NUMBER}

docker-build-push:
name: Build and Push Docker Images
runs-on: ubuntu-latest
needs: [run-tests]
if: github.event_name == 'push'
steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2

- name: Log in to Docker Hub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}

- name: Extract metadata (tags, labels) for backend
id: meta-backend
uses: docker/metadata-action@v4
with:
images: ${{ secrets.DOCKERHUB_USERNAME }}/project-backend
tags: |
type=ref,event=branch
type=sha,format=short
latest

- name: Build and push backend image
uses: docker/build-push-action@v4
with:
context: .
file: ./backend.dockerfile
push: true
tags: ${{ steps.meta-backend.outputs.tags }}
labels: ${{ steps.meta-backend.outputs.labels }}
cache-from: type=registry,ref=${{ secrets.DOCKERHUB_USERNAME }}/project-backend:buildcache
cache-to: type=registry,ref=${{ secrets.DOCKERHUB_USERNAME }}/project-backend:buildcache,mode=max