From f1846015d018d27302942cc94a988db56c8f8ba1 Mon Sep 17 00:00:00 2001 From: Jeppe Reinhold Date: Sat, 10 Feb 2024 00:09:13 +0100 Subject: [PATCH] Continue with publishing even if some sandboxes fail --- .github/workflows/generate-sandboxes.yml | 17 +++++-- scripts/sandbox/generate.ts | 57 +++++++++++++++++------- 2 files changed, 55 insertions(+), 19 deletions(-) diff --git a/.github/workflows/generate-sandboxes.yml b/.github/workflows/generate-sandboxes.yml index 8c1af15b40ad..643361e63143 100644 --- a/.github/workflows/generate-sandboxes.yml +++ b/.github/workflows/generate-sandboxes.yml @@ -1,4 +1,4 @@ -name: Generate and push sandboxes +name: Generate and publish sandboxes on: schedule: @@ -56,15 +56,24 @@ jobs: run: yarn wait-on tcp:127.0.0.1:6001 - name: Generate + id: generate run: yarn generate-sandboxes --local-registry - name: Publish + # publish sandboxes even if the generation fails, as some sandboxes might have been generated successfully + if: !cancelled() run: yarn publish-sandboxes --remote=https://storybook-bot:${{ secrets.PAT_STORYBOOK_BOT }}@github.com/storybookjs/sandboxes.git --push --branch=next - - name: The job has failed - if: ${{ failure() || cancelled() }} + - name: Report failure to Discord + if: failure() env: DISCORD_WEBHOOK: ${{ secrets.DISCORD_MONITORING_URL }} uses: Ilshidur/action-discord@master with: - args: "The generation of sandboxes on the **next** branch has failed. [View Run](https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }})" + args: | + The generation of some or all sandboxes on the **next** branch has failed. + [View Job](https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}/job/${{ github.job }}) + + ${{ steps.generate.outputs.failed-before-script-templates && format('The following templates failed to execute the before script:\n- {0}', steps.generate.outputs.failed-before-script-templates) || '' }} + + ${{ steps.generate.outputs.failed-before-script-templates && format('The following templates failed to initialize Storybook:\n- {0}', steps.generate.outputs.failed-init-templates) || '' }} diff --git a/scripts/sandbox/generate.ts b/scripts/sandbox/generate.ts index 27f17f8c7ad0..969247d6ba1d 100755 --- a/scripts/sandbox/generate.ts +++ b/scripts/sandbox/generate.ts @@ -27,6 +27,7 @@ import { REPROS_DIRECTORY, LOCAL_REGISTRY_URL, } from '../utils/constants'; +import { setOutput } from '@actions/core'; const sbInit = async ( cwd: string, @@ -148,7 +149,10 @@ const runGenerators = async ( const limit = pLimit(1); - await Promise.all( + const failedBeforeScriptTemplates: string[] = []; + const failedInitTemplates: string[] = []; + + await Promise.allSettled( generators.map(({ dirName, name, script, expected, env }) => limit(async () => { let flags: string[] = []; @@ -173,19 +177,26 @@ const runGenerators = async ( // Some tools refuse to run inside an existing directory and replace the contents, // where as others are very picky about what directories can be called. So we need to // handle different modes of operation. - if (script.includes('{{beforeDir}}')) { - const scriptWithBeforeDir = script.replaceAll('{{beforeDir}}', BEFORE_DIR_NAME); - await runCommand( - scriptWithBeforeDir, - { - cwd: createBaseDir, - timeout: SCRIPT_TIMEOUT, - }, - debug - ); - } else { - await ensureDir(createBeforeDir); - await runCommand(script, { cwd: createBeforeDir, timeout: SCRIPT_TIMEOUT }, debug); + try { + if (script.includes('{{beforeDir}}')) { + const scriptWithBeforeDir = script.replaceAll('{{beforeDir}}', BEFORE_DIR_NAME); + await runCommand( + scriptWithBeforeDir, + { + cwd: createBaseDir, + timeout: SCRIPT_TIMEOUT, + }, + debug + ); + } else { + await ensureDir(createBeforeDir); + await runCommand(script, { cwd: createBeforeDir, timeout: SCRIPT_TIMEOUT }, debug); + } + } catch (error) { + console.error(`❌ Failed to execute before-script for template: ${name}`); + console.error(error); + failedBeforeScriptTemplates.push(name); + return; } await localizeYarnConfigFiles(createBaseDir, createBeforeDir); @@ -196,7 +207,14 @@ const runGenerators = async ( // Make sure there are no git projects in the folder await remove(join(beforeDir, '.git')); - await addStorybook({ baseDir, localRegistry, flags, debug, env }); + try { + await addStorybook({ baseDir, localRegistry, flags, debug, env }); + } catch (error) { + console.error(`❌ Failed to add Storybook to template: ${name}`); + console.error(error); + failedInitTemplates.push(name); + return; + } await addDocumentation(baseDir, { name, dirName }); @@ -218,6 +236,15 @@ const runGenerators = async ( }) ) ); + + if (process.env.GITHUB_ACTIONS === 'true') { + if (failedBeforeScriptTemplates.length > 0) { + setOutput('failed-before-script-templates', failedBeforeScriptTemplates.join('\n- ')); + } + if (failedInitTemplates.length > 0) { + setOutput('failed-init-templates', failedInitTemplates.join('\n- ')); + } + } }; export const options = createOptions({