From c871e76e9cecc1528f1ddac9410af0193303bd92 Mon Sep 17 00:00:00 2001 From: RMcVelia Date: Fri, 1 Dec 2023 11:21:01 +0000 Subject: [PATCH] Add smoke test post deployment --- .github/actions/deploy/action.yml | 44 +++------------------------ .github/actions/smoke-test/action.yml | 18 +++++++++++ bin/smoke | 42 ++++++++++++++++++++++--- 3 files changed, 60 insertions(+), 44 deletions(-) create mode 100644 .github/actions/smoke-test/action.yml diff --git a/.github/actions/deploy/action.yml b/.github/actions/deploy/action.yml index e9967d93..c4d65f24 100644 --- a/.github/actions/deploy/action.yml +++ b/.github/actions/deploy/action.yml @@ -86,47 +86,11 @@ runs: TF_VAR_azure_sp_credentials: ${{ inputs.azure-credentials }} CONFIRM_PRODUCTION: true - - name: Set up environment variables - shell: bash - run: | - case ${{ env.cluster }} in - test) - echo "cluster_rg=s189t01-tsc-ts-rg" >> $GITHUB_ENV - echo "cluster_name=s189t01-tsc-test-aks" >> $GITHUB_ENV - ;; - production) - echo "cluster_rg=s189p01-tsc-pd-rg" >> $GITHUB_ENV - echo "cluster_name=s189p01-tsc-production-aks" >> $GITHUB_ENV - ;; - *) - echo "unknown cluster" - ;; - esac - - - name: Install kubectl - uses: azure/setup-kubectl@v3 + - name: Run Smoke Tests for ${{ inputs.environment }} + uses: ./.github/actions/smoke-test with: - version: "v1.26.1" # default is latest stable - - - name: K8 setup - shell: bash - run: | - az aks get-credentials -g ${{ env.cluster_rg }} -n ${{ env.cluster_name }} - make install-konduit - -# review app seeded? -# - name: Generate example data -# shell: bash -# if: inputs.environment == 'review' -# run: kubectl exec -n ${{ env.namespace }} deployment/teacher-relocation-payment-${APP_NAME} -- /bin/sh -c "cd /app && bin/rails RAILS_ENV=test db:schema:load && bin/rails RAILS_ENV=test db:seed" - -# - name: Run Smoke Tests for ${{ inputs.environment }} -# uses: ./.github/actions/smoke-test_v2/ -# with: -# environment: ${{ inputs.environment }} -# app-env: ${{ env.aks_app_environment }} -# pr-number: ${{ inputs.pr-number }} -# slack-webhook: ${{ inputs.slack-webhook }} + current-commit-sha: ${{ inputs.sha }} + url: ${{ steps.set_env_var.outputs.deploy_url }} # - name: Notify Slack channel on job failure # if: ${{ failure() && github.ref == 'refs/heads/main' }} diff --git a/.github/actions/smoke-test/action.yml b/.github/actions/smoke-test/action.yml new file mode 100644 index 00000000..4015d435 --- /dev/null +++ b/.github/actions/smoke-test/action.yml @@ -0,0 +1,18 @@ +name: smoke-test +description: runs smoke tests + +inputs: + url: + description: The URL of the deployed environment. + required: true + current-commit-sha: + description: The sha of the current commit + required: true + +runs: + using: composite + + steps: + - name: Run smoke tests + shell: bash + run: bin/smoke ${{ inputs.url }} ${{ inputs.current-commit-sha }} diff --git a/bin/smoke b/bin/smoke index a171bf01..2871cb22 100755 --- a/bin/smoke +++ b/bin/smoke @@ -1,8 +1,42 @@ -#!/usr/bin/env sh -# -# Application smoke test script +#!/usr/bin/env bash + +# App service healthcheck +# Runs as a smoke test in deployment workflows +# Can also run manually # +# Usage: +# ./smoke set -e -bundle exec rspec spec/features/smoke_spec.rb --tag smoke_test +url=$1 +if [[ -z $url ]]; then + echo `date`" - smoke test failed (URL is missing)" + exit 1 +fi +response=$(curl -sL $url/healthcheck/version) +response_sha=\"$(cut -d " " -f 4 <<< $response)\" + +current_commit_sha=\"$2\" +if [[ -z $current_commit_sha ]]; then + echo `date`" - smoke test failed (head sha is missing)" + exit 1 +fi + +if [[ $response_sha == $current_commit_sha ]]; then + echo "✅ Correct version deployed" +else + echo "Fail: healthcheck sha is $response_sha but current commit is $current_commit_sha" + exit 1 +fi + +response=$(curl -sL $url/healthcheck/database) + +database_connected=$(cut -d " " -f 2 <<< $response) + +if [[ $database_connected == 'PASSED' ]]; then + echo "✅ Database is connected" +else + echo "Fail: database is not connected" + exit 1 +fi