Make COVID Forecast Submission #4
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: Make COVID Forecast Submission | |
on: | |
schedule: | |
# Every Wednesday at 6pm CET (17:00 UTC) | |
- cron: '0 17 * * 3' | |
workflow_dispatch: # Allow manual triggering | |
jobs: | |
submit-forecast: | |
runs-on: ubuntu-latest | |
permissions: | |
contents: write | |
pull-requests: write | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v4 | |
- name: Set up Python | |
uses: actions/setup-python@v4 | |
with: | |
python-version: '3.10' | |
- name: Install dependencies | |
run: | | |
python -m pip install --upgrade pip | |
pip install requests pandas numpy | |
- name: Create required directories | |
run: | | |
mkdir -p covid/submissions | |
- name: Run covid forecasting script | |
run: python covid/run-covid-forecasts.py | |
env: | |
PYTHONPATH: ${{ github.workspace }} | |
- name: Commit forecasts to repository | |
run: | | |
# Configure git | |
git config user.name "github-actions[bot]" | |
git config user.email "github-actions[bot]@users.noreply.github.com" | |
# Pull latest changes | |
git pull origin main --rebase | |
# Add and commit the new forecasts | |
git add covid/submissions/ | |
# Only commit if there are changes | |
if git diff --staged --quiet; then | |
echo "No new forecasts to commit" | |
else | |
git commit -m "Store covid forecasts $(date +%Y-%m-%d)" | |
# Try to push, if it fails due to conflicts, pull and push again | |
git push || (git pull --rebase origin main && git push) | |
fi | |
- name: Fork and sync target repository | |
run: | | |
# Install GitHub CLI | |
gh auth login --with-token <<< "${{ secrets.PRIVATE_ACCESS_TOKEN }}" | |
# Check if fork exists and is accessible | |
if ! gh repo view ${{ github.actor }}/covid19-forecast-hub >/dev/null 2>&1; then | |
# Fork doesn't exist or is inaccessible, create it | |
FORK_DIR=$(gh repo fork CDCgov/covid19-forecast-hub --clone=true --remote=true | grep -o 'Cloning into '"'"'\K[^'"'"']*') | |
else | |
# Fork exists, clone it directly | |
git clone "https://${{ secrets.PRIVATE_ACCESS_TOKEN }}@github.com/${{ github.actor }}/covid19-forecast-hub.git" | |
FORK_DIR="covid19-forecast-hub" | |
fi | |
# Navigate to the forked repository directory | |
pushd "$FORK_DIR" | |
# Add upstream remote if it doesn't exist | |
git remote add upstream https://github.com/CDCgov/covid19-forecast-hub.git 2>/dev/null || true | |
# Set the origin remote with authentication token | |
git remote set-url origin "https://${{ secrets.PRIVATE_ACCESS_TOKEN }}@github.com/${{ github.actor }}/covid19-forecast-hub.git" | |
# Fetch and sync with upstream | |
git fetch upstream | |
git checkout main | |
git reset --hard upstream/main | |
git push origin main --force | |
popd | |
- name: Copy forecast files | |
run: | | |
# Get the fork directory name | |
FORK_DIR=$(find . -maxdepth 1 -type d -name "covid19-forecast-hub*" -print -quit) | |
# Create target directory if it doesn't exist | |
mkdir -p "$FORK_DIR/model-output/Metaculus-cp" | |
# Copy new forecasts | |
cp -r covid/submissions/* "$FORK_DIR/model-output/Metaculus-cp/" | |
- name: Create Pull Request | |
run: | | |
# Get the fork directory name | |
FORK_DIR=$(find . -maxdepth 1 -type d -name "covid19-forecast-hub*" -print -quit) | |
cd "$FORK_DIR" | |
# Setup git config | |
git config user.name "github-actions[bot]" | |
git config user.email "github-actions[bot]@users.noreply.github.com" | |
# Create a new branch with timestamp to ensure uniqueness | |
BRANCH_NAME="covid-forecast-update-$(date +%Y%m%d-%H%M%S)" | |
git checkout -b $BRANCH_NAME | |
# Add and commit changes | |
git add model-output/Metaculus-cp/ | |
# Only commit if there are changes | |
if git diff --staged --quiet; then | |
echo "No changes to commit" | |
exit 0 | |
else | |
git commit -m "Update covid forecasts $(date +%Y-%m-%d)" | |
# Set the remote URL with authentication token | |
git remote set-url origin "https://${{ secrets.PRIVATE_ACCESS_TOKEN }}@github.com/${{ github.actor }}/covid19-forecast-hub.git" | |
# Force push to fork with the new unique branch | |
git push -f origin $BRANCH_NAME | |
# Create PR | |
gh pr create \ | |
--title "Update covid forecasts $(date +%Y-%m-%d)" \ | |
--body "Automated covid forecast submission from Metaculus" \ | |
--repo CDCgov/covid19-forecast-hub \ | |
--base main \ | |
--head "${{ github.actor }}:$BRANCH_NAME" | |
fi | |
env: | |
PRIVATE_ACCESS_TOKEN: ${{ secrets.PRIVATE_ACCESS_TOKEN }} |