|
| 1 | +name: Monthly Line Count (cloc) |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_dispatch: # Allows manual triggering |
| 5 | + schedule: |
| 6 | + # Runs at 02:00 UTC on the 1st day of every month. |
| 7 | + - cron: '0 2 1 * *' |
| 8 | + |
| 9 | +jobs: |
| 10 | + generate-cloc-report: |
| 11 | + name: Generate cloc Report |
| 12 | + runs-on: ubuntu-latest |
| 13 | + permissions: |
| 14 | + contents: read # Needed for actions/checkout |
| 15 | + # Caches also sometimes need actions:write for some scopes, but usually not for basic cache. |
| 16 | + # If you encounter permission issues with cache, you might need to adjust. |
| 17 | + |
| 18 | + steps: |
| 19 | + - name: Checkout repository |
| 20 | + uses: actions/checkout@v4 |
| 21 | + # fetch-depth: 0 # Default is 1. If you need full history for other reasons, uncomment. |
| 22 | + # For just getting HEAD SHA, depth 1 is fine. |
| 23 | + |
| 24 | + - name: Get current commit SHA |
| 25 | + id: get_head_sha |
| 26 | + run: echo "sha=$(git rev-parse HEAD)" >> $GITHUB_OUTPUT |
| 27 | + |
| 28 | + - name: Restore last cloc-processed SHA from cache |
| 29 | + id: cache-restore-last-sha |
| 30 | + uses: actions/cache/restore@v4 # Explicitly use restore action |
| 31 | + with: |
| 32 | + path: .last_cloc_processed_sha.txt # File to store the SHA |
| 33 | + # Key specific to this workflow and branch. |
| 34 | + # We don't include the current SHA here because we want to restore the *previous* run's SHA. |
| 35 | + key: cloc-processed-sha-${{ github.ref_name }} # e.g., cloc-processed-sha-main |
| 36 | + |
| 37 | + - name: Decide if cloc needs to run |
| 38 | + id: decide_run |
| 39 | + run: | |
| 40 | + CURRENT_SHA="${{ steps.get_head_sha.outputs.sha }}" |
| 41 | + echo "Current HEAD SHA: $CURRENT_SHA" |
| 42 | + RUN_CLOC="true" # Default to run |
| 43 | +
|
| 44 | + if [[ -f .last_cloc_processed_sha.txt ]]; then |
| 45 | + LAST_PROCESSED_SHA=$(cat .last_cloc_processed_sha.txt) |
| 46 | + echo "Last cloc-processed SHA: $LAST_PROCESSED_SHA" |
| 47 | + if [[ "$CURRENT_SHA" == "$LAST_PROCESSED_SHA" ]]; then |
| 48 | + echo "No new commits on ${{ github.ref_name }} since last cloc run. Skipping." |
| 49 | + RUN_CLOC="false" |
| 50 | + else |
| 51 | + echo "New commits detected. Proceeding with cloc." |
| 52 | + fi |
| 53 | + else |
| 54 | + echo "No previous cloc-processed SHA found in cache (first run or cache expired). Proceeding with cloc." |
| 55 | + fi |
| 56 | + echo "run_cloc=${RUN_CLOC}" >> $GITHUB_OUTPUT |
| 57 | +
|
| 58 | + - name: Install cloc |
| 59 | + if: steps.decide_run.outputs.run_cloc == 'true' |
| 60 | + run: sudo apt-get update && sudo apt-get install -y cloc |
| 61 | + |
| 62 | + - name: Run cloc and generate report |
| 63 | + if: steps.decide_run.outputs.run_cloc == 'true' |
| 64 | + id: run_cloc_step # Give this step an ID |
| 65 | + run: | |
| 66 | + echo "Running cloc..." |
| 67 | + REPORT_FILE="cloc_report_$(date +%Y-%m).txt" |
| 68 | + |
| 69 | + cloc . \ |
| 70 | + --exclude-dir=.git,.github,.gradle,build,out,dist,node_modules,vendor \ |
| 71 | + --report-file="$REPORT_FILE" |
| 72 | + |
| 73 | + echo "cloc report generated: $REPORT_FILE" |
| 74 | + echo "--- cloc Report Start ---" |
| 75 | + cat "$REPORT_FILE" |
| 76 | + echo "--- cloc Report End ---" |
| 77 | + |
| 78 | + echo "REPORT_FILENAME=${REPORT_FILE}" >> $GITHUB_ENV |
| 79 | + # Create/Update the SHA file for caching for the next run |
| 80 | + echo "${{ steps.get_head_sha.outputs.sha }}" > .last_cloc_processed_sha.txt |
| 81 | + echo "Updated .last_cloc_processed_sha.txt with current SHA: ${{ steps.get_head_sha.outputs.sha }}" |
| 82 | +
|
| 83 | +
|
| 84 | + - name: Upload cloc Report Artifact |
| 85 | + if: steps.decide_run.outputs.run_cloc == 'true' |
| 86 | + uses: actions/upload-artifact@v4 |
| 87 | + with: |
| 88 | + name: cloc-monthly-report-${{ env.REPORT_FILENAME }} |
| 89 | + path: ${{ env.REPORT_FILENAME }} |
| 90 | + retention-days: 90 |
| 91 | + |
| 92 | + - name: Save last cloc-processed SHA to cache |
| 93 | + # This step will only run if cloc was executed because .last_cloc_processed_sha.txt |
| 94 | + # is only updated in the 'Run cloc and generate report' step, which is conditional. |
| 95 | + # More robust: also make this step conditional on run_cloc == 'true' |
| 96 | + if: steps.decide_run.outputs.run_cloc == 'true' && steps.run_cloc_step.outcome == 'success' |
| 97 | + uses: actions/cache/save@v4 # Explicitly use save action |
| 98 | + with: |
| 99 | + path: .last_cloc_processed_sha.txt |
| 100 | + key: cloc-processed-sha-${{ github.ref_name }} # Must match the restore key |
0 commit comments