Update run-workflow-tests.yml #3
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: Run Xircuits Workflows Test | |
on: | |
push: | |
branches: [ main ] | |
pull_request: | |
branches: "*" | |
workflow_dispatch: | |
jobs: | |
build-and-run: | |
runs-on: ubuntu-latest | |
strategy: | |
fail-fast: false | |
matrix: | |
python-version: ["3.9", "3.10", "3.11"] | |
env: | |
TEST_XIRCUITS: | | |
examples/image_prediction.xircuits | |
examples/msg_trigger.xircuits | |
steps: | |
- name: Checkout Repository | |
uses: actions/checkout@v3 | |
- name: Set up Python ${{ matrix.python-version }} | |
uses: actions/setup-python@v4 | |
with: | |
python-version: ${{ matrix.python-version }} | |
- name: Create virtual environment | |
run: | | |
python -m venv venv | |
echo "${{ github.workspace }}/venv/bin" >> $GITHUB_PATH | |
source venv/bin/activate | |
pip install --upgrade pip | |
- name: Install xircuits in virtual environment | |
run: pip install xircuits | |
- name: Install openai component library | |
run: xircuits install openai | |
- name: Set Environment Variables | |
run: | | |
LIBRARY_NAME=$(echo "${GITHUB_REPOSITORY##*/}" | sed 's/-/_/g') | |
echo "LIBRARY_NAME=$LIBRARY_NAME" >> $GITHUB_ENV | |
COMPONENT_LIBRARY_PATH="xai_components/${LIBRARY_NAME}" | |
echo "COMPONENT_LIBRARY_PATH=$COMPONENT_LIBRARY_PATH" >> $GITHUB_ENV | |
if [ "${{ github.event_name }}" == "pull_request" ]; then | |
echo "BRANCH_NAME=${{ github.head_ref }}" >> $GITHUB_ENV | |
else | |
echo "BRANCH_NAME=${GITHUB_REF#refs/heads/}" >> $GITHUB_ENV | |
fi | |
- name: List Xircuits | |
run: xircuits list | |
- name: Clone Repository | |
run: | | |
rm -rf ${{ env.COMPONENT_LIBRARY_PATH }} | |
if [ "${{ github.event_name }}" == "pull_request" ]; then | |
REPO_URL="${{ github.event.pull_request.head.repo.clone_url }}" | |
else | |
REPO_URL="https://github.com/${{ github.repository }}" | |
fi | |
git clone -b ${{ env.BRANCH_NAME }} $REPO_URL ${{ env.COMPONENT_LIBRARY_PATH }} | |
- name: Install Component Library | |
run: | | |
if [ -f "${{ env.COMPONENT_LIBRARY_PATH }}/requirements.txt" ]; then | |
echo "requirements.txt found, installing dependencies..." | |
pip install -r ${{ env.COMPONENT_LIBRARY_PATH }}/requirements.txt | |
else | |
echo "requirements.txt not found." | |
fi | |
- name: Test .xircuits Workflows | |
run: | | |
export PYTHONPATH="${GITHUB_WORKSPACE}:${PYTHONPATH}" | |
LOG_FILE="${GITHUB_WORKSPACE}/workflow_logs.txt" | |
TEST_FILES=$(echo "$TEST_XIRCUITS" | tr '\n' ' ') | |
echo "Repository: $LIBRARY_NAME" > $LOG_FILE | |
echo "Branch: $BRANCH_NAME" >> $LOG_FILE | |
echo -e "Testing Files:\n$TEST_FILES" >> $LOG_FILE | |
#Get the server token for GitHub secret if any | |
TOKEN_APP="${{secrets.SLACK_APP_TOKEN}}" | |
TOKEN_BOT="${{secrets.SLACK_BOT_TOKEN}}" | |
# Set waiting time to consider server run a success | |
SLEEP_TIME=120 | |
CHECK_INTERVAL=5 # Check server every 5 seconds | |
IFS=' ' read -r -a FILE_ARRAY <<< "$TEST_FILES" | |
FAIL=0 | |
if [ ${#FILE_ARRAY[@]} -eq 0 ]; then | |
echo "No .xircuits files specified for testing." | tee -a $LOG_FILE | |
else | |
for file in "${FILE_ARRAY[@]}"; do | |
FULL_PATH="${COMPONENT_LIBRARY_PATH}/${file}" | |
if [ -f "$FULL_PATH" ]; then | |
WORKFLOW_LOG_FILE="${FULL_PATH%.*}_workflow_log.txt" | |
echo -e "\n\nProcessing $FULL_PATH..." | tee -a $LOG_FILE | |
xircuits compile $FULL_PATH "${FULL_PATH%.*}.py" 2>&1 | tee -a $LOG_FILE | |
python "${FULL_PATH%.*}.py" --slack_app_token "$TOKEN_APP" --slack_bot_token "$TOKEN_BOT" 2>&1 | tee -a $WORKFLOW_LOG_FILE & | |
WORKFLOW_PID=$! | |
TIME_PASSED=0 | |
while ps -p $WORKFLOW_PID > /dev/null && [ $TIME_PASSED -lt $SLEEP_TIME ]; do | |
sleep $CHECK_INTERVAL | |
TIME_PASSED=$((TIME_PASSED + CHECK_INTERVAL)) | |
done | |
if ps -p $WORKFLOW_PID > /dev/null; then | |
kill -9 $WORKFLOW_PID | |
echo "Test for $FULL_PATH server ran successfully for $SLEEP_TIME seconds." | tee -a $LOG_FILE | |
else | |
echo "Test for $FULL_PATH failed to run for $SLEEP_TIME seconds.." | tee -a $LOG_FILE | |
FAIL=1 | |
fi | |
else | |
echo "Specified file $FULL_PATH does not exist." | tee -a $LOG_FILE | |
FAIL=1 | |
fi | |
done | |
fi | |
if [ $FAIL -ne 0 ]; then | |
echo "One or more workflows failed or did not finish as expected." | tee -a $LOG_FILE | |
exit 1 | |
else | |
echo "All workflows processed successfully." | tee -a $LOG_FILE | |
fi | |
- name: Upload log file | |
if: always() | |
uses: actions/upload-artifact@v4 | |
with: | |
name: ${{ env.LIBRARY_NAME }}-validation-workflow-${{ matrix.python-version }} | |
path: ${{ github.workspace }}/workflow_logs.txt |