Skip to content

v2 Pre-build Checks (apigw) #70

v2 Pre-build Checks (apigw)

v2 Pre-build Checks (apigw) #70

# Pre-build Checks (API Gateway)
# 1. Unit tests with code coverage (pytest)
# 2. Code quality analysis (flake8)
# 3. Dependency analysis (vulnerabilities)
# 4. Dependency analysis (copyleft licenses)
name: v2 Pre-build Checks (apigw)
# env:
# GH_TOKEN: ${{ github.token }}
on:
# Runs when a pull request to main is being assigned
pull_request:
types: [ assigned, synchronize ]
branches:
- 'main'
paths:
- 'aiverify-apigw/**'
- 'aiverify-shared-library/**'
# Run this workflow manually from Actions tab
workflow_dispatch:
inputs:
branch_to_test:
description: 'Branch or tag to run test'
required: true
default: 'main'
type: string
# Allow one concurrent deployment
concurrency:
group: ${{ github.repository }}-${{ github.workflow }}
cancel-in-progress: true
jobs:
pre-build-checks:
# Run only when PR is assigned, even on subsequent commits (i.e. synchronize)
if: (github.event_name == 'pull_request' && github.event.pull_request.assignee != null) || github.event_name == 'workflow_dispatch' || github.event_name == 'push'
runs-on: ubuntu-latest
timeout-minutes: 40
steps:
- name: Set env variables
run: |
if [ "${{ github.event_name }}" == "pull_request" ]; then
echo "BRANCH_TO_TEST=${{ github.event.pull_request.head.ref }}" >> $GITHUB_ENV
echo "PR_NUM=#${{ github.event.pull_request.number }}" >> $GITHUB_ENV
elif [ "${{ github.event_name }}" == "push" ]; then
echo "BRANCH_TO_TEST=${{ github.ref }}" >> $GITHUB_ENV
echo "PR_NUM=#000" >> "$GITHUB_ENV"
elif [ "${{ github.event_name }}" == "workflow_dispatch" ]; then
echo "BRANCH_TO_TEST=${{ inputs.branch_to_test }}" >> $GITHUB_ENV
echo "PR_NUM=#000" >> "$GITHUB_ENV"
fi
echo "WDIR=aiverify-apigw" >> $GITHUB_ENV
echo "CI_DIR=../.ci" >> $GITHUB_ENV
- name: Checkout code
uses: actions/checkout@v4
with:
ref: ${{ env.BRANCH_TO_TEST }}
sparse-checkout: |
aiverify-apigw
aiverify-shared-library
aiverify-test-engine
common
.ci
# Setup python
- name: Setup python 3.11
uses: actions/setup-python@v5
with:
python-version: "3.11"
cache: 'pip'
- name: Setup Node.js 23
uses: actions/setup-node@v4
with:
node-version: '23'
# Install dependencies
- name: Install dependencies
working-directory: ${{ github.workspace }}/${{ env.WDIR }}
run: |
echo "Install dependencies"
cd ../aiverify-shared-library
npm install
npm run build
cd ../aiverify-apigw/aiverify-apigw-node
npm install
npm link ../../aiverify-shared-library
cd ..
pip install -e ../aiverify-test-engine[all]
pip install -e .
pip install pipdeptree > /dev/null
pipdeptree -fl
pip install pytest pytest-mock pytest-html pytest-json pytest-cov coverage faker > /dev/null
pip install flake8 flake8-html > /dev/null
# Check 1: Unit tests & coverage
- name: Unit tests with coverage
id: unit_test
working-directory: ${{ github.workspace }}/${{ env.WDIR }}
if: ${{ ! cancelled() }}
run: |
set +e
bash ${{ env.CI_DIR }}/run-test.sh aiverify_apigw -m
source ${{ env.CI_DIR }}/gen_pre_build_summ.sh test aiverify_apigw
test_status=$?
source ${{ env.CI_DIR }}/gen_pre_build_summ.sh coverage aiverify_apigw
coverage_status=$?
echo "unit_test_status=$UNITTEST_SUMMARY" >> $GITHUB_OUTPUT
echo "code_coverage_status=$COVERAGE_SUMMARY" >> $GITHUB_OUTPUT
set -e
if [ $test_status -ne 0 ] || [ $coverage_status -ne 0 ]; then
echo "STATUS=failure" >> $GITHUB_ENV
exit 1
fi
# Check 2: Code quality analysis (flake8)
- name: Code quality analysis (flake8)
id: code_quality
if: ${{ ! cancelled() }}
working-directory: ${{ github.workspace }}/${{ env.WDIR }}
run: |
set +e
bash ${{ env.CI_DIR }}/run-flake8.sh aiverify_apigw
source ${{ env.CI_DIR }}/gen_pre_build_summ.sh lint aiverify_apigw
lint_status=$?
echo "code_quality_status=$LINT_SUMMARY" >> $GITHUB_OUTPUT
set -e
if [ $lint_status -ne 0 ]; then
echo "STATUS=failure" >> $GITHUB_ENV
exit $lint_status
fi
# Check 3: Dependency vulnerability & license analysis (pip-audit)
- name: Dependency analysis (vulnerabilities & licenses)
id: dependency_analysis
if: ${{ ! cancelled() }}
working-directory: ${{ github.workspace }}/${{ env.WDIR }}
run: |
set +e
bash ${{ env.CI_DIR }}/run-pip-audit.sh aiverify_apigw
source ${{ env.CI_DIR }}/gen_pre_build_summ.sh dependency aiverify_apigw
dep_status=$?
source ${{ env.CI_DIR }}/gen_pre_build_summ.sh license aiverify_apigw
lic_status=$?
echo "dependency_status=$DEPENDENCY_SUMMARY" >> $GITHUB_OUTPUT
echo "license_status=$LICENSE_SUMMARY" >> $GITHUB_OUTPUT
set -e
if [ $dep_status -ne 0 ] || [ $lic_status -ne 0 ]; then
echo "STATUS=failure" >> $GITHUB_ENV
exit 1
fi
shell: bash
# Send status to Slack
- name: Send slack notification
if: ${{ ! cancelled() }}
uses: slackapi/[email protected]
with:
payload: |
{
"workflow": "${{ github.repository }} | ${{ github.workflow }} | ${{ inputs.pr_num }} | ${{ env.ALGO_NAME }}",
"status": "${{ env.STATUS }}",
"details": "${{ steps.unit_test.outputs.unit_test_status }} | ${{ steps.unit_test.outputs.code_coverage_status }} | ${{ steps.code_quality.outputs.code_quality_status }} | ${{ steps.dependency_analysis.outputs.dependency_status }} | ${{ steps.dependency_analysis.outputs.license_status }}",
"ref": "${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}"
}
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_CI }}