|
| 1 | +# This workflow uses actions that are not certified by GitHub. |
| 2 | +# They are provided by a third-party and are governed by |
| 3 | +# separate terms of service, privacy policy, and support |
| 4 | +# documentation. |
| 5 | + |
| 6 | +# Workflow to register the DESCARTES Course with the UMH DESCARTES Course Sites page. |
| 7 | +name: Register Descartes Course Site |
| 8 | + |
| 9 | +on: |
| 10 | + # Runs on pushes targeting the default branch |
| 11 | + push: |
| 12 | + branches: ["main"] |
| 13 | + # Allows you to run this workflow manually from the Actions tab |
| 14 | + workflow_dispatch: |
| 15 | + |
| 16 | +# Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages |
| 17 | +permissions: |
| 18 | + contents: write |
| 19 | + pull-requests: write |
| 20 | + id-token: write |
| 21 | + |
| 22 | +# Allow only one concurrent deployment, skipping runs queued between the run in-progress and latest queued. |
| 23 | +# However, do NOT cancel in-progress runs as we want to allow these production deployments to complete. |
| 24 | +concurrency: |
| 25 | + group: "descartes" |
| 26 | + cancel-in-progress: false |
| 27 | + |
| 28 | +jobs: |
| 29 | + # Deployment job |
| 30 | + deploy: |
| 31 | + runs-on: ubuntu-latest |
| 32 | + steps: |
| 33 | + - name: Checkout current repository |
| 34 | + uses: actions/checkout@v4 |
| 35 | + with: |
| 36 | + path: current-repo |
| 37 | + |
| 38 | + - name: Checkout Descartes Course repository |
| 39 | + uses: actions/checkout@v4 |
| 40 | + with: |
| 41 | + repository: uhm-descartes/descartes-modules |
| 42 | + path: descartes-modules |
| 43 | + token: ${{ secrets.DESCARTES_TOKEN }} |
| 44 | + |
| 45 | + - name: Update CSV and Create Pull Request |
| 46 | + env: |
| 47 | + GH_TOKEN: ${{ secrets.DESCARTES_TOKEN }} |
| 48 | + COURSE_URL: ${{ steps.deployment.outputs.page_url }} |
| 49 | + COURSE_REPOSITORY: ${{ github.repository }} |
| 50 | + DESCARTES_COURSES_DIR: "descartes-modules" |
| 51 | + DESCARTES_COURSES_CSV: "course-sites/descartes-courses.csv" |
| 52 | + run: | |
| 53 | + echo "Extract course name, course full name, and description from _config.yml" |
| 54 | + if [ -f "current-repo/_config.yml" ]; then |
| 55 | + COURSE_NAME=$(grep "^morea_course:" current-repo/_config.yml | sed 's/morea_course: *//' | tr -d '"' | tr '[:lower:]' '[:upper:]') |
| 56 | + echo "Found course name: $COURSE_NAME" |
| 57 | + COURSE_FULL_NAME=$(grep "^morea_course_name:" current-repo/_config.yml | sed 's/morea_course_name: *//' | tr -d '"') |
| 58 | + echo "Found course full name: $COURSE_FULL_NAME" |
| 59 | + COURSE_DESCRIPTION=$(grep "^morea_description:" current-repo/_config.yml | sed 's/morea_description: *//' | tr -d '"') |
| 60 | + echo "Found course description: $COURSE_DESCRIPTION" |
| 61 | + COURSE_PREREQUISITES=$(grep "^morea_prerequisites:" current-repo/_config.yml | sed 's/morea_prerequisites: *//' | tr -d '"') |
| 62 | + echo "Found course prerequisites: $COURSE_PREREQUISITES" |
| 63 | + else |
| 64 | + echo "ERROR: _config.yml not found!" |
| 65 | + exit 11 |
| 66 | + fi |
| 67 | +
|
| 68 | + cd $DESCARTES_COURSES_DIR |
| 69 | + |
| 70 | + echo "Configure git" |
| 71 | + git config --local user.name "UHM DESCARTES Action Bot" |
| 72 | + git config --local user.email "uhmnrt@hawaii.edu" |
| 73 | + git config --local user.password "$GH_TOKEN" |
| 74 | + |
| 75 | + echo "Check if the exact entry already exists (with full name and description)" |
| 76 | + EXPECTED_ENTRY="\"$COURSE_NAME\",\"$COURSE_URL\",\"$COURSE_FULL_NAME\",\"$COURSE_DESCRIPTION\",\"$COURSE_PREREQUISITES\",\"$COURSE_REPOSITORY\"" |
| 77 | + if grep -Fxq "$EXPECTED_ENTRY" "$DESCARTES_COURSES_CSV"; then |
| 78 | + echo "Entry '$EXPECTED_ENTRY' already exists in CSV file. Skipping update." |
| 79 | + exit 0 |
| 80 | + fi |
| 81 | + |
| 82 | + echo "Check if an entry with same course name and URL exists (but potentially different full name or description or prerequisites or repository)" |
| 83 | + PARTIAL_ENTRY="$COURSE_NAME,$COURSE_URL" |
| 84 | + if grep -q "^$PARTIAL_ENTRY," "$DESCARTES_COURSES_CSV"; then |
| 85 | + echo "Found entry with same course and URL but different full name/description/prerequisites/repository. Will update entry." |
| 86 | + NEEDS_UPDATE=true |
| 87 | + elif grep -q "^$COURSE_NAME," "$DESCARTES_COURSES_CSV"; then |
| 88 | + echo "Found entry with same course name but different URL/full name/description/prerequisites/repository. Will update entry." |
| 89 | + NEEDS_UPDATE=true |
| 90 | + else |
| 91 | + echo "No existing entry found. Will add new entry." |
| 92 | + NEEDS_UPDATE=true |
| 93 | + fi |
| 94 | + |
| 95 | + echo "Create a new branch" |
| 96 | + BRANCH_NAME="update-${COURSE_NAME// /}-$(date +%s)" |
| 97 | + git checkout -b "$BRANCH_NAME" |
| 98 | + |
| 99 | + if grep -q "^$COURSE_NAME," "$DESCARTES_COURSES_CSV"; then |
| 100 | + echo "Update existing entry with full name and description" |
| 101 | + sed -i.bak "s|^$COURSE_NAME,.*|$EXPECTED_ENTRY|g" "$DESCARTES_COURSES_CSV" |
| 102 | + echo "Updated existing $COURSE_NAME entry with: $COURSE_URL, full name: $COURSE_FULL_NAME, and description: $COURSE_DESCRIPTION" |
| 103 | + else |
| 104 | + echo "Append new entry with full name and description" |
| 105 | + echo -e "\n$EXPECTED_ENTRY" >> "$DESCARTES_COURSES_CSV" |
| 106 | + sed -i '/^\s*$/d' "$DESCARTES_COURSES_CSV" |
| 107 | + echo "Added new $COURSE_NAME entry with: $COURSE_URL, full name: $COURSE_FULL_NAME, and description: $COURSE_DESCRIPTION" |
| 108 | + fi |
| 109 | + |
| 110 | + echo "Check if there are changes" |
| 111 | + if git diff --quiet; then |
| 112 | + echo "No changes to commit" |
| 113 | + exit 0 |
| 114 | + fi |
| 115 | + |
| 116 | + echo "Commit and push changes" |
| 117 | + git add "$DESCARTES_COURSES_CSV" |
| 118 | + git commit -m "Update $COURSE_NAME deploy URL, full name, and description" |
| 119 | + git push origin "$BRANCH_NAME" |
| 120 | + |
| 121 | + echo "Create pull request using GitHub CLI" |
| 122 | + gh pr create \ |
| 123 | + --title "Update $COURSE_NAME deploy URL, full name, and description" \ |
| 124 | + --body "Automatically generated PR to update $COURSE_NAME course site URL to: $COURSE_URL with full name: $COURSE_FULL_NAME and description: $COURSE_DESCRIPTION" \ |
| 125 | + --head "$BRANCH_NAME" \ |
| 126 | + --base main |
0 commit comments