Skip to content

Commit 3a92583

Browse files
cometkimAmirSa12
andauthored
feat(cli): append some context to GITHUB_OUTPUT (#364)
Co-authored-by: AmirSa12 <[email protected]>
1 parent 80fffa9 commit 3a92583

File tree

3 files changed

+79
-2
lines changed

3 files changed

+79
-2
lines changed

README.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,69 @@ on:
303303

304304
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.
305305

306+
#### Run E2E test using outputs
307+
308+
After `pkg-pr-new publish` runs successfully, some outputs are available.
309+
310+
- `sha`: The short SHA used. (E.g. `a832a55`)
311+
- `urls`: Space-separated URLs of published packages.
312+
- `packages`: Space-separated, Yarn-compatible package locators of published packages.
313+
314+
This is useful for using published packages in other subsequent jobs immediately after publishing. (E.g. E2E tests)
315+
316+
```yml
317+
name: Publish and Test Packages
318+
on: [push, pull_request]
319+
320+
jobs:
321+
publish:
322+
runs-on: ubuntu-latest
323+
outputs:
324+
sha: ${{ steps.publish.outputs.sha }}
325+
urls: ${{ steps.publish.outputs.urls }}
326+
steps:
327+
- name: Checkout code
328+
uses: actions/checkout@v4
329+
330+
- run: corepack enable
331+
- uses: actions/setup-node@v4
332+
with:
333+
node-version: 20
334+
cache: "pnpm"
335+
336+
- name: Install dependencies
337+
run: pnpm install
338+
339+
- name: Build
340+
run: pnpm build
341+
342+
- id: publish
343+
run: pnpm dlx pkg-pr-new publish
344+
345+
e2e-test:
346+
runs-on: ubuntu-latest
347+
needs: publish
348+
steps:
349+
- name: Checkout code
350+
uses: actions/checkout@v4
351+
with:
352+
repository: user/my-package-e2e
353+
354+
- run: corepack enable
355+
- uses: actions/setup-node@v4
356+
with:
357+
node-version: 20
358+
359+
- name: Install dependencies
360+
run: pnpm install
361+
362+
- name: Install published package
363+
run: pnpm add ${{ needs.publish.outputs.urls }}
364+
365+
- name: Run e2e test cases
366+
run: # ...
367+
```
368+
306369
## Custom GitHub Messages and Comments
307370

308371
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.

packages/cli/environments.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ declare global {
3030
GITHUB_JOB: string;
3131
// 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.
3232
GITHUB_RUN_ATTEMPT: string;
33+
// A file to set action outputs
34+
GITHUB_OUTPUT: string;
3335
}
3436
}
3537
}

packages/cli/index.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ const main = defineCommand({
164164
GITHUB_RUN_ID,
165165
GITHUB_RUN_ATTEMPT,
166166
GITHUB_ACTOR_ID,
167+
GITHUB_OUTPUT,
167168
} = process.env;
168169

169170
const [owner, repo] = GITHUB_REPOSITORY.split("/");
@@ -193,6 +194,7 @@ const main = defineCommand({
193194
}
194195

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

197199
const deps: Map<string, string> = new Map(); // pkg.pr.new versions of the package
198200
const realDeps: Map<string, string> | null = isPeerDepsEnabled
@@ -225,8 +227,6 @@ const main = defineCommand({
225227
if (isCompact) {
226228
await verifyCompactMode(pJson.name);
227229
}
228-
229-
const formattedSha = isCompact ? abbreviateCommitHash(sha) : sha;
230230
const longDepUrl = new URL(
231231
`/${owner}/${repo}/${pJson.name}@${formattedSha}`,
232232
apiUrl,
@@ -552,6 +552,18 @@ const main = defineCommand({
552552
await fs.writeFile(jsonFilePath, output);
553553
console.warn(`metadata written to ${jsonFilePath}`);
554554
}
555+
556+
await fs.appendFile(GITHUB_OUTPUT, `sha=${formattedSha}\n`, "utf8");
557+
await fs.appendFile(
558+
GITHUB_OUTPUT,
559+
`urls=${outputMetadata.packages.map((pkg) => pkg.url).join(" ")}\n`,
560+
"utf8",
561+
);
562+
await fs.appendFile(
563+
GITHUB_OUTPUT,
564+
`packages=${outputMetadata.packages.map((pkg) => `${pkg.name}@${pkg.url}`).join(" ")}\n`,
565+
"utf8",
566+
);
555567
},
556568
};
557569
},

0 commit comments

Comments
 (0)