From 239886a263e9e903eb5369484c28c5e77fb192f4 Mon Sep 17 00:00:00 2001 From: Bruno Domingues Date: Wed, 14 May 2025 16:17:54 -0300 Subject: [PATCH] Add quality gate workflow. --- .github/workflows/quality-gate.yml | 108 +++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 .github/workflows/quality-gate.yml diff --git a/.github/workflows/quality-gate.yml b/.github/workflows/quality-gate.yml new file mode 100644 index 0000000..84bc294 --- /dev/null +++ b/.github/workflows/quality-gate.yml @@ -0,0 +1,108 @@ +name: Quality gate + +on: + push: + branches: + - '**' + pull_request: + branches: + - '**' + types: [ opened, synchronize, reopened ] + +jobs: + sonar: + name: SonarQube Analysis + runs-on: ubuntu-latest + permissions: + contents: read + + steps: + - name: ⬇️ Checkout Code + uses: actions/checkout@v4 + with: + fetch-depth: 0 + persist-credentials: false + + - name: Generate App Token + id: generate-app-token + uses: actions/create-github-app-token@v1 + with: + app-id: ${{ secrets.APP_ID }} + private-key: ${{ secrets.APP_PRIVATE_KEY }} + owner: 'Aignosi' + repositories: 'sientia-dataops-library,sientia-mlops-library' + + - name: Prepare requirements.txt + id: prepare-requirements + run: | + sed -e "s|git+ssh://git@github.com/|git+https://github.com/|g" \ + -e "s|git@github.com:|git+https://github.com/|g" \ + requirements.txt > requirements_prepared.txt + echo "PROCESSED_REQUIREMENTS_FILE=requirements_prepared.txt" >> $GITHUB_OUTPUT + + - name: Configure Git to use App Token + env: + GH_APP_TOKEN: ${{ steps.generate-app-token.outputs.token }} + run: | + git config --global url."https://oauth2:${GH_APP_TOKEN}@github.com/".insteadOf "https://github.com/" + + - name: 🔧 Setup Python + uses: actions/setup-python@v4 + with: + python-version: "3.11" + + - name: 🗄️ Cache Python dependencies + uses: actions/cache@v3 + with: + path: ~/.cache/pip + key: ${{ runner.os }}-pip-${{ hashFiles(steps.prepare-requirements.outputs.PROCESSED_REQUIREMENTS_FILE) }} + restore-keys: | + ${{ runner.os }}-pip- + + - name: 📦 Install Dependencies + run: | + python -m pip install --upgrade pip + pip install -r ${{ steps.prepare-requirements.outputs.PROCESSED_REQUIREMENTS_FILE }} + pip install pytest pytest-cov + + - name: ⬇️ Setup Node.js 18 + uses: actions/setup-node@v4 + with: + node-version: 18 + + - name: 📥 Setup SonarScanner + uses: warchant/setup-sonar-scanner@v7 + + - name: 🧪 Run Tests with Pytest + run: | + set +e + pytest tests --junitxml=pytest.xml --cov=laborious --cov-report=xml --cov-report=term + PYTEST_EXIT_CODE=$? + set -e + + if [ $PYTEST_EXIT_CODE -eq 0 ]; then + echo "Pytest executado com sucesso." + elif [ $PYTEST_EXIT_CODE -eq 5 ]; then + echo "Pytest finalizado com código 5 (Nenhum teste encontrado). Tratando como sucesso para este workflow." + exit 0 + else + echo "Pytest falhou com código de saída $PYTEST_EXIT_CODE." + exit $PYTEST_EXIT_CODE + fi + + - name: 📊 Run SonarQube Analysis + env: + SONAR_PROJECT_KEY: ${{ secrets.SONAR_PROJECT_KEY }} + SONAR_HOST_URL: ${{ secrets.SONAR_HOST_URL }} + SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} + run: | + sonar-scanner \ + -Dsonar.projectKey=$SONAR_PROJECT_KEY \ + -Dsonar.sources=laborious \ + -Dsonar.tests=tests \ + -Dsonar.python.coverage.reportPaths=coverage.xml \ + -Dsonar.python.xunit.reportPath=pytest.xml \ + -Dsonar.host.url=$SONAR_HOST_URL \ + -Dsonar.token=$SONAR_TOKEN \ + -Dsonar.python.version=3.11 \ + -Dsonar.projectVersion=1.0.0