GitHub Action to create a release

With this workflow in place, creating a new release is as simple as pushing a version tag to your repository. The GitHub Action takes care of everything else for you. It checks out your code, sets up Node.js (using the latest LTS version), and installs your project’s dependencies if a package.json file is present. Once that’s done, it runs your build process to prepare the project for distribution.

The workflow then collects the necessary files, ignoring anything listed in .distignore, and copies them into a clean dist directory. From there it creates a zip archive of your project and attaches it to a new GitHub Release. The entire process is fully automated, which means you no longer need to manually package or upload files every time you want to publish a new version. Simply tag your release, push it, and GitHub handles the rest.

Place it under “./github/workflows/release.yml”

name: Build and Release

on:
  push:
    tags:
      - '[0-9]+'  # Matches single-digit tags like 1, 2, 3, etc.
      - '[0-9]+.[0-9]+'  # Matches tags like 1.0, 1.1, 2.0, etc.
      - '[0-9]+.[0-9]+.[0-9]+'  # Matches tags like 1.0.0, 1.0.1, 2.0.0, etc.

jobs:
  dist:
    name: "Build distribution release."
    runs-on: ubuntu-latest

    steps:
      - name: "Checks out the repository."
        uses: "actions/checkout@v2"

      - name: "Setup NodeJS."
        uses: actions/setup-node@v2
        with:
          node-version: '20'  # Use the latest LTS version of Node.js

      - name: "Install NPM dependencies"
        run: |
          if [ -f package.json ]; then
            npm install
          fi

      - name: "Build release: Stable"
        run: |
          if [ -f package.json ]; then
            npm run build
          fi

      - name: "Copy files to distribution directory excluding .distignore entries"
        run: |
          if [ -f .distignore ]; then
            rsync -a --delete --exclude-from='.distignore' ./ dist/
          else
            rsync -a --delete ./ dist/
          fi

      - name: "Create a Release Archive"
        run: |
          cd dist
          zip -r ../test-2.zip .

      - name: "Debug: List Distribution Directory"
        run: ls -la dist

      - name: "Create a GitHub Release"
        uses: softprops/action-gh-release@v1
        with:
          files: test-2.zip
        env:
          GITHUB_TOKEN: $ # Replase TEST_TOKEN with your token name
Click to Copy