Logo

dev-resources.site

for different kinds of informations.

Publishing JSR package with Github Actions that react-hook-use-cta used

Published at
1/4/2025
Categories
beginners
webdev
githubactions
tooling
Author
rafde
Author
5 person written this
rafde
open
Publishing JSR package with Github Actions that react-hook-use-cta used

JSR

Publishing to Javascript Registry is straightforward.

  • Sign-in using a github account
  • Click Publish a package.
    • Create a new scope create a new scop Name your scope
    • Or select an existing scope Image description
  • Name your new package and create name package and create

JSR Package Settings

Make sure you go to your package settings to fill out

Image description

  • description of your package
  • compatibility. I selected Node and Deno since my package is for consumption

Image description

Fill this section out to link your jsr package with your github repo. JSR makes publishing easy through this linking. Better than having to make tokens.

Publish setup

JSR walks you through publishing

Image description

You can setup up jsr.json by copying and replacing
@your-scope/your-package to your jsr.json file in the repo root directory

{
  "name": "@your-scope/your-package",
  "version": "0.0.0",
  "license": "MIT",
  "exports": "./src/index.ts"
}
Enter fullscreen mode Exit fullscreen mode

You'll be able to publish through CLI, or github actions. I will be going over how to update via the .github/workflows/publish-package.yml file from the previous article

Github actions

.github/workflows/publish-package.yml should now look like

name: Publish to package registry

on:
  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:
    inputs:
      jsr:
        type: boolean
        default: true
        description: publish to JSR
      npm:
        type: boolean
        default: true
        description: publish to npm

  workflow_call:
    inputs:
      jsr:
        type: boolean
        default: true
      npm:
        type: boolean
        default: true

jobs:
  publish:
    runs-on: ubuntu-latest
    permissions:
      contents: read
      id-token: write # The OIDC ID token is used for authentication with JSR.

    steps:
      - name: Checkout repository
        if: ${{ inputs.jsr || inputs.npm }}
        uses: actions/checkout@v4

      - name: Set up Node.js
        if: ${{ inputs.jsr || inputs.npm }}
        uses: actions/setup-node@v4
        with:
          node-version: "lts/*"
          registry-url: 'https://registry.npmjs.org'
          cache: npm

      - name: Restore cache
        if: ${{ inputs.jsr  || inputs.npm  }}
        uses: actions/cache@v4
        with:
          path: |
            ~/.npm
          key: ${{ runner.os }}-publish-${{ hashFiles('**/package-lock.json') }}
          restore-keys: |
            ${{ runner.os }}-publish-

      - name: Install npm dependencies
        if: ${{ inputs.jsr  || inputs.npm  }}
        run: npm ci

      - name: Publish to jsr
        if: ${{ inputs.jsr }}
        run: npx jsr publish

      - name: Publish to npm
        if: ${{ inputs.npm  }}
        run: npm publish --provenance --access public
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_AUTH_TOKEN }}
Enter fullscreen mode Exit fullscreen mode

I included workflow_dispatch to let you select which to publish in case you need to do them individually from github actions.

workflow_call is also included to let you reuse the workflow. I'll go over how to use this in another article.

Setup JSR versioning

Unless you want to manually update jsr.json, you can use npm version to also update jsr.json using:

util/versioning.cjs

const fs = require( 'node:fs', );

const pkg = require( '../package.json', );
const jsr = require( '../jsr.json', );

jsr.version = pkg.version;

fs.writeFile(
  'jsr.json',
  JSON.stringify(
    jsr,
    null,
    2,
  ),
  {
    encoding: 'utf8',
    flags: 'w',
  },
  ( err, ) => {
    if ( err ) {
      console.error( 'error updating version', err, );
      return;
    }
    console.log( 'jsr.json version updated', );
  },
);
Enter fullscreen mode Exit fullscreen mode

Changes to package.json

I am only showing the necessary changes while trying to make it easy to copy and paste.

{
  "scripts": {
    "version": "node util/versioning.cjs && git add jsr.json && npm run build && git add -A dist",
  }
}
Enter fullscreen mode Exit fullscreen mode

jsr.json will now update using the new semver npm will get when you call npm version.

Now you are setup for publishing through Github actions.

The next step is setting up your github pages using nextjs

githubactions Article's
30 articles in total
Favicon
Git Commands Every Developer Must Know πŸ”₯
Favicon
Github Actions with Vercel in 2024
Favicon
Undo Mistakes in Git: Revert, Reset, and Checkout Simplified
Favicon
Taming the CI Beast: Optimizing a Massive Next.js Application (Part 1)
Favicon
Visualize TypeScript Dependencies of Changed Files in a Pull Request Using dependency-cruiser-report-action
Favicon
From Code to Cloud: Builds Next.js on GitHub Actions, straight to production
Favicon
Publishing JSR package with Github Actions that react-hook-use-cta used
Favicon
Zero Config Spring Batch: Just Write Business Logic
Favicon
When GitHub Actions Build Fails Due to pnpm-lockfile
Favicon
CI/CD Tools for Startups: Empowering IT Professionals to Scale Smarter
Favicon
Securely access Amazon EKS with GitHub Actions and OpenID Connect
Favicon
Publishing NPM package with Github Actions that react-hook-use-cta used
Favicon
[Boost]
Favicon
Building and Deploying a New API (Part 2)
Favicon
From days to minutes: Build and publish React Native apps using Fastlane and Github Actions
Favicon
Private LLMs for GitHub Actions
Favicon
Desplegar a Firebase con GitHub actions
Favicon
How to build a chatbot powered by github actions
Favicon
Building an educational game with AI tools and Azure Static Web Apps (Part 2)
Favicon
Continous Integration And Continous Deployment Of A Full-Stack Docker-Compose Application
Favicon
Git Hub Pages is a free and awesome solution for your profile or personal site
Favicon
Build vs. Buy: Choosing the Right Approach to IaC Management
Favicon
How to release a version of a web app using GitHub Workflow with GitHub Actions
Favicon
CI/CD com GitHub Actions e teste local com Act
Favicon
Deploying Containerized Applications to AWS ECS Using Terraform and CI/CD (Project Summary)
Favicon
Automating Unity Builds with GitHub Actions
Favicon
Auto Deploy Laravel with Deployer.yml sample With GithubΒ Runner
Favicon
Create an auto-merging workflow on Github
Favicon
github actions
Favicon
VS Code + LLM = ?

Featured ones: