From 05602d55fb76a8469363d6e31a59b876cffb22e3 Mon Sep 17 00:00:00 2001 From: Michael Zalimeni Date: Tue, 28 Nov 2023 17:07:46 -0500 Subject: [PATCH] ci: fix test failure Slack notifications - Skip notifications for cancelled workflows. Cancellation can be manual or caused by branch concurrency limits. - Fix multi-line JSON parsing error by only printing the summary line of the commit message. We do not need more than this in Slack. - Update Slack webhook name to match purpose. --- .github/workflows/go-tests.yml | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/.github/workflows/go-tests.yml b/.github/workflows/go-tests.yml index 5421c4a8d0..f7dc6e2cb6 100644 --- a/.github/workflows/go-tests.yml +++ b/.github/workflows/go-tests.yml @@ -498,20 +498,36 @@ jobs: steps: - name: evaluate upstream job results run: | + NEEDS_JSON='${{ toJSON(needs) }}' + + # if failure (not cancelled), notify Slack + if echo "${NEEDS_JSON}" | grep -E -i '\"result\": \"(failure)\"'; then + printf "Tests failed, notifying Slack" + echo "FAILED_TESTS=true" >> $GITHUB_ENV + fi + # exit 1 if failure or cancelled result for any upstream job - if printf '${{ toJSON(needs) }}' | grep -E -i '\"result\": \"(failure|cancelled)\"'; then + # this ensures that we fail the PR check regardless of cancellation, rather than skip-passing it + # see https://docs.github.com/en/actions/using-jobs/using-conditions-to-control-job-execution#overview + if echo "${NEEDS_JSON}" | grep -E -i '\"result\": \"(failure|cancelled)\"'; then printf "Tests failed or workflow cancelled:\n\n${{ toJSON(needs) }}" exit 1 fi + - name: Set Slack commit message summary + # avoid duplicating branch filter logic below and enable easier message content debugging + # always() ensures this runs even if the test eval step exits 1 + if: always() + run: echo "COMMIT_MESSAGE_SUMMARY=$(echo ${{ github.event.head_commit.message }} | head -n 1)" >> $GITHUB_ENV - name: Notify Slack - if: ${{ failure() && (github.ref_name == 'main' || startsWith(github.ref_name, 'release/')) }} + # always() ensures this runs even if the test eval step exits 1 + if: ${{ always() && env.FAILED_TESTS == 'true' && (github.ref_name == 'main' || startsWith(github.ref_name, 'release/')) }} id: slack uses: slackapi/slack-github-action@e28cf165c92ffef168d23c5c9000cffc8a25e117 # v1.24.0 with: # github.event.head_commit.message and github.ref_name both rely on this event occurring on a push / merge payload: | { - "message": "❌ ${{ github.workflow }} workflow failed: \n\n- Run: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }} \n- Branch: ${{ github.ref_name }} \n- Message: ${{ github.event.head_commit.message }} \n- Author: ${{ github.event.sender.login }}" + "message": "❌ ${{ github.workflow }} workflow failed: \n\n- Run: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }} \n- Branch: ${{ github.ref_name }} \n- Message: ${{ env.COMMIT_MESSAGE_SUMMARY }} \n- Author: ${{ github.event.sender.login }}" } env: - SLACK_WEBHOOK_URL: ${{ secrets.CONSUL_NIGHTLY_INTEG_TEST_SLACK_WEBHOOK }} \ No newline at end of file + SLACK_WEBHOOK_URL: ${{ secrets.CONSUL_PROTECTED_BRANCH_TEST_SLACK_WEBHOOK }} \ No newline at end of file