diff --git a/.github/workflows/composite-actions/kubernetes-e2e-tests/action.yaml b/.github/workflows/composite-actions/kubernetes-e2e-tests/action.yaml index 17283a2e598..56ebb52cfbf 100644 --- a/.github/workflows/composite-actions/kubernetes-e2e-tests/action.yaml +++ b/.github/workflows/composite-actions/kubernetes-e2e-tests/action.yaml @@ -21,6 +21,9 @@ inputs: matrix-label: required: true description: The version of the matrix being used - used to name artifacts to prevent filename collisions + slackbot-bearer: + required: true + description: The secret bearer token for the slackbot integration runs: using: "composite" @@ -37,6 +40,7 @@ runs: shell: bash run: make go-test - name: Summarize tests + if: ${{ always() }} # Print out a summary of ALL tests run under this action. In the future we can use this tool # with the --json flag to export the results for centralized processing. shell: bash @@ -53,3 +57,12 @@ runs: with: name: go-test-summary-${{ inputs.cluster-name }}-${{ inputs.matrix-label }}-attempt-${{ github.run_attempt }} path: ./_test/test_log/go-test-summary + - name: Send Slack Message + if: ${{ always() }} + env: + SLACKBOT_BEARER: ${{ inputs.slackbot-bearer }} + # SLACK_CHANNEL: C04CJMXAH7A #edge-nightly-results by default + SLACK_CHANNEL: C0314KESVNV #slack-integration-testing for testing + shell: bash + run: | + go run .github/workflows/helpers/notify/slack_test_summary.go './_test/test_log/go-test-summary' diff --git a/.github/workflows/helpers/notify/slack_test_summary.go b/.github/workflows/helpers/notify/slack_test_summary.go new file mode 100644 index 00000000000..d6298b19609 --- /dev/null +++ b/.github/workflows/helpers/notify/slack_test_summary.go @@ -0,0 +1,85 @@ +// Helper script designed to send a cohesive Slack notification about the result of a collection of GitHub workflows +// This is a simpler variant of notify-from-json.go, due to some issues we +// encountered around the accuracy of the notification: https://github.com/solo-io/solo-projects/issues/5191 +// +// This works by: +// 1. Read in the test summary +// 2. Send a Slack notification with the contents +// +// Example usage: +// export SLACKBOT_BEARER=${{ secrets.SLACKBOT_BEARER }} +// export SLACK_CHANNEL=C0314KESVNV +// go run .github/workflows/helpers/notify/slack_test_summary.go ./_test/test_log/go-test-summary + +package main + +import ( + "bytes" + "encoding/json" + "fmt" + "io" + "net/http" + "os" + "time" +) + +const ( + postMessageEndpoint = "https://slack.com/api/chat.postMessage" +) + +type Payload struct { + Channel string `json:"channel"` + Text string `json:"text"` +} + +func main() { + summaryFile := os.Args[1] + fmt.Printf("slack_test_summary.go invoked with: %v", summaryFile) + + b, err := os.ReadFile(summaryFile) + if err != nil { + panic(err) + } + + mustSendSlackText(string(b)) +} + +func mustSendSlackText(text string) { + fmt.Printf("send slack message with text: %s", text) + mustSendSlackMessage(Payload{ + Channel: os.ExpandEnv("$SLACK_CHANNEL"), + Text: os.ExpandEnv(text), + }) +} + +func mustSendSlackMessage(data Payload) { + payloadBytes, err := json.Marshal(data) + if err != nil { + panic(err) + } + + req, err := http.NewRequest(http.MethodPost, postMessageEndpoint, bytes.NewReader(payloadBytes)) + if err != nil { + panic(err) + } + req.Header.Set("Content-Type", "application/json; charset=utf-8") + req.Header.Set("Authorization", os.ExpandEnv("Bearer $SLACKBOT_BEARER")) + + var netClient = &http.Client{ + Timeout: time.Second * 10, + } + + resp, err := netClient.Do(req) + if err != nil { + panic(err) + } + defer resp.Body.Close() + + fmt.Println(resp.Status) + b, err := io.ReadAll(resp.Body) + if err != nil { + panic(err) + } + fmt.Println(string(b)) + +} diff --git a/.github/workflows/nightly-tests.yaml b/.github/workflows/nightly-tests.yaml index 8034d4d1d2f..c370e8cf5ec 100644 --- a/.github/workflows/nightly-tests.yaml +++ b/.github/workflows/nightly-tests.yaml @@ -112,6 +112,7 @@ jobs: run-regex: ${{ matrix.test.go-test-run-regex }} istio-version: ${{ steps.dotenv.outputs.istio_version }} matrix-label: ${{ matrix.version-files.label }} + slackbot-bearer: ${{ secrets.SLACKBOT_BEARER }} end_to_end_tests_main: name: End-to-End (branch=main, cluster=${{ matrix.test.cluster-name }}, version=${{ matrix.version-files.label }} ) diff --git a/.github/workflows/pr-kubernetes-tests.yaml b/.github/workflows/pr-kubernetes-tests.yaml index f4451bdb2db..bffc51dc53f 100644 --- a/.github/workflows/pr-kubernetes-tests.yaml +++ b/.github/workflows/pr-kubernetes-tests.yaml @@ -128,3 +128,4 @@ jobs: run-regex: ${{ matrix.test.go-test-run-regex }} istio-version: ${{ steps.dotenv.outputs.istio_version }} matrix-label: "pr" + slackbot-bearer: ${{ secrets.SLACKBOT_BEARER }}