Skip to content

Commit 5f6a705

Browse files
committed
feat: custom registry
1 parent 2bb9bcb commit 5f6a705

File tree

3 files changed

+33
-6
lines changed

3 files changed

+33
-6
lines changed

README.md

+15
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,21 @@ For example, you can add a step before running the Changesets GitHub Action:
122122
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
123123
```
124124

125+
If you want to publish package to other registry than NPM, you can do that by providing custom registry domain. GitHub Actions will create a `.npmrc` accordingly to specified registry.
126+
For example, you can specify changeset to publish a package to GitHub Pcakages
127+
128+
```yml
129+
- name: Create Release Pull Request or Publish to GitHub Packages
130+
id: changesets
131+
uses: changesets/action@v1
132+
with:
133+
publish: yarn release
134+
registry: npm.pkg.github.com
135+
env:
136+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
137+
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
138+
```
139+
125140
#### Custom Publishing
126141

127142
If you want to hook into when publishing should occur but have your own publishing functionality you can utilize the `hasChangesets` output.

action.yml

+4
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ inputs:
2828
description: "A boolean value to indicate whether to create Github releases after `publish` or not"
2929
required: false
3030
default: true
31+
registry:
32+
description: "Specify the registry to publish to. Default to `registry.npmjs.org`"
33+
required: false
34+
default: "registry.npmjs.org"
3135
outputs:
3236
published:
3337
description: A boolean value to indicate whether a publishing is happened or not

src/index.ts

+14-6
Original file line numberDiff line numberDiff line change
@@ -55,32 +55,40 @@ const getOptionalInput = (name: string) => core.getInput(name) || undefined;
5555
"No changesets found, attempting to publish any unpublished packages to npm"
5656
);
5757

58+
let registry = core.getInput("registry");
5859
let userNpmrcPath = `${process.env.HOME}/.npmrc`;
60+
61+
core.info(`Publishing will be targeted to the registry: ${registry}`);
62+
5963
if (fs.existsSync(userNpmrcPath)) {
6064
core.info("Found existing user .npmrc file");
6165
const userNpmrcContent = await fs.readFile(userNpmrcPath, "utf8");
6266
const authLine = userNpmrcContent.split("\n").find((line) => {
63-
// check based on https://github.com/npm/cli/blob/8f8f71e4dd5ee66b3b17888faad5a7bf6c657eed/test/lib/adduser.js#L103-L105
64-
return /^\s*\/\/registry\.npmjs\.org\/:[_-]authToken=/i.test(line);
67+
// create regex from string, allowing to adapt with any custom registry
68+
const registryRegex = new RegExp(
69+
`^\\s*//${registry}/:[_-]authToken=`,
70+
"i"
71+
);
72+
return registryRegex.test(line);
6573
});
6674
if (authLine) {
6775
core.info(
68-
"Found existing auth token for the npm registry in the user .npmrc file"
76+
"Found existing auth token for the registry in the user .npmrc file"
6977
);
7078
} else {
7179
core.info(
72-
"Didn't find existing auth token for the npm registry in the user .npmrc file, creating one"
80+
"Didn't find existing auth token for the registry in the user .npmrc file, creating one"
7381
);
7482
fs.appendFileSync(
7583
userNpmrcPath,
76-
`\n//registry.npmjs.org/:_authToken=${process.env.NPM_TOKEN}\n`
84+
`\n//${registry}/:_authToken=${process.env.NPM_TOKEN}\n`
7785
);
7886
}
7987
} else {
8088
core.info("No user .npmrc file found, creating one");
8189
fs.writeFileSync(
8290
userNpmrcPath,
83-
`//registry.npmjs.org/:_authToken=${process.env.NPM_TOKEN}\n`
91+
`//${registry}/:_authToken=${process.env.NPM_TOKEN}\n`
8492
);
8593
}
8694

0 commit comments

Comments
 (0)