final-vibevault-template/.autograde/workflow_templates/java.yml
2025-12-02 15:55:32 +08:00

201 lines
7.6 KiB
YAML

name: autograde-java
on:
push:
branches:
- main
workflow_dispatch:
permissions:
contents: read
pull-requests: write
jobs:
java:
runs-on: docker
container:
image: gradle:9.0-jdk21
options: --user root
timeout-minutes: 20
steps:
- name: Install dependencies (CN mirror)
run: |
set -e
# 替换 Debian/Ubuntu 源为腾讯云镜像
for f in /etc/apt/sources.list /etc/apt/sources.list.d/*.list /etc/apt/sources.list.d/*.sources; do
[ -f "$f" ] || continue
sed -i -E 's|https?://deb.debian.org|http://mirrors.cloud.tencent.com|g' "$f" || true
sed -i -E 's|https?://security.debian.org|http://mirrors.cloud.tencent.com/debian-security|g' "$f" || true
sed -i -E 's|https?://archive.ubuntu.com|http://mirrors.cloud.tencent.com|g' "$f" || true
sed -i -E 's|https?://ports.ubuntu.com|http://mirrors.cloud.tencent.com|g' "$f" || true
done
apt-get -o Acquire::Check-Valid-Until=false -o Acquire::AllowInsecureRepositories=true update -y
DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends git ca-certificates python3 python3-pip nodejs rsync
pip3 install --break-system-packages python-dotenv requests -i https://mirrors.cloud.tencent.com/pypi/simple
rm -rf /var/lib/apt/lists/*
- name: Checkout code
uses: actions/checkout@v3
with:
fetch-depth: 1
- name: Fix permissions
run: |
# Ensure workspace is owned by current user
chown -R $(whoami):$(whoami) ${{ github.workspace }} || true
- name: Fetch hidden tests (if available)
working-directory: ${{ github.workspace }}
env:
EXTERNAL_GITEA_HOST: ${{ secrets.EXTERNAL_GITEA_HOST }}
run: |
set -e
TESTS_USERNAME="${RUNNER_TESTS_USERNAME:-}"
TESTS_TOKEN="${RUNNER_TESTS_TOKEN:-}"
if [ -z "$TESTS_TOKEN" ] || [ -z "$TESTS_USERNAME" ]; then
echo "Warning: RUNNER_TESTS_USERNAME / RUNNER_TESTS_TOKEN not set, skipping private tests"
exit 0
fi
# Resolve Gitea Host
if [ -n "$EXTERNAL_GITEA_HOST" ]; then
HOST="$EXTERNAL_GITEA_HOST"
elif [ -n "$GITEA_ROOT_URL" ]; then
HOST=$(echo "$GITEA_ROOT_URL" | sed 's|https\?://||' | sed 's|/$||')
else
HOST=$(echo "${{ github.server_url }}" | sed 's|https\?://||' | cut -d'/' -f1)
fi
echo "📥 Fetching private tests repository..."
echo " Gitea host: $HOST"
# Infer organization and assignment ID from repository name
ORG=$(echo "${{ github.repository }}" | cut -d'/' -f1)
REPO_NAME=$(echo "${{ github.repository }}" | cut -d'/' -f2)
# Extract assignment ID from repo name (e.g., hw2-stu_xxx -> hw2, hw2-template -> hw2)
if echo "$REPO_NAME" | grep -q -- '-stu_'; then
ASSIGNMENT_ID=$(echo "$REPO_NAME" | sed 's/-stu_.*//')
elif echo "$REPO_NAME" | grep -q -- '-template'; then
ASSIGNMENT_ID=$(echo "$REPO_NAME" | sed 's/-template.*//')
else
ASSIGNMENT_ID="hw1" # fallback
fi
echo " Organization: $ORG"
echo " Assignment ID: $ASSIGNMENT_ID"
# Clone private test repository
AUTH_URL="http://${TESTS_USERNAME}:${TESTS_TOKEN}@${HOST}/${ORG}/${ASSIGNMENT_ID}-tests.git"
if ! git -c http.sslVerify=false clone --depth=1 "$AUTH_URL" _priv_tests 2>&1; then
echo "❌ Failed to clone ${ASSIGNMENT_ID}-tests repository!"
exit 1
fi
# Verify test directory exists
if [ ! -d "_priv_tests/java" ]; then
echo "❌ java/ directory not found in ${ASSIGNMENT_ID}-tests!"
exit 1
fi
if [ ! -d "_priv_tests/java/src/test" ]; then
echo "❌ java/src/test/ not found in ${ASSIGNMENT_ID}-tests!"
exit 1
fi
# Copy tests to src/test/
rsync -a _priv_tests/java/src/test/ src/test/
echo "✅ Private tests copied: _priv_tests/java/src/test/ → src/test/"
- name: Run tests using Gradle
working-directory: ${{ github.workspace }}
env:
LANGUAGE: java
run: |
gradle test --no-daemon || true
- name: Extract test results
working-directory: ${{ github.workspace }}
run: |
# Find JUnit XML report
XML_REPORT=$(find build/test-results/test -name "TEST-*.xml" | head -n 1)
if [ -n "$XML_REPORT" ]; then
cp "$XML_REPORT" junit.xml
echo "✅ Found JUnit report: $XML_REPORT"
else
echo "⚠️ No JUnit report found!"
touch junit.xml
fi
- name: Grade
working-directory: ${{ github.workspace }}
env:
LANGUAGE: java
run: |
python3 ./.autograde/grade.py --junit junit.xml --out grade.json --summary summary.md
- name: Prepare artifacts
working-directory: ${{ github.workspace }}
run: |
mkdir -p artifacts
cp junit.xml summary.md grade.json artifacts/ 2>/dev/null || true
- name: Create grade metadata
working-directory: ${{ github.workspace }}
env:
LANGUAGE: java
REPO: ${{ github.repository }}
run: |
if [ ! -f grade.json ]; then
echo "⚠️ grade.json not found, skipping metadata creation"
exit 0
fi
# 生成 JSON 元数据
if [ -f ./.autograde/create_grade_metadata.py ]; then
python3 ./.autograde/create_grade_metadata.py > metadata.json || echo "{}" > metadata.json
echo "✅ Grade metadata created (using create_grade_metadata.py)"
elif [ -f ./.autograde/create_minimal_metadata.py ]; then
export GRADE_TYPE=programming
python3 ./.autograde/create_minimal_metadata.py > metadata.json || echo "{}" > metadata.json
echo "✅ Grade metadata created (using create_minimal_metadata.py)"
else
echo "⚠️ No metadata creation script found, skipping"
echo "{}" > metadata.json
fi
- name: Upload metadata (teacher only)
if: env.RUNNER_METADATA_REPO != '' && env.RUNNER_METADATA_TOKEN != ''
working-directory: ${{ github.workspace }}
shell: bash
env:
METADATA_REPO: ${{ env.RUNNER_METADATA_REPO }}
METADATA_TOKEN: ${{ env.RUNNER_METADATA_TOKEN }}
METADATA_BRANCH: ${{ env.RUNNER_METADATA_BRANCH }}
STUDENT_REPO: ${{ github.repository }}
RUN_ID: ${{ github.run_id }}
COMMIT_SHA: ${{ github.sha }}
SERVER_URL: ${{ github.server_url }}
run: |
set -e
if [ ! -f metadata.json ]; then
echo "No metadata.json found, skip uploading."
exit 0
fi
python3 ./.autograde/upload_metadata.py \
--metadata-file metadata.json \
--metadata-repo "${METADATA_REPO}" \
--branch "${METADATA_BRANCH:-main}" \
--student-repo "${STUDENT_REPO}" \
--run-id "${RUN_ID}" \
--commit-sha "${COMMIT_SHA}" \
--workflow grade \
--server-url "${SERVER_URL}" \
--external-host "${EXTERNAL_GITEA_HOST}"
rm -f metadata.json