<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Gaagul</title>
    <description>The latest articles on DEV Community by Gaagul (@gaagul).</description>
    <link>https://dev.to/gaagul</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3006422%2F2d6ccf65-fcec-4dd9-92d6-d9b65c51ef99.jpg</url>
      <title>DEV Community: Gaagul</title>
      <link>https://dev.to/gaagul</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/gaagul"/>
    <language>en</language>
    <item>
      <title>How to Publish and Release npm Packages Using GitHub Actions</title>
      <dc:creator>Gaagul</dc:creator>
      <pubDate>Wed, 02 Apr 2025 10:33:04 +0000</pubDate>
      <link>https://dev.to/gaagul/how-to-publish-and-release-npm-packages-using-github-actions-4640</link>
      <guid>https://dev.to/gaagul/how-to-publish-and-release-npm-packages-using-github-actions-4640</guid>
      <description>&lt;p&gt;If you've ever released an npm package manually, you know how repetitive it can be. You have to bump the version, update the changelog, create a Git tag, push everything to GitHub, publish to npm, and maybe even draft a release. Mess up one step, and things can get messy fast.&lt;br&gt;
That's why automating this process with GitHub Actions is a game-changer. It takes care of versioning, publishing, and creating releases - all without you lifting a finger. In this post, I'll walk you through setting up a GitHub Action that does it all for you.&lt;br&gt;
Let's dive in! 🚀&lt;/p&gt;

&lt;p&gt;🔐 Setup: Creating and Adding Required Tokens&lt;/p&gt;

&lt;p&gt;1️⃣ Create an npm token&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;We need to create and add an npm token in the GitHub repository secrets to publish packages from the workflow.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Follow the official docs: Creating and Viewing Access Tokens&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;2️⃣ Create a GitHub Repository Secret&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;In your GitHub repository, go to Settings → Secrets and variables → Actions.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Click on New repository secret.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Name the secret NPM_TOKEN.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Paste the npm token as the secret value.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Click Add secret.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;3️⃣ GitHub Token (Created Automatically)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;GitHub provides an authentication token for GitHub Actions, allowing secure API requests.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;More info: GitHub Token Documentation&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;🔄 Workflow Overview&lt;br&gt;
📌 Triggering the Action&lt;br&gt;
The action runs when a PR is merged into main and has the release label.&lt;br&gt;
It checks if the PR includes a version bump label (patch, minor, or major).&lt;br&gt;
🔼 Version Bumping&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Based on the label, the script updates the package version using pnpm version.&lt;/li&gt;
&lt;li&gt;The new version is committed and pushed back to the repository.
🏗️ Building and Publishing&lt;/li&gt;
&lt;li&gt;The package is built using pnpm build.&lt;/li&gt;
&lt;li&gt;It is then published to npm using pnpm publish.
🏷️ Creating a GitHub Release&lt;/li&gt;
&lt;li&gt;Once published, the workflow tags the new version and creates a GitHub release.&lt;/li&gt;
&lt;li&gt;This makes the new version easily accessible and trackable for users.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;🛠️ GitHub Actions Workflow (YAML)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;name: Release &amp;amp; Publish

on:
  pull_request:
    types:
      - closed
    branches:
      - main

jobs:
  release:
    if: &amp;gt;-
      github.event.pull_request.merged == true &amp;amp;&amp;amp;
      contains(join(github.event.pull_request.labels.*.name, ','), 'release')
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v3

      - name: Setup Node.js
        uses: actions/setup-node@v3
        with:
          node-version: "lts/*"
          registry-url: "https://registry.npmjs.org/"
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

      - name: Setup pnpm
        run: |
          corepack enable
          pnpm install
          echo "pnpm setup complete."

      - name: Determine version bump type
        id: version_bump
        run: |
          labels=("${{ join(github.event.pull_request.labels.*.name, ' ') }}")

          if [[ "${labels[@]}" == *patch* ]]; then
            echo "RELEASE_TYPE=patch" &amp;gt;&amp;gt; $GITHUB_ENV
          elif [[ "${labels[@]}" == *minor* ]]; then
            echo "RELEASE_TYPE=minor" &amp;gt;&amp;gt; $GITHUB_ENV
          elif [[ "${labels[@]}" == *major* ]]; then
            echo "RELEASE_TYPE=major" &amp;gt;&amp;gt; $GITHUB_ENV
          else
            echo "No valid release label found. Exiting..."
            exit 1
          fi

      - name: Setup git user
        run: |
          git config user.name "&amp;lt;USER_NAME&amp;gt;"
          git config user.email "&amp;lt;USER_EMAIL&amp;gt;"
          echo "Git user setup complete."

      - name: Bump version
        run: |
          pnpm version ${{ env.RELEASE_TYPE }} --no-git-tag-version
          git add package.json
          git commit -m "ACTION: Bump version to $(node -p "require('./package.json').version")"
          git push

      - name: Build package
        run: |
          pnpm build

      - name: Publish package
        run: |
            pnpm publish --access public --no-git-checks
        env:
            NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

      - name: Get new version
        id: package_version
        run: echo "VERSION=$(jq -r .version package.json)" &amp;gt;&amp;gt; $GITHUB_ENV

      - name: Create GitHub Release
        uses: softprops/action-gh-release@v1
        with:
          tag_name: v${{ env.VERSION }}
          name: Release v${{ env.VERSION }}
          generate_release_notes: true
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;🔧 Key GitHub Actions Used&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;actions/checkout@v3 → This action checks out your repository so that the workflow can access your code.&lt;/li&gt;
&lt;li&gt;actions/setup-node@v3 → Sets up a Node.js environment, specifying the version and npm registry authentication.&lt;/li&gt;
&lt;li&gt;softprops/action-gh-release@v1 → Automatically creates a GitHub Release with the new version tag and release notes.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;🔀 Alternative: Raising a PR Instead of Pushing Directly to Main&lt;/p&gt;

&lt;p&gt;If you don't want the workflow to commit version bumps and changes directly to main, you can configure it to create a pull request instead. This keeps your main branch clean and allows for review before merging.&lt;/p&gt;

&lt;p&gt;🛠 Using peter-evans/create-pull-request&lt;/p&gt;

&lt;p&gt;Modify your workflow to use the peter-evans/create-pull-request action:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;yamlCopy- name: Create Pull Request for Version Bump
  uses: peter-evans/create-pull-request@v7
  with:
    branch: release/version-bump
    title: "Bump package version to ${{ env.VERSION }}"
    body: "This PR updates the package version to ${{ env.VERSION }}."
    commit-message: "chore: bump package version to ${{ env.VERSION }}"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;🎉 Wrapping Up&lt;/p&gt;

&lt;p&gt;Automating npm package releases with GitHub Actions can save time, reduce errors, and make your workflow seamless. By setting up this automation, you ensure that every release is consistent, well-documented, and instantly available to your users.&lt;br&gt;
Whether you push changes directly to main or prefer a review process with a pull request, this setup gives you flexibility and control over your releases.&lt;br&gt;
Now it's your turn! Have you automated your npm releases? Do you have any improvements or additional steps in your workflow? Drop your thoughts in the comments! 🚀&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
