|
| 1 | +name: Nightly Changelog |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - feature/nightlies |
| 7 | + workflow_call: |
| 8 | + inputs: |
| 9 | + branch: |
| 10 | + description: 'Branch to check for changes' |
| 11 | + required: false |
| 12 | + type: string |
| 13 | + default: 'develop' |
| 14 | + outputs: |
| 15 | + has_changes: |
| 16 | + description: "True if there were changes" |
| 17 | + value: ${{ jobs.changelog.outputs.has_changes }} |
| 18 | + changelog: |
| 19 | + description: "The changelog" |
| 20 | + value: ${{ jobs.changelog.outputs.changelog }} |
| 21 | + |
| 22 | +jobs: |
| 23 | + changelog: |
| 24 | + runs-on: ubuntu-latest |
| 25 | + outputs: |
| 26 | + has_changes: ${{ steps.changelog.outputs.has_changes }} |
| 27 | + changelog: ${{ steps.changelog.outputs.changelog }} |
| 28 | + steps: |
| 29 | + - name: Checkout repository |
| 30 | + uses: actions/checkout@v4 |
| 31 | + with: |
| 32 | + fetch-depth: 0 # Fetch all history for all branches and tags |
| 33 | + |
| 34 | + - name: Restore last commit SHA from cache |
| 35 | + id: cache-commit-sha |
| 36 | + uses: actions/cache@v4 |
| 37 | + with: |
| 38 | + path: last_commit_sha.txt |
| 39 | + # A unique key is used to force a cache miss and trigger a save post-job. |
| 40 | + key: last-commit-sha-${{ github.repository }}-${{ inputs.branch }}-${{ github.run_id }} |
| 41 | + # The most recent cache is restored using a partial key match. |
| 42 | + restore-keys: | |
| 43 | + last-commit-sha-${{ github.repository }}-${{ inputs.branch }}- |
| 44 | +
|
| 45 | + - name: Generate changelog |
| 46 | + id: changelog |
| 47 | + run: | |
| 48 | + # Get the SHA of the latest commit on the branch |
| 49 | + CURRENT_COMMIT_SHA=$(git rev-parse origin/${{ inputs.branch }}) |
| 50 | +
|
| 51 | + # Get the previously cached SHA. If the file doesn't exist, the variable will be empty. |
| 52 | + LAST_COMMIT_SHA=$(cat last_commit_sha.txt 2>/dev/null || echo "") |
| 53 | +
|
| 54 | + # If no SHA was restored, fall back to a time-based comparison. |
| 55 | + if [ -z "$LAST_COMMIT_SHA" ]; then |
| 56 | + echo "No valid cache found. Comparing against the last 24 hours." |
| 57 | + COMMIT_LOG=$(git log origin/${{ inputs.branch }} --since="24 hours ago" --pretty=format:"%s") |
| 58 | + else |
| 59 | + echo "Valid cache found. Comparing against commit $LAST_COMMIT_SHA." |
| 60 | + COMMIT_LOG=$(git log $LAST_COMMIT_SHA..$CURRENT_COMMIT_SHA --pretty=format:"%s") |
| 61 | + fi |
| 62 | +
|
| 63 | + # Set outputs |
| 64 | + if [ -z "$COMMIT_LOG" ]; then |
| 65 | + echo "has_changes=false" >> $GITHUB_OUTPUT |
| 66 | + echo "changelog=" >> $GITHUB_OUTPUT |
| 67 | + else |
| 68 | + echo "has_changes=true" >> $GITHUB_OUTPUT |
| 69 | + echo "changelog<<EOF" >> $GITHUB_OUTPUT |
| 70 | + echo "$COMMIT_LOG" >> $GITHUB_OUTPUT |
| 71 | + echo "EOF" >> $GITHUB_OUTPUT |
| 72 | + fi |
| 73 | +
|
| 74 | + # Overwrite the local file with the new SHA to be cached post-job. |
| 75 | + echo $CURRENT_COMMIT_SHA > last_commit_sha.txt |
| 76 | +
|
| 77 | + - name: Debug Outputs |
| 78 | + if: always() |
| 79 | + run: | |
| 80 | + echo "Debug Outputs:" |
| 81 | + echo "has_changes: ${{ steps.changelog.outputs.has_changes }}" |
| 82 | + echo "changelog: ${{ steps.changelog.outputs.changelog }}" |
0 commit comments