Skip to content

Commit

Permalink
Add upload url to release package action outputs
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickcorbett1 committed Nov 1, 2024
1 parent 31ff97e commit 843552c
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/sixty-snails-mate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@changesets/action": minor
---

Add uploadUrl to publishedPackages output
51 changes: 50 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,17 @@ This action for [Changesets](https://github.com/atlassian/changesets) creates a

### Outputs

"/https:\/\/uploads\.github\.com\/repos\/([^/]+)\/([^/]+)\/releases\/(\d+)\/assets(?:\?name=([^&]+))?(?:&label=([^&]+))?/"

- published - A boolean value to indicate whether a publishing has happened or not
- publishedPackages - A JSON array to present the published packages. The format is `[{"name": "@xx/xx", "version": "1.2.0"}, {"name": "@xx/xy", "version": "0.8.9"}]`
- publishedPackages - A JSON array to present the published packages.
The format is
```
[
{"name": "@org/package1", "version": "1.2.0", "uploadUrl": "https://uploads.github.com/repos/org/repo/releases/package1_releaseId/assets{?name, label}" },
{"name": "@org/package2", "version": "0.8.9", "uploadUrl": "https://uploads.github.com/repos/org/repo/releases/package2_releaseId/assets{?name, label}" }
]
```

### Example workflow:

Expand Down Expand Up @@ -122,6 +131,46 @@ For example, you can add a step before running the Changesets GitHub Action:
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
```

#### Uploading extra assets

If you want to upload extra assets after a release is created you can utilize the `publishedPackages[0].uploadUrl` output.

```yml
name: Release
on:
push:
branches:
- main
jobs:
release:
name: Release
runs-on: ubuntu-latest
steps:
- name: Checkout Repo
uses: actions/checkout@v3
- name: Setup Node.js 20.x
uses: actions/setup-node@v3
with:
node-version: 20.x
- name: Install Dependencies
run: yarn
- name: Create Release Pull Request or Publish to npm
id: changesets
uses: changesets/action@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Publish
if: steps.changesets.outputs.published == 'true'
# You can do something with the published packages array
run: my-file-uploader upload-files {{ steps.changesets.outputs.publishedPackages }}
```

#### Custom Publishing

If you want to hook into when publishing should occur but have your own publishing functionality, you can utilize the `hasChangesets` output.
Expand Down
37 changes: 26 additions & 11 deletions src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import readChangesetState from "./readChangesetState";
import resolveFrom from "resolve-from";
import { throttling } from "@octokit/plugin-throttling";

type ReleasedPackage = Package & { uploadUrl: string };

// GitHub Issues/PRs messages have a max size limit on the
// message body payload.
// `body is too long (maximum is 65536 characters)`.
Expand Down Expand Up @@ -76,7 +78,7 @@ const createRelease = async (
);
}

await octokit.rest.repos.createRelease({
return await octokit.rest.repos.createRelease({
name: tagName,
tag_name: tagName,
body: changelogEntry.content,
Expand Down Expand Up @@ -133,7 +135,8 @@ export async function runPublish({
await gitUtils.pushTags();

let { packages, tool } = await getPackages(cwd);
let releasedPackages: Package[] = [];
let packagesToRelease: Package[] = [];
let releasedPackages: ReleasedPackage[] = [];

if (tool !== "root") {
let newTagRegex = /New tag:\s+(@[^/]+\/[^@]+|[^/]+)@([^\s]+)/;
Expand All @@ -152,18 +155,23 @@ export async function runPublish({
"This is probably a bug in the action, please open an issue"
);
}
releasedPackages.push(pkg);
packagesToRelease.push(pkg);
}

if (createGithubReleases) {
await Promise.all(
releasedPackages.map((pkg) =>
createRelease(octokit, {
pkg,
tagName: `${pkg.packageJson.name}@${pkg.packageJson.version}`,
releasedPackages = (
await Promise.all(
packagesToRelease.map(async (pkg) => {
const release = await createRelease(octokit, {
pkg,
tagName: `${pkg.packageJson.name}@${pkg.packageJson.version}`,
});
if (release) {
return { ...pkg, uploadUrl: release.data.upload_url };
}
})
)
);
).filter((pkg) => pkg !== undefined);
}
} else {
if (packages.length === 0) {
Expand All @@ -179,12 +187,18 @@ export async function runPublish({
let match = line.match(newTagRegex);

if (match) {
releasedPackages.push(pkg);
packagesToRelease.push(pkg);
if (createGithubReleases) {
await createRelease(octokit, {
const release = await createRelease(octokit, {
pkg,
tagName: `v${pkg.packageJson.version}`,
});
if (release) {
releasedPackages.push({
...pkg,
uploadUrl: release.data.upload_url,
});
}
}
break;
}
Expand All @@ -197,6 +211,7 @@ export async function runPublish({
publishedPackages: releasedPackages.map((pkg) => ({
name: pkg.packageJson.name,
version: pkg.packageJson.version,
uploadUrl: pkg.uploadUrl,
})),
};
}
Expand Down

0 comments on commit 843552c

Please sign in to comment.