Skip to content

Update Stock Map

Update Stock Map #280

name: Update Stock Map
on:
# Run daily at midnight UTC
schedule:
- cron: '0 0 * * *'
# Allow manual triggering
workflow_dispatch:
inputs:
verbose:
description: 'Enable verbose logging'
type: boolean
default: false
jobs:
update-stock-map:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
token: ${{ secrets.GITHUB_TOKEN }}
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.11'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Start API server
run: |
echo "Starting FastAPI server in background..."
python server.py &
SERVER_PID=$!
echo "SERVER_PID=$SERVER_PID" >> $GITHUB_ENV
# Wait for server to start
echo "Waiting for server to start..."
for i in {1..30}; do
if curl -f http://localhost:8000/health >/dev/null 2>&1; then
echo "Server is ready!"
break
fi
echo "Waiting... ($i/30)"
sleep 2
done
# Verify server is running
if ! curl -f http://localhost:8000/health >/dev/null 2>&1; then
echo "Failed to start server"
exit 1
fi
- name: Update stock map
run: |
echo "Updating stock map..."
# Prepare arguments
ARGS=""
if [ "${{ github.event.inputs.verbose }}" = "true" ]; then
ARGS="$ARGS --verbose"
fi
# Run the update script
python updateStocksMap.py $ARGS
- name: Stop API server
if: always()
run: |
if [ -n "$SERVER_PID" ]; then
echo "Stopping server (PID: $SERVER_PID)..."
kill $SERVER_PID || true
fi
# Kill any remaining python processes
pkill -f "python server.py" || true
- name: Check for changes
id: check_changes
run: |
git add stockmap.json
if git diff --cached --quiet; then
echo "No changes detected in stockmap.json"
echo "changes_detected=false" >> $GITHUB_OUTPUT
else
echo "Changes detected in stockmap.json"
echo "changes_detected=true" >> $GITHUB_OUTPUT
fi
- name: Commit and push changes
if: steps.check_changes.outputs.changes_detected == 'true'
run: |
git config --local user.email "action@github.com"
git config --local user.name "GitHub Action"
# Get current date for commit message
CURRENT_DATE=$(date '+%Y-%m-%d %H:%M:%S UTC')
# Count number of stocks
STOCK_COUNT=$(jq 'length' stockmap.json)
git add stockmap.json
git commit -m "🔄 Auto-update stock map - $CURRENT_DATE
- Updated stockmap.json with latest NEPSE data
- Total stocks: $STOCK_COUNT
- Updated by: GitHub Actions (scheduled)"
git push
- name: Create issue on failure
if: failure()
uses: actions/github-script@v7
with:
script: |
const title = '🚨 Stock Map Update Failed';
const body = `
## Stock Map Update Failed
**Date**: ${new Date().toISOString()}
**Workflow**: ${context.workflow}
**Run ID**: ${context.runId}
The automated stock map update process has failed. Please check the [workflow logs](${context.payload.repository.html_url}/actions/runs/${context.runId}) for details.
### Possible Issues:
- API server failed to start
- Network connectivity issues
- Changes to NEPSE API endpoints
- Rate limiting or server downtime
### Manual Steps:
1. Check if the NEPSE API is accessible
2. Verify the API endpoints still exist
3. Run the update script manually: \`python updateStocksMap.py\`
4. Check for any breaking changes in the data format
**Auto-generated by GitHub Actions**
`;
// Check if issue already exists
const issues = await github.rest.issues.listForRepo({
owner: context.repo.owner,
repo: context.repo.repo,
state: 'open',
labels: 'automation,stock-update'
});
const existingIssue = issues.data.find(issue =>
issue.title.includes('Stock Map Update Failed')
);
if (!existingIssue) {
await github.rest.issues.create({
owner: context.repo.owner,
repo: context.repo.repo,
title: title,
body: body,
labels: ['automation', 'stock-update', 'bug']
});
}
- name: Upload logs on failure
if: failure()
uses: actions/upload-artifact@v4
with:
name: stock-update-logs
path: stock_update.log
retention-days: 7
- name: Summary
if: always()
run: |
echo "## Stock Map Update Summary" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
if [ -f stockmap.json ]; then
STOCK_COUNT=$(jq 'length' stockmap.json)
echo "- **Total stocks in map**: $STOCK_COUNT" >> $GITHUB_STEP_SUMMARY
fi
echo "- **Update time**: $(date)" >> $GITHUB_STEP_SUMMARY
echo "- **Workflow**: ${{ github.workflow }}" >> $GITHUB_STEP_SUMMARY
echo "- **Triggered by**: ${{ github.event_name }}" >> $GITHUB_STEP_SUMMARY
if [ "${{ steps.check_changes.outputs.changes_detected }}" = "true" ]; then
echo "- **Status**: ✅ Changes detected and committed" >> $GITHUB_STEP_SUMMARY
else
echo "- **Status**: ℹ️ No changes detected" >> $GITHUB_STEP_SUMMARY
fi
if [ -f stock_update.log ]; then
echo "" >> $GITHUB_STEP_SUMMARY
echo "### Update Log" >> $GITHUB_STEP_SUMMARY
echo '```' >> $GITHUB_STEP_SUMMARY
tail -n 20 stock_update.log >> $GITHUB_STEP_SUMMARY
echo '```' >> $GITHUB_STEP_SUMMARY
fi