diff --git a/README.md b/README.md index f061634b..6717edf6 100644 --- a/README.md +++ b/README.md @@ -303,6 +303,69 @@ on: As noted in [#140](https://github.com/stackblitz-labs/pkg.pr.new/issues/140), workflows run on tags too, that's not an issue at all, but in case users would like to avoid duplicate publishes. +#### Run E2E test using outputs + +After `pkg-pr-new publish` runs successfully, some outputs are available. + +- `sha`: The short SHA used. (E.g. `a832a55`) +- `urls`: Space-separated URLs of published packages. +- `packages`: Space-separated, Yarn-compatible package locators of published packages. + +This is useful for using published packages in other subsequent jobs immediately after publishing. (E.g. E2E tests) + +```yml +name: Publish and Test Packages +on: [push, pull_request] + +jobs: + publish: + runs-on: ubuntu-latest + outputs: + sha: ${{ steps.publish.outputs.sha }} + urls: ${{ steps.publish.outputs.urls }} + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - run: corepack enable + - uses: actions/setup-node@v4 + with: + node-version: 20 + cache: "pnpm" + + - name: Install dependencies + run: pnpm install + + - name: Build + run: pnpm build + + - id: publish + run: pnpm dlx pkg-pr-new publish + + e2e-test: + runs-on: ubuntu-latest + needs: publish + steps: + - name: Checkout code + uses: actions/checkout@v4 + with: + repository: user/my-package-e2e + + - run: corepack enable + - uses: actions/setup-node@v4 + with: + node-version: 20 + + - name: Install dependencies + run: pnpm install + + - name: Install published package + run: pnpm add ${{ needs.publish.outputs.urls }} + + - name: Run e2e test cases + run: # ... +``` + ## Custom GitHub Messages and Comments For advanced use cases where you want more control over the messages posted by pkg.pr.new, you can use the `--json` option in combination with `--comment=off`. This allows you to generate metadata about the publish operation without creating a default comment, which you can then use to create custom comments via the GitHub Actions API. diff --git a/packages/cli/environments.ts b/packages/cli/environments.ts index 28bca743..0dd78c39 100644 --- a/packages/cli/environments.ts +++ b/packages/cli/environments.ts @@ -30,6 +30,8 @@ declare global { GITHUB_JOB: string; // A unique number for each attempt of a particular workflow run in a repository. This number begins at 1 for the workflow run's first attempt, and increments with each re-run. For example, 3. GITHUB_RUN_ATTEMPT: string; + // A file to set action outputs + GITHUB_OUTPUT: string; } } } diff --git a/packages/cli/index.ts b/packages/cli/index.ts index 376be6b4..72a2c286 100644 --- a/packages/cli/index.ts +++ b/packages/cli/index.ts @@ -164,6 +164,7 @@ const main = defineCommand({ GITHUB_RUN_ID, GITHUB_RUN_ATTEMPT, GITHUB_ACTOR_ID, + GITHUB_OUTPUT, } = process.env; const [owner, repo] = GITHUB_REPOSITORY.split("/"); @@ -193,6 +194,7 @@ const main = defineCommand({ } const { sha } = await checkResponse.json(); + const formattedSha = isCompact ? abbreviateCommitHash(sha) : sha; const deps: Map = new Map(); // pkg.pr.new versions of the package const realDeps: Map | null = isPeerDepsEnabled @@ -225,8 +227,6 @@ const main = defineCommand({ if (isCompact) { await verifyCompactMode(pJson.name); } - - const formattedSha = isCompact ? abbreviateCommitHash(sha) : sha; const longDepUrl = new URL( `/${owner}/${repo}/${pJson.name}@${formattedSha}`, apiUrl, @@ -552,6 +552,18 @@ const main = defineCommand({ await fs.writeFile(jsonFilePath, output); console.warn(`metadata written to ${jsonFilePath}`); } + + await fs.appendFile(GITHUB_OUTPUT, `sha=${formattedSha}\n`, "utf8"); + await fs.appendFile( + GITHUB_OUTPUT, + `urls=${outputMetadata.packages.map((pkg) => pkg.url).join(" ")}\n`, + "utf8", + ); + await fs.appendFile( + GITHUB_OUTPUT, + `packages=${outputMetadata.packages.map((pkg) => `${pkg.name}@${pkg.url}`).join(" ")}\n`, + "utf8", + ); }, }; },