Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions packages/cli/environments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
Expand Down
16 changes: 14 additions & 2 deletions packages/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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("/");
Expand Down Expand Up @@ -193,6 +194,7 @@ const main = defineCommand({
}

const { sha } = await checkResponse.json();
const formattedSha = isCompact ? abbreviateCommitHash(sha) : sha;

const deps: Map<string, string> = new Map(); // pkg.pr.new versions of the package
const realDeps: Map<string, string> | null = isPeerDepsEnabled
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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",
);
},
};
},
Expand Down
Loading