Certainly! release-please is a tool that automates the creation of releases for your projects. When used with GitHub Actions, it can automatically generate release PRs or directly publish to npm. Below, I'll guide you through setting up a GitHub Action to use release-please for your pnpm monorepo with two packages: packages/client and packages/types.
Step 1: Install release-please in your monorepo
First, you need to install release-please as a dev dependency in the root of your monorepo.
release-please uses a manifest file to configure how it should handle releases for different packages. Create a .release-please-manifest.json file in the root of your monorepo with the following content:
This file maps each package to its initial release status. release-please will update this file as it creates releases.
Step 3: Create GitHub Action Workflow
Create a new workflow file at .github/workflows/release-please.yml with the following content:
yaml
name: Release Please
on:
push:
branches:
- main # or whatever your default branch is
jobs:
release-please:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v3
with:
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of release notes
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: '18' # or whatever Node.js version you are using
- name: Install dependencies
run: pnpm install
- name: Release Please - Create Release PR
id: release
uses: google-github-actions/release-please-action@v3
with:
release-type: node
package-name: packages/client,packages/types
manifest-file: .release-please-manifest.json
bump-minor-pre-major: true
bump-patch-for-minor-pre-major: true
- name: Check outputs
run: |
echo "Release Created: ${{ steps.release.outputs.releases-created }}"
if [ "${{ steps.release.outputs.releases-created }}" = "true" ]; then
echo "Release PR Number: ${{ steps.release.outputs.pull-request-number }}"
echo "Release URL: ${{ steps.release.outputs.release-url }}"
fi
Step 4: Encrypt NPM Token
To publish to npm, you need to add an NPM token to your GitHub repository secrets.
Generate an NPM token by running npm login and copying the token, or generate one from your npm account settings.
Go to your GitHub repository.
Navigate to Settings > Secrets and variables > Actions.
Click New repository secret.
Name the secret NPM_TOKEN and paste your NPM token as the value.
Step 5: Update Workflow to Publish to npm
Modify the .github/workflows/release-please.yml to include the npm publishing step:
yaml
name: Release Please
on:
push:
branches:
- main # or whatever your default branch is
jobs:
release-please:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v3
with:
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of release notes
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: '18' # or whatever Node.js version you are using
registry-url: 'https://registry.npmjs.org'
- name: Install dependencies
run: pnpm install
- name: Release Please - Create Release PR
id: release
uses: google-github-actions/release-please-action@v3
with:
release-type: node
package-name: packages/client,packages/types
manifest-file: .release-please-manifest.json
bump-minor-pre-major: true
bump-patch-for-minor-pre-major: true
- name: Check outputs
run: |
echo "Release Created: ${{ steps.release.outputs.releases-created }}"
if [ "${{ steps.release.outputs.releases-created }}" = "true" ]; then
echo "Release PR Number: ${{ steps.release.outputs.pull-request-number }}"
echo "Release URL: ${{ steps.release.outputs.release-url }}"
fi
- name: Publish to npm
if: steps.release.outputs.releases-created == 'true'
run: |
pnpm --filter packages/client publish --access public --no-git-checks
pnpm --filter packages/types publish --access public --no-git-checks
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
Explanation:
Checkout repository: Checks out your repository code.
Setup Node.js: Sets up the Node.js environment.
Install dependencies: Installs the dependencies using pnpm.
Release Please - Create Release PR: Runs the release-please action to create release PRs for the specified packages. It updates the .release-please-manifest.json file accordingly
Check Outputs: Prints the release outputs for debugging purposes.
Publish to npm: If releases were created (releases-created is true), this step publishes the packages to npm using the NPM_TOKEN secret. The --no-git-checks flag is used to bypass git checks since we are in a CI environment.
With this setup, every push to the main branch will trigger the release-please action. If there are changes that require a new release, release-please will create a release PR. Once the PR is merged, the workflow will publish the new versions of packages/client and packages/types to npm.