|
1 | | -# Publishing |
2 | | - |
3 | | -In order for other people to install and use this component, you can publish the |
4 | | -package to npm. |
5 | | - |
6 | | -You will first need to have an npmjs account with permissions to push to your |
7 | | -package name. |
8 | | - |
9 | | -If this is your first time, here are the recommended steps: |
10 | | - |
11 | | -1. Ensure the package.json "name" matches what you want it to be called. It |
12 | | - should either be like `my-package` or `@my-org/my-package`. If it's the |
13 | | - latter, ensure you have an npmjs account with permissions to push to |
14 | | - `my-org`. |
15 | | -2. `npm login` to login to npmjs. |
16 | | -3. `npm run clean` to clean your `/dist` directory. |
17 | | -4. `npm ci` to install the dependencies with the versions specified in |
18 | | - `package-lock.json`. |
19 | | -5. `npm run build` to build the package fresh. |
20 | | -6. (Optional) `npm run typecheck` to typecheck the package. |
21 | | -7. (Optional) `npm run lint` to lint the package. |
22 | | -8. (Optional) `npm run test` to test the package. |
23 | | -9. (Optional) `npm pack` will create a .tgz file of the package. You can then |
24 | | - try installing it in another project with |
25 | | - `npm install ./path/to/your-package.tgz` to sanity check that it works as |
26 | | - expected. You can remove the .tgz file after. |
27 | | -10. `npm publish --access public` to publish the package to npm. |
28 | | -11. `git tag v0.1.0` to tag the new version. |
29 | | -12. `git push --follow-tags` to push the tags to the repository. This way, other |
30 | | - contributors can always see what code was published with each version. |
31 | | - Running `npm version ...` will create these tags and commits automatically. |
32 | | - |
33 | | -After the initial publish, you can use the release scripts documented below, |
34 | | -which will do steps 3-12 automatically (except the sanity check in step 9). |
35 | | - |
36 | | -## Package scripts for releasing |
37 | | - |
38 | | -In package.json, there are some scripts that are useful for doing releases. |
39 | | - |
40 | | -- `preversion` will run the tests and typecheck the code before marking a new |
41 | | - version. |
42 | | -- `version` will open the changelog in vim and then save it before committing |
43 | | - the new version. |
44 | | -- `prepublishOnly` will make a clean build of the package before publishing. |
45 | | - |
46 | | -These are not required and can be modified or removed if desired. They will all |
47 | | -be run automatically when using one of the deployment commands. |
48 | | - |
49 | | -## Deploying a new alpha version |
| 1 | +# Publishing from CI |
50 | 2 |
|
51 | | -```sh |
52 | | -npm run alpha |
53 | | -``` |
| 3 | +The package is versioned with Changesets and released from `v<version>` tags. |
| 4 | +When a reviewed version PR merges to `main`, the version workflow tags the |
| 5 | +commit that set the new version and starts |
| 6 | +[`release.yml`](.github/workflows/release.yml) on that tag. The release builds |
| 7 | +and tests that exact commit, then stages the tested package on npm with trusted |
| 8 | +publishing. A maintainer reviews the staged package on npm and approves it with |
| 9 | +2FA. |
54 | 10 |
|
55 | | -This will create a prerelease version with an `@alpha` tag. It will then publish |
56 | | -the package to npm and push the code and new tag. Users can install the package |
57 | | -with `npm install @your-package@alpha`. |
| 11 | +## Setup |
58 | 12 |
|
59 | | -## Deploying a new release version |
| 13 | +Before the first release, replace the template npm package name and GitHub |
| 14 | +repository with your own, and confirm `package.json` repository metadata matches |
| 15 | +the GitHub repository. Then: |
60 | 16 |
|
61 | | -```sh |
62 | | -npm run release |
63 | | -``` |
| 17 | +- If the package isn't on npm yet, publish its first version yourself, since npm |
| 18 | + can only configure trusted publishing and staging for an existing package. Run |
| 19 | + the same checks as the release workflow, from a clean clone of the commit you |
| 20 | + intend to release: |
64 | 21 |
|
65 | | -This will create a patch version and publish as `latest`. It will then publish |
66 | | -the package to npm and push the code and new tag. To publish a new minor or |
67 | | -major version, you can run the commands manually: |
| 22 | + ```sh |
| 23 | + git clone https://github.com/example-org/sample-component && cd sample-component |
| 24 | + npm ci |
| 25 | + npm run build |
| 26 | + npm run check |
| 27 | + npm run test:e2e |
| 28 | + git status --porcelain # should print nothing |
| 29 | + npm publish --access public |
| 30 | + ``` |
68 | 31 |
|
69 | | -```sh |
70 | | -npm version minor # or major |
71 | | -npm publish |
72 | | -git push --follow-tags |
73 | | -``` |
| 32 | +- Register stage-only npm trusted publishing for `release.yml`: |
| 33 | + |
| 34 | + ```sh |
| 35 | + npm trust github @example/sample-component --repo example-org/sample-component --file release.yml --allow-stage-publish |
| 36 | + ``` |
74 | 37 |
|
75 | | -## Building a one-off package |
| 38 | +- Allow GitHub Actions to create pull requests, for the Changesets version PR. |
| 39 | +- Require the always-running `Test and lint` check on `main`. GitHub asks a |
| 40 | + maintainer to approve workflow runs on bot-created version PRs before that |
| 41 | + check can complete. |
| 42 | + |
| 43 | +## Releasing another commit |
| 44 | + |
| 45 | +To release a commit other than the one CI tags, such as a prerelease from a |
| 46 | +branch, tag it yourself: |
76 | 47 |
|
77 | 48 | ```sh |
78 | | -npm run clean |
79 | | -npm run build |
80 | | -npm pack |
| 49 | +gh api repos/example-org/sample-component/git/refs -f ref=refs/tags/v1.2.3 -f sha=<commit> |
81 | 50 | ``` |
82 | 51 |
|
83 | | -You can then provide the .tgz file to others to install via |
84 | | -`npm install ./path/to/your-package.tgz`. |
| 52 | +The tag must match `package.json`'s version, and stable releases must be on |
| 53 | +`main`. The first prerelease identifier selects the npm channel, such as |
| 54 | +`alpha`. |
| 55 | + |
| 56 | +## Recovery |
| 57 | + |
| 58 | +The tag records intent to release; npm is the source of truth for what was |
| 59 | +staged. If the workflow fails, check npm's staged packages first: |
| 60 | + |
| 61 | +- Tagged, but Release didn't start: start it on the tag with |
| 62 | + `gh workflow run release.yml -R example-org/sample-component --ref v1.2.3`. |
| 63 | +- Not staged: rerun the failed jobs |
| 64 | + (`gh run rerun <run-id> -R example-org/sample-component --failed`). A rerun |
| 65 | + uses the run's original commit and works for 30 days. To release a different |
| 66 | + commit, fix the source and move the tag: |
| 67 | + |
| 68 | + ```sh |
| 69 | + gh api -X PATCH repos/example-org/sample-component/git/refs/tags/v1.2.3 -f sha=<commit> -F force=true |
| 70 | + ``` |
| 71 | + |
| 72 | +- Staged, but rejected in review: `npm stage reject <stage-id>` frees the |
| 73 | + version, so you can move the tag to a corrected commit. A published version |
| 74 | + cannot be reused; prepare a new version instead. |
0 commit comments