BFD-3524: Create CI-Lambda to enforce unit testing as gate for merges #29
Workflow file for this run
This file contains 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: CI - Python | |
on: | |
pull_request: | |
types: [opened, synchronize, reopened] | |
paths: | |
- '**/*.py' | |
- '**/*requirement*.txt' | |
merge_group: | |
branches: | |
- master | |
# paths: | |
# - '**/*.py' | |
# - '**/*requirement*.txt' | |
workflow_dispatch: | |
env: | |
BASE_COMMIT: "" | |
GH_TOKEN: ${{ github.token }} | |
## | |
## PROJECT MATRIX REFERENCE: | |
## [ | |
## { projectName: "sftp-outbound-transfer-lambda", projectRoot: "ops/terraform/services/eft/lambda_src/sftp_outbound_transfer", testsRoot: "ops/terraform/services/eft/lambda_src/sftp_outbound_transfer/tests", pythonVersion: "3.12" }, | |
## { projectName: "update-pipeline-slis-lambda", projectRoot: "ops/terraform/services/pipeline/modules/bfd_pipeline_slis/lambda_src", testsRoot: "ops/terraform/services/pipeline/modules/bfd_pipeline_slis/lambda_src/tests", pythonVersion: "3.11" } | |
## ] | |
jobs: | |
python-check: | |
runs-on: ubuntu-latest | |
outputs: | |
jobstatus: ${{ join(steps.*.conclusion) }} | |
MATRIX: ${{ steps.match-proj-deltas.outputs.MATRIX }} | |
steps: | |
- name: Checkout code | |
id: code-checkout | |
uses: actions/checkout@v4 | |
with: | |
fetch-depth: 2 | |
- name: Establish Project Config | |
id: create-config | |
run: | | |
FN=PRJLIST.json | |
touch "${{ runner.temp }}/${FN}" | |
echo "PRJCONFIG=${FN}" >> $GITHUB_OUTPUT | |
- name: Get changed directory list as CSV with count | |
id: changed-dirs | |
uses: tj-actions/changed-files@v44 | |
with: | |
dir_names: "true" | |
separator: "," | |
- name: Capture Modified Folders with Count | |
id: validate-testworthy-changes | |
run: | | |
UNIQ_FOLDERS=${{ runner.temp }}/UNIQFLDR.txt | |
echo ${{ steps.changed-dirs.outputs.all_changed_files }} >> ${UNIQ_FOLDERS} | |
UNIQ64=$(cat ${UNIQ_FOLDERS}|base64) | |
echo "FOLDERLISTCSV=${UNIQ64}" >> $GITHUB_OUTPUT | |
echo "FOLDERCT=${{ steps.changed-dirs.outputs.all_changed_files_count }}" | |
- name: Define Managed Project Roots | |
id: define-proj-list | |
uses: jsdaniell/[email protected] | |
with: | |
name: ${{ steps.create-config.outputs.PRJCONFIG }} | |
dir: "${{ runner.temp }}/" | |
json: '[ { "projectName": "sftp-outbound-transfer-lambda", "projectRoot": "ops/terraform/services/eft/lambda_src/sftp_outbound_transfer", "testsRoot": "ops/terraform/services/eft/lambda_src/sftp_outbound_transfer/tests", "pythonVersion": "3.12" }, { "projectName": "update-pipeline-slis-lambda", "projectRoot": "ops/terraform/services/pipeline/modules/bfd_pipeline_slis/lambda_src", "testsRoot": "ops/terraform/services/pipeline/modules/bfd_pipeline_slis/lambda_src/tests", "pythonVersion": "3.11" } ]' | |
- name: Match Changed Folder List to Project Roots | |
id: match-proj-deltas | |
run: | | |
PROJ_JSON="${{ runner.temp }}/${{ steps.create-config.outputs.PRJCONFIG }}" | |
PROJ_ROOT_LIST=$(cat ${PROJ_JSON} | jq -c '[ .[]|.projectRoot ]') | |
FOLDERLISTCSV64=${{ steps.validate-testworthy-changes.outputs.FOLDERLISTCSV }} | |
FOLDERLISTJSON=$(echo ${FOLDERLISTCSV64}|base64 -d| awk '{ n=split($0,a,","); { for(i in a) printf "%s\n",a[i]; } }'|sort|uniq| jq --raw-input '.' | jq --slurp) | |
PROJECTMATCHJSON=$(echo ${FOLDERLISTJSON} | jq '.[]'|sed 's/"//g'|while read pathnm; do cat ${PROJ_JSON}| jq --arg match $pathnm '.[]|select(.projectRoot|startswith($match))'; done | jq '[.]') | |
echo "MATRIX="$PROJECTMATCHJSON >> $GITHUB_OUTPUT | |
matrix-pytest: | |
runs-on: ubuntu-latest | |
needs: python-check | |
if: ${{ !contains(needs.python-check.outputs.jobstatus, 'failure') }} | |
strategy: | |
matrix: | |
projects: ${{ fromJSON(needs.python-check.outputs.MATRIX) }} | |
steps: | |
- name: Setup Python Tests for affected folders | |
id: install-pytest-set | |
uses: actions/[email protected] | |
with: | |
python-version: ${{ matrix.projects.pythonVersion }} | |
- name: Run Python Tests per project | |
id: run-pytest-set | |
run: | | |
echo "Testing ${{ matrix.projects.projectRoot }}" | |
# create virtual environment for each DIR and execute tests | |
# python -m venv venv | |
# source venv/bin/activate | |
pip install --upgrade pip | |
pip install -r ${{ matrix.projects.projectRoot }}/requirements.txt | |
pip install pytest | |
pytest ${{ matrix.projects.projectRoot }} |