diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 2f2676bc..4a124f54 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -271,8 +271,37 @@ jobs: runs-on: ubuntu-latest needs: [tests, linter, static-analysis, performance-tests] if: github.ref == 'refs/heads/main' && github.event_name == 'push' + concurrency: + group: production-deploy-main + cancel-in-progress: true steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Check if this is the latest main commit + id: deploy_guard + run: | + latest_main_sha=$(git ls-remote origin refs/heads/main | cut -f1) + + echo "Current workflow SHA: ${{ github.sha }}" + echo "Latest main SHA: $latest_main_sha" + + if [ -z "$latest_main_sha" ]; then + echo "Unable to determine the latest main SHA." + exit 1 + fi + + if [ "$latest_main_sha" != "${{ github.sha }}" ]; then + echo "This workflow is not for the latest commit on main. Skipping deploy." + echo "should_deploy=false" >> "$GITHUB_OUTPUT" + exit 0 + fi + + echo "This workflow is for the latest commit on main. Proceeding with deploy." + echo "should_deploy=true" >> "$GITHUB_OUTPUT" + - name: Trigger deployment + if: steps.deploy_guard.outputs.should_deploy == 'true' run: | max_attempts=5 attempt=1 @@ -310,3 +339,7 @@ jobs: echo "All $max_attempts attempts failed. Giving up." exit 1 + + - name: Deployment skipped + if: steps.deploy_guard.outputs.should_deploy == 'false' + run: echo "Skipped deployment because a newer commit is already on main."