Skip to content

Add a way to set a custom NPM registry #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
80 changes: 76 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ Pass your token to the action:

```yaml
- name: Publish
uses: MetaMask/action-npm-publish@v1
uses: MetaMask/action-npm-publish@v2
with:
npm-token: ${{ secrets.NPM_TOKEN }}
```

To publish an npm module whenever a release PR is merged, you could do something like this:
To publish an NPM module whenever a release PR is merged, you could add a workflow file like the following to your project. Note that this requires you add a `publish` environment to your repository and set the `NPM_TOKEN` environment variable within that environment to your NPM token:

```yaml
name: Publish to npm
Expand Down Expand Up @@ -65,7 +65,7 @@ jobs:
key: ${{ github.sha }}
- name: Publish
# omitting npm-token will perform a dry run publish
uses: MetaMask/action-npm-publish@v1
uses: MetaMask/action-npm-publish@v2
publish-npm:
# use a github actions environment
environment: publish
Expand All @@ -80,7 +80,79 @@ jobs:
./node_modules/.yarn-state.yml
key: ${{ github.sha }}
- name: Publish
uses: MetaMask/action-npm-publish@v1
uses: MetaMask/action-npm-publish@v2
with:
npm-token: ${{ secrets.NPM_TOKEN }}
```

If you are making changes to the workflow(s) in your repository and need to test that your package still gets published correctly, you can configure the action to use your own NPM registry instead of the official one. For instance, here is a workflow file that uses [Gemfury](https://gemfury.com/help/npm-registry/):

```yaml
name: Publish to npm

on:
pull_request:
types: [closed]

jobs:
cache-build:
permissions:
contents: write
if: |
github.event.pull_request.merged == true &&
startsWith(github.event.pull_request.head.ref, 'release/')
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Get Node.js version
id: nvm
run: echo ::set-output name=NODE_VERSION::$(cat .nvmrc)
- uses: actions/setup-node@v2
with:
node-version: ${{ steps.nvm.outputs.NODE_VERSION }}
- run: |
yarn setup
yarn build
- uses: actions/cache@v2
id: restore-build
with:
path: ./*
key: ${{ github.sha }}
publish-npm-dry-run:
runs-on: ubuntu-latest
needs: cache-build
steps:
- uses: actions/cache@v2
id: restore-build
with:
path: ./*
key: ${{ github.sha }}
- name: Publish
uses: MetaMask/action-npm-publish@v2
with:
# omitting npm-token will perform a dry run publish
npm-registry: https://npm.fury.io/YOUR-USERNAME-GOES-HERE/
publish-npm:
# use a github actions environment
environment: publish
runs-on: ubuntu-latest
needs: publish-npm-dry-run
steps:
- uses: actions/cache@v2
id: restore-build
with:
path: ./*
key: ${{ github.sha }}
- name: Publish
uses: MetaMask/action-npm-publish@v2
with:
npm-registry: https://npm.fury.io/YOUR-USERNAME-GOES-HERE/
npm-token: ${{ secrets.NPM_TOKEN }}
```

## API

### Inputs

- **`npm-registry`** _(optional; defaults to whatever Yarn's `npmPublishRegistry` option defaults to)_. The URL of the NPM registry that Yarn commands will use to access and publish packages.
- **`npm-token`** _(optional)_. The auth token associated with the registry that Yarn commands will use to access and publish packages. If omitted, the action will perform a dry-run publish.
6 changes: 5 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
name: 'Publish to npm'
description: 'Publish the release to npm'
inputs:
npm-registry:
description: 'The URL of the NPM registry that Yarn commands will use to access and publish packages.'
required: false
npm-token:
description: 'The token used for npm publishing. If omitted the action will perform a dry run npm publish.'
description: 'The auth token associated with the registry that Yarn commands will use to access and publish packages. If omitted, the action will perform a dry-run publish.'
required: false

runs:
Expand All @@ -12,4 +15,5 @@ runs:
shell: bash
run: ${{ github.action_path }}/scripts/main.sh
env:
YARN_NPM_REGISTRY_SERVER: ${{ inputs.npm-registry }}
YARN_NPM_AUTH_TOKEN: ${{ inputs.npm-token }}