From 5da6cf8c1d8ea403953c15c6d15d69a8d9e91763 Mon Sep 17 00:00:00 2001 From: Karl Cardenas <29551334+karl-cardenas-coding@users.noreply.github.com> Date: Thu, 16 Jan 2025 08:59:01 -0700 Subject: [PATCH] ci: DOC-1597 CI/CD Error Catching (#5451) * ci: adding set -e * ci: more test * ci: more test * ci: test with exit code 5 * ci: not set -e * ci: DOC-1597 * ci: removed script * docs: updated README * docs: format fix * chore: fix mistake * ci: retest * ci: mock test for 5 * ci: test with 5 * ci: test with 5 * ci: clean build test * ci: ready for merge * docs: apply suggestions from code review Co-authored-by: Adelina Simion <43963729+addetz@users.noreply.github.com> * ci: added logic for retry * docs: updated comment --------- Co-authored-by: Adelina Simion <43963729+addetz@users.noreply.github.com> --- .github/actions/build-cached-cves/action.yaml | 2 ++ .github/actions/build-cached-packs/action.yaml | 2 ++ .github/workflows/release-preview.yaml | 3 ++- .github/workflows/release.yaml | 5 +++-- .github/workflows/screenshot_capture.yaml | 1 + Makefile | 11 ++++++++++- README.md | 4 ++++ 7 files changed, 24 insertions(+), 4 deletions(-) diff --git a/.github/actions/build-cached-cves/action.yaml b/.github/actions/build-cached-cves/action.yaml index 289dbc0fa2..37e0d70906 100644 --- a/.github/actions/build-cached-cves/action.yaml +++ b/.github/actions/build-cached-cves/action.yaml @@ -43,4 +43,6 @@ runs: run: | rm -rf build npm run build + exit_code=$? + echo "BUILD_EXIT_CODE=$$exit_code" >> $GITHUB_ENV shell: bash diff --git a/.github/actions/build-cached-packs/action.yaml b/.github/actions/build-cached-packs/action.yaml index c017dbd6c2..2aaf3083b4 100644 --- a/.github/actions/build-cached-packs/action.yaml +++ b/.github/actions/build-cached-packs/action.yaml @@ -45,4 +45,6 @@ runs: run: | rm -rf build npm run build + exit_code=$? + echo "BUILD_EXIT_CODE=$$exit_code" >> $GITHUB_ENV shell: bash diff --git a/.github/workflows/release-preview.yaml b/.github/workflows/release-preview.yaml index b72faaf46b..a74d6be5a1 100644 --- a/.github/workflows/release-preview.yaml +++ b/.github/workflows/release-preview.yaml @@ -59,7 +59,7 @@ jobs: run: | set +e # Disable automatic stop on command failure touch .env - make build + make build-ci exit_code=$? echo "Build command exit code: $exit_code" echo "BUILD_EXIT_CODE=$exit_code">> $GITHUB_ENV @@ -77,6 +77,7 @@ jobs: gh-token: ${{ secrets.GITHUB_TOKEN }} - name: Deploy Preview + if: ${{ env.BUILD_EXIT_CODE == '0' }} run: | aws s3 sync --cache-control 'public, max-age=604800' --exclude '*.html' --exclude build/scripts/ build/ s3://docs-latest.spectrocloud.com --delete aws s3 sync --cache-control 'public, max-age=0, s-maxage=604800' build/ s3://docs-latest.spectrocloud.com --delete diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index de4399c5c5..cbee839783 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -67,7 +67,7 @@ jobs: touch .env make versions-ci make build-ci - + - name: Build with cached packs if: ${{ env.BUILD_EXIT_CODE == '5' }} uses: ./.github/actions/build-cached-packs @@ -81,6 +81,7 @@ jobs: gh-token: ${{ secrets.GITHUB_TOKEN }} - name: Upload to AWS + if: ${{ env.BUILD_EXIT_CODE == '0' }} run: | echo "CURRENT_STEP=Upload to AWS" >> $GITHUB_ENV aws s3 sync --cache-control 'public, max-age=604800' --exclude '*.html' --exclude '*.xml' --exclude build/scripts/ build/ s3://docs.spectrocloud.com --delete @@ -88,7 +89,7 @@ jobs: aws cloudfront create-invalidation --distribution-id ${{ secrets.DISTRIBUTION_ID }} --paths "/*" - name: Slack Notification on Failure - if: ${{ failure() }} + if: ${{ failure() || env.BUILD_EXIT_CODE != '0' }} uses: rtCamp/action-slack-notify@v2.3.2 env: SLACK_WEBHOOK: ${{ secrets.SLACK_PRIVATE_TEAM_WEBHOOK }} diff --git a/.github/workflows/screenshot_capture.yaml b/.github/workflows/screenshot_capture.yaml index 7d7046479f..a067aba40b 100644 --- a/.github/workflows/screenshot_capture.yaml +++ b/.github/workflows/screenshot_capture.yaml @@ -64,6 +64,7 @@ jobs: gh-token: ${{ secrets.GITHUB_TOKEN }} - name: Upload Build + if: ${{ env.BUILD_EXIT_CODE == '0' }} uses: actions/upload-artifact@v4 with: name: "build" diff --git a/Makefile b/Makefile index b7cd574b2b..b496a72393 100644 --- a/Makefile +++ b/Makefile @@ -158,6 +158,10 @@ build-cached-cves: ## Run npm build with cached CVEs retry fi; \ } + +# This step is designed to always return a positive exit code due to the echo that outputs the exit code. +# This is by design to ensure that the build step does not fail and subsequent GitHub Actions steps can execute that are dependent on the exit code. +# It may be counterintuitive, but it is necessary to ensure that the build CI workflow does not fails completely without providing an opportunity for the retry steps that check the exit code. build-ci: ## Run npm build in CI environment @echo "building site" npm run clear @@ -165,9 +169,14 @@ build-ci: ## Run npm build in CI environment @{ \ npm run build; \ exit_code=$$?; \ - echo "Build exited with code $$exit_code..."; \ echo "BUILD_EXIT_CODE=$$exit_code" >> $(GITHUB_ENV); \ + echo "Build exited with code $$exit_code..."; \ + if [ $$exit_code -ne 0 ] && [ $$exit_code -ne 5 ] && [ $$exit_code -ne 7 ]; then \ + echo "Unacceptable exit code: $$exit_code"; \ + exit 1; \ + fi; \ } + versions: ## Create Docusarus content versions @echo "creating versions" diff --git a/README.md b/README.md index 10fd3cec61..0e35052690 100644 --- a/README.md +++ b/README.md @@ -1191,6 +1191,10 @@ make clean-versions Librarium provides the following exit codes. These exit codes are returned by both the `npm run start` and `npm run build` commands. +> [!NOTE] +> +> Any exit codes added to the table must also be added to the Makefile's `build-ci` command. + | **Exit Code** | **Description** | | ----------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | | `0` | The command was executed successfully. |