Skip to content

Commit 37411af

Browse files
markpollackchedim
authored andcommitted
Optimize CI/CD builds by skipping when no new commits exist
Add change detection to continuous-integration.yml workflow to prevent unnecessary builds when no commits have been made since the last successful build. This reduces resource usage while maintaining the regular build schedule. Key changes: - Check last successful build timestamp via GitHub API - Compare with latest commit timestamp - Skip all build steps if no new commits found - Add clear feedback when builds are skipped - Maintain existing concurrency controls Signed-off-by: [email protected]
1 parent 1b300aa commit 37411af

File tree

1 file changed

+52
-5
lines changed

1 file changed

+52
-5
lines changed

.github/workflows/continuous-integration.yml

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ jobs:
2929
name: Build branch
3030
runs-on: ubuntu-latest
3131
if: ${{ github.repository_owner == 'spring-projects' }}
32+
permissions:
33+
contents: read
34+
actions: read
3235
concurrency:
3336
group: continuous-integration-${{ github.ref }}
3437
cancel-in-progress: true # Skip if another build is running - next cron will trigger soon
@@ -43,20 +46,63 @@ jobs:
4346
- name: Checkout source code
4447
uses: actions/checkout@v4
4548

49+
- name: Check for new commits since last build
50+
id: changes
51+
run: |
52+
# Get the timestamp of the latest commit on this branch
53+
LATEST_COMMIT_TIME=$(git log -1 --format="%ct" HEAD)
54+
echo "Latest commit timestamp: $LATEST_COMMIT_TIME ($(date -d @$LATEST_COMMIT_TIME))"
55+
56+
# Get the last successful build time using GitHub API
57+
# Look for the most recent successful CI build on this branch
58+
LAST_SUCCESS=$(curl -s \
59+
-H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
60+
-H "Accept: application/vnd.github.v3+json" \
61+
"https://api.github.com/repos/${{ github.repository }}/actions/runs?branch=${{ github.ref_name }}&status=success&per_page=50" \
62+
| jq -r '.workflow_runs[] | select(.name == "CI/CD build" and .conclusion == "success") | .created_at' \
63+
| head -1)
64+
65+
if [ -n "$LAST_SUCCESS" ] && [ "$LAST_SUCCESS" != "null" ]; then
66+
LAST_SUCCESS_TIME=$(date -d "$LAST_SUCCESS" +%s)
67+
echo "Last successful build: $LAST_SUCCESS_TIME ($(date -d @$LAST_SUCCESS_TIME))"
68+
69+
if [ "$LATEST_COMMIT_TIME" -le "$LAST_SUCCESS_TIME" ]; then
70+
echo "No new commits since last successful build - skipping"
71+
echo "skip_build=true" >> $GITHUB_OUTPUT
72+
exit 0
73+
else
74+
echo "New commits found since last successful build - proceeding"
75+
echo "skip_build=false" >> $GITHUB_OUTPUT
76+
fi
77+
else
78+
echo "No previous successful build found - proceeding with build"
79+
echo "skip_build=false" >> $GITHUB_OUTPUT
80+
fi
81+
82+
- name: Build skipped - no new commits
83+
if: steps.changes.outputs.skip_build == 'true'
84+
run: |
85+
echo "✅ CI/CD build skipped - no new commits since last successful build"
86+
echo "This saves resources and reduces unnecessary builds"
87+
echo "The next scheduled build will check again for new commits"
88+
4689
- name: Free Disk Space
90+
if: steps.changes.outputs.skip_build != 'true'
4791
uses: jlumbroso/free-disk-space@54081f138730dfa15788a46383842cd2f914a1be # v1.3.1
4892
with:
4993
large-packages: false
5094
docker-images: false
5195

5296
- name: Set up JDK 17
97+
if: steps.changes.outputs.skip_build != 'true'
5398
uses: actions/setup-java@v4
5499
with:
55100
java-version: '17'
56101
distribution: 'temurin'
57102
cache: 'maven'
58103

59104
- name: Configure Testcontainers
105+
if: steps.changes.outputs.skip_build != 'true'
60106
run: |
61107
echo "testcontainers.reuse.enable=true" > $HOME/.testcontainers.properties
62108
@@ -66,6 +112,7 @@ jobs:
66112
# key: docker-${{ runner.os }}-${{ hashFiles('**/OllamaImage.java') }}
67113

68114
- name: Build and test with Maven
115+
if: steps.changes.outputs.skip_build != 'true'
69116
env:
70117
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
71118
SPRING_AI_OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
@@ -85,20 +132,20 @@ jobs:
85132
fi
86133
87134
- name: Generate Java docs
88-
if: github.ref == 'refs/heads/main'
135+
if: github.ref == 'refs/heads/main' && steps.changes.outputs.skip_build != 'true'
89136
run: ./mvnw --batch-mode javadoc:aggregate
90137

91138
- name: Generate assembly
92-
if: github.ref == 'refs/heads/main'
139+
if: github.ref == 'refs/heads/main' && steps.changes.outputs.skip_build != 'true'
93140
working-directory: spring-ai-docs
94141
run: ../mvnw --batch-mode assembly:single
95142

96143
- name: Capture project version
97-
if: github.ref == 'refs/heads/main'
144+
if: github.ref == 'refs/heads/main' && steps.changes.outputs.skip_build != 'true'
98145
run: echo PROJECT_VERSION=$(mvn help:evaluate -Dexpression=project.version --quiet -DforceStdout) >> $GITHUB_ENV
99146

100147
- name: Setup SSH key
101-
if: github.ref == 'refs/heads/main'
148+
if: github.ref == 'refs/heads/main' && steps.changes.outputs.skip_build != 'true'
102149
env:
103150
DOCS_SSH_KEY: ${{ secrets.DOCS_SSH_KEY }}
104151
DOCS_SSH_HOST_KEY: ${{ secrets.DOCS_SSH_HOST_KEY }}
@@ -109,7 +156,7 @@ jobs:
109156
echo "$DOCS_SSH_HOST_KEY" > "$HOME/.ssh/known_hosts"
110157
111158
- name: Deploy docs
112-
if: github.ref == 'refs/heads/main'
159+
if: github.ref == 'refs/heads/main' && steps.changes.outputs.skip_build != 'true'
113160
env:
114161
DOCS_HOST: ${{ secrets.DOCS_HOST }}
115162
DOCS_PATH: ${{ secrets.DOCS_PATH }}

0 commit comments

Comments
 (0)