Stage to Prod #77
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Unit Tests | |
| on: | |
| workflow_dispatch: | |
| push: | |
| branches-ignore: | |
| - '**' | |
| pull_request: | |
| branches: [ main, dev, stage ] | |
| jobs: | |
| UnitTest: | |
| runs-on: ubuntu-latest | |
| env: | |
| DATABASE_NAME: test_database | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| - name: Set up Python | |
| uses: actions/setup-python@v5 | |
| with: | |
| python-version: '3.10' | |
| - name: Install dependencies | |
| run: | | |
| python -m pip install --upgrade pip | |
| pip install -r requirements.txt | |
| - name: Determine output folder | |
| id: set_output_folder | |
| shell: bash | |
| run: | | |
| if [[ "$GITHUB_EVENT_NAME" == "pull_request" ]]; then | |
| branch_name="$GITHUB_BASE_REF" | |
| else | |
| branch_name="$GITHUB_REF_NAME" | |
| fi | |
| case "$branch_name" in | |
| main) echo "output_folder=prod" >> "$GITHUB_ENV" ;; | |
| stage) echo "output_folder=stage" >> "$GITHUB_ENV" ;; | |
| dev) echo "output_folder=dev" >> "$GITHUB_ENV" ;; | |
| *) echo "Unknown branch: $branch_name"; exit 1 ;; | |
| esac | |
| - name: Run tests with coverage (show failures in logs) | |
| shell: bash | |
| run: | | |
| set -o pipefail | |
| timestamp="$(date '+%Y-%m-%d_%H-%M-%S')" | |
| mkdir -p test_results | |
| log_file="test_results/${timestamp}_report.log" | |
| { | |
| echo | |
| echo "Test Cases Report" | |
| echo | |
| } | tee -a "$log_file" | |
| # Run unittest in verbose mode; mirror output to console and file | |
| python -m coverage run --source=src -m unittest discover -s tests/unit_tests -v 2>&1 | tee -a "$log_file" | |
| test_status=${PIPESTATUS[0]} | |
| echo -e "\nCoverage Report\n" | tee -a "$log_file" | |
| coverage report 2>&1 | tee -a "$log_file" | |
| exit $test_status | |
| - name: Check coverage | |
| run: coverage report --fail-under=85 | |
| # Optional: keep the log as a build artifact for easy download | |
| - name: Upload test log artifact | |
| uses: actions/upload-artifact@v4 | |
| with: | |
| name: unit-test-log | |
| path: test_results/ | |
| - name: Upload report to Azure | |
| uses: LanceMcCarthy/Action-AzureBlobUpload@v2 | |
| with: | |
| source_folder: 'test_results' | |
| destination_folder: '${{ env.output_folder }}' | |
| connection_string: ${{ secrets.AZURE_STORAGE_CONNECTION_STRING }} | |
| container_name: 'osw-validation-service' | |
| clean_destination_folder: false | |
| delete_if_exists: false |