Last active
June 15, 2021 07:03
-
-
Save Sv443/79e6713191e41b4957c1f8698dca4468 to your computer and use it in GitHub Desktop.
GitHub Actions: Publish to NPM and GitHub Package Registry on created release
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# In order for this to work, since GitHub Package Registry likes to be a thorn in your ass, you'll have to follow this guide, otherwise this script will not work: | |
# https://pastebin.com/f24qc4Wk | |
# | |
# >>>> There are two lines that end in four exclamation marks. You will have to enter your usernames there for this script to work. | |
# | |
# Author: Sv443 | |
# License: WTFPL (http://www.wtfpl.net/txt/copying/) | |
name: Publish to NPM and GPR | |
on: | |
release: # When a release is created, run the job | |
types: ["created"] | |
jobs: | |
# The "build" job checks out your code, installs Node.js and installs all dependencies and devDependencies | |
build: | |
runs-on: "ubuntu-latest" | |
timeout-minutes: 10 | |
strategy: | |
fail-fast: false | |
matrix: | |
node-version: [13.x] # Set the Node version here | |
steps: | |
- uses: actions/checkout@v1 # Checkout the latest commit | |
- name: Use Node.js ${{ matrix.node-version }} # Set up Node.js | |
uses: actions/setup-node@v1 | |
with: | |
node-version: ${{ matrix.node-version }} | |
- name: Install Dependencies # Run the command "npm ci" to clean install all dependencies and devDependencies | |
run: npm ci | |
# Publish to NPM: | |
publish-npm: | |
runs-on: "ubuntu-latest" | |
needs: ["build"] # Requires the "build" job to be completed first | |
steps: | |
- uses: actions/checkout@v1 # Checkout latest commit | |
- name: Use Node.js ${{ matrix.node-version }} # Set up Node.js | |
uses: actions/setup-node@v1 | |
with: | |
node-version: ${{ matrix.node-version }} | |
registry-url: https://registry.npmjs.org | |
scope: "@your_npm_username_here" # Add your NPM username here, in all lowercase ! ! ! ! ! | |
- name: Publish to NPM | |
run: npm publish # Runs the command "npm publish" | |
env: | |
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} # Sets the environment variable NODE_AUTH_TOKEN to the NPM_TOKEN you set in Repo Settings / Secrets | |
publish-gpr: | |
runs-on: "ubuntu-latest" | |
needs: ["build"] | |
steps: | |
- uses: actions/checkout@v1 # Checkout latest commit | |
- name: Use Node.js ${{ matrix.node-version }} # Set up Node.js | |
uses: actions/setup-node@v1 | |
with: | |
node-version: ${{ matrix.node-version }} | |
registry-url: https://npm.pkg.github.com # Use the GitHub registry URL instead of the NPM registry URL | |
scope: "@your_github_username" # Add your GitHub username here, in all lowercase ! ! ! ! ! | |
- name: Publish to GPR | |
run: | # Replaces the file "package.json" with the file "package-gpr.json" - make sure to read the link at the top to understand what this mean | |
mv package.json package-npm.json | |
mv package-gpr.json package.json | |
npm publish | |
env: | |
NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }} # The GITHUB_TOKEN is automatically included by GitHub | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # The GITHUB_TOKEN is automatically included by GitHub |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment