Publishing an npm Package to GitHub Packages with GitHub Actions and GitHub App Authentication
In this post, we’ll walk through the process of publishing an npm package to GitHub Packages using GitHub Actions. We’ll also explore how to authenticate via a GitHub App, utilizing the peter-murray/workflow-application-token-action to obtain a JSON Web Token (JWT) for authentication. This approach provides a more secure and scalable method of managing access tokens in your CI/CD pipelines.
Prerequisites
Before we begin, ensure you have the following:
- A GitHub repository for your npm package.
- A GitHub App with the necessary permissions to generate a JWT.
- An npm account (if you also want to publish to npm’s registry).
C
Step 1: Create a GitHub App
To authenticate your GitHub Actions via a GitHub App, you first need to create the GitHub App.
- Navigate to GitHub Apps settings in your GitHub account.
- Click on New GitHub App.
- Fill in the required fields, such as the GitHub App name, Homepage URL, and Webhook URL (optional).
- Under Repository Permissions, grant the app the following permissions:
Contents(read and write)Packages(read and write)Actions(read and write)
- Save the App and generate a private key. That can be done in the end of the GitHub App settings page. Download this key, as you’ll need it later.
Step 2: Set Up Repository Secrets
Next, you’ll need to add the GitHub App’s credentials as secrets in your repository:
- Go to your repository on GitHub.
- Navigate to Settings > Secrets and variables > Actions.
- Add the following secrets:
APP_ID: The ID of your GitHub App.APP_PRIVATE_KEY: The private key you downloaded earlier (in base64 format).
Step 3: Configure GitHub Actions Workflow
Now that the GitHub App and repository secrets are set up, it’s time to configure the GitHub Actions workflow. This workflow will build your npm package and publish it to GitHub Packages.
Create a .github/workflows/publish.yml file in your repository with the following content:
name: Publish Package
on:
push:
branches:
- main
jobs:
publish:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: 16
registry-url: https://npm.pkg.github.com/ORG_OR_USERNAME
- name: Authenticate using GitHub App
id: jwt
uses: peter-murray/workflow-application-token-action@v3
with:
application_id: $
application_private_key: $
- name: Publish to GitHub Packages
run: npm publish
env:
NODE_AUTH_TOKEN: $
- name: Publish to npm registry
run: npm publish --registry=https://registry.npmjs.org
env:
NODE_AUTH_TOKEN: $
Step 4: Install GitHub App So it can be used in the workflow
Step 4: Understanding the Workflow
Let’s break down what’s happening in the workflow file:
-
Checkout code: The
actions/checkoutaction checks out the repository code. -
Set up Node.js: The
actions/setup-nodeaction sets up the Node.js environment. We specify the Node.js version and the registry URL for GitHub Packages. -
Authenticate using GitHub App: The
peter-murray/workflow-application-token-actionis used to generate a JWT using the GitHub App’s credentials. This JWT is used to authenticate with GitHub Packages. -
Publish to GitHub Packages: The
npm publishcommand is used to publish the package to GitHub Packages. TheNODE_AUTH_TOKENenvironment variable is set to the JWT obtained in the previous step. -
Publish to npm registry: Optionally, if you want to publish your package to npm’s registry as well, you can do so in this step. Ensure that you have added the
NPM_TOKENsecret to your repository.
Step 5: Test the Workflow
After pushing your changes to the main branch, the workflow will trigger automatically. You can check the progress and logs in the Actions tab of your repository.
If everything is set up correctly, your npm package will be published to GitHub Packages (and npm if configured).
Conclusion
By using a GitHub App for authentication and leveraging GitHub Actions, you can securely and efficiently automate the process of publishing npm packages to GitHub Packages. The peter-murray/workflow-application-token-action simplifies the JWT generation process, allowing you to manage authentication in a secure and scalable manner.
This setup provides a robust CI/CD pipeline, enabling you to focus on development while ensuring your packages are published seamlessly.