Skip to content

Commit b608ef6

Browse files
committed
Add workflow
1 parent 504e907 commit b608ef6

File tree

1 file changed

+140
-0
lines changed

1 file changed

+140
-0
lines changed
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
name: Run Xircuits Workflows Test
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
branches: "*"
8+
workflow_dispatch:
9+
10+
jobs:
11+
build-and-run:
12+
runs-on: ubuntu-latest
13+
strategy:
14+
fail-fast: false
15+
matrix:
16+
python-version: ["3.9", "3.10", "3.11"]
17+
env:
18+
TEST_XIRCUITS: |
19+
examples/image_prediction.xircuits
20+
examples/msg_trigger.xircuits
21+
examples/slack_openai.xircuits
22+
23+
steps:
24+
- name: Checkout Repository
25+
uses: actions/checkout@v3
26+
27+
- name: Set up Python ${{ matrix.python-version }}
28+
uses: actions/setup-python@v4
29+
with:
30+
python-version: ${{ matrix.python-version }}
31+
32+
- name: Create virtual environment
33+
run: |
34+
python -m venv venv
35+
echo "${{ github.workspace }}/venv/bin" >> $GITHUB_PATH
36+
source venv/bin/activate
37+
pip install --upgrade pip
38+
39+
- name: Install xircuits in virtual environment
40+
run: pip install xircuits
41+
42+
- name: Set Environment Variables
43+
run: |
44+
LIBRARY_NAME=$(echo "${GITHUB_REPOSITORY##*/}" | sed 's/-/_/g')
45+
echo "LIBRARY_NAME=$LIBRARY_NAME" >> $GITHUB_ENV
46+
COMPONENT_LIBRARY_PATH="xai_components/${LIBRARY_NAME}"
47+
echo "COMPONENT_LIBRARY_PATH=$COMPONENT_LIBRARY_PATH" >> $GITHUB_ENV
48+
if [ "${{ github.event_name }}" == "pull_request" ]; then
49+
echo "BRANCH_NAME=${{ github.head_ref }}" >> $GITHUB_ENV
50+
else
51+
echo "BRANCH_NAME=${GITHUB_REF#refs/heads/}" >> $GITHUB_ENV
52+
fi
53+
54+
- name: List Xircuits
55+
run: xircuits list
56+
57+
- name: Clone Repository
58+
run: |
59+
rm -rf ${{ env.COMPONENT_LIBRARY_PATH }}
60+
if [ "${{ github.event_name }}" == "pull_request" ]; then
61+
REPO_URL="${{ github.event.pull_request.head.repo.clone_url }}"
62+
else
63+
REPO_URL="https://github.com/${{ github.repository }}"
64+
fi
65+
git clone -b ${{ env.BRANCH_NAME }} $REPO_URL ${{ env.COMPONENT_LIBRARY_PATH }}
66+
67+
- name: Install Component Library
68+
run: |
69+
if [ -f "${{ env.COMPONENT_LIBRARY_PATH }}/requirements.txt" ]; then
70+
echo "requirements.txt found, installing dependencies..."
71+
pip install -r ${{ env.COMPONENT_LIBRARY_PATH }}/requirements.txt
72+
else
73+
echo "requirements.txt not found."
74+
fi
75+
76+
- name: Test .xircuits Workflows
77+
run: |
78+
export PYTHONPATH="${GITHUB_WORKSPACE}:${PYTHONPATH}"
79+
LOG_FILE="${GITHUB_WORKSPACE}/workflow_logs.txt"
80+
TEST_FILES=$(echo "$TEST_XIRCUITS" | tr '\n' ' ')
81+
echo "Repository: $LIBRARY_NAME" > $LOG_FILE
82+
echo "Branch: $BRANCH_NAME" >> $LOG_FILE
83+
echo -e "Testing Files:\n$TEST_FILES" >> $LOG_FILE
84+
85+
#Get the server token for GitHub secret if any
86+
TOKEN_APP="${{secrets.SLACK_APP_TOKEN}}"
87+
TOKEN_BOT="${{secrets.SLACK_BOT_TOKEN}}"
88+
89+
# Set waiting time to consider server run a success
90+
SLEEP_TIME=120
91+
CHECK_INTERVAL=5 # Check server every 5 seconds
92+
93+
IFS=' ' read -r -a FILE_ARRAY <<< "$TEST_FILES"
94+
FAIL=0
95+
if [ ${#FILE_ARRAY[@]} -eq 0 ]; then
96+
echo "No .xircuits files specified for testing." | tee -a $LOG_FILE
97+
else
98+
for file in "${FILE_ARRAY[@]}"; do
99+
FULL_PATH="${COMPONENT_LIBRARY_PATH}/${file}"
100+
if [ -f "$FULL_PATH" ]; then
101+
WORKFLOW_LOG_FILE="${FULL_PATH%.*}_workflow_log.txt"
102+
echo -e "\n\nProcessing $FULL_PATH..." | tee -a $LOG_FILE
103+
xircuits compile $FULL_PATH "${FULL_PATH%.*}.py" 2>&1 | tee -a $LOG_FILE
104+
105+
python "${FULL_PATH%.*}.py" --slack_app_token "$TOKEN_APP" --slack_bot_token "$TOKEN_BOT" 2>&1 | tee -a $WORKFLOW_LOG_FILE &
106+
WORKFLOW_PID=$!
107+
108+
TIME_PASSED=0
109+
while ps -p $WORKFLOW_PID > /dev/null && [ $TIME_PASSED -lt $SLEEP_TIME ]; do
110+
sleep $CHECK_INTERVAL
111+
TIME_PASSED=$((TIME_PASSED + CHECK_INTERVAL))
112+
done
113+
114+
if ps -p $WORKFLOW_PID > /dev/null; then
115+
kill -9 $WORKFLOW_PID
116+
echo "Test for $FULL_PATH server ran successfully for $SLEEP_TIME seconds." | tee -a $LOG_FILE
117+
else
118+
echo "Test for $FULL_PATH failed to run for $SLEEP_TIME seconds.." | tee -a $LOG_FILE
119+
FAIL=1
120+
fi
121+
else
122+
echo "Specified file $FULL_PATH does not exist." | tee -a $LOG_FILE
123+
FAIL=1
124+
fi
125+
done
126+
fi
127+
128+
if [ $FAIL -ne 0 ]; then
129+
echo "One or more workflows failed or did not finish as expected." | tee -a $LOG_FILE
130+
exit 1
131+
else
132+
echo "All workflows processed successfully." | tee -a $LOG_FILE
133+
fi
134+
135+
- name: Upload log file
136+
if: always()
137+
uses: actions/upload-artifact@v4
138+
with:
139+
name: ${{ env.LIBRARY_NAME }}-validation-workflow-${{ matrix.python-version }}
140+
path: ${{ github.workspace }}/workflow_logs.txt

0 commit comments

Comments
 (0)