Skip to content

Instantly share code, notes, and snippets.

@alexalvess
Created July 3, 2024 17:44
Show Gist options
  • Select an option

  • Save alexalvess/8f5ac46c71b4947e1d18717776494cd3 to your computer and use it in GitHub Desktop.

Select an option

Save alexalvess/8f5ac46c71b4947e1d18717776494cd3 to your computer and use it in GitHub Desktop.
Prepare and Publish a NPM Package
name: Publish to NPM
on:
pull_request:
types: [closed]
branches:
- '**'
jobs:
create_tag_and_release:
runs-on: ubuntu-latest
if: ${{ github.event_name == 'pull_request' && github.event.pull_request.merged == true }}
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
- name: Configure Git credentials
run: |
git config --global user.email "github-actions[bot]@users.noreply.github.com"
git config --global user.name "github-actions[bot]"
- name: Extract package version
id: version
run: echo ::set-output name=version::$(node -e "console.log(require('./package.json').version)")
- name: Check if tag exists
id: tag_exists
run: |
git fetch --tags
tags=$(git tag -l)
if echo "$tags" | grep -q "v${{ steps.version.outputs.version }}"; then
echo "Tag v${{ steps.version.outputs.version }} already exists"
echo "exists=true" >> $GITHUB_ENV
else
echo "Tag v${{ steps.version.outputs.version }} does not exist"
echo "exists=false" >> $GITHUB_ENV
fi
- name: Create tag
if: env.exists == 'false'
run: git tag -a v${{ steps.version.outputs.version }} -m "Version ${{ steps.version.outputs.version }}"
- name: Push tag to GitHub
if: env.exists == 'false'
uses: ad-m/github-push-action@master
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
tags: v${{ steps.version.outputs.version }}
branch: ${{ github.ref }}
- name: Create release
if: env.exists == 'false'
id: create_release
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: v${{ steps.version.outputs.version }}
release_name: Release ${{ steps.version.outputs.version }}
body: |
Release ${{ steps.version.outputs.version }}
draft: false
prerelease: |
if [[ "${{ steps.version.outputs.version }}" == *"beta"* ]]; then
true
else
false
fi
publish:
runs-on: ubuntu-latest
needs: create_tag_and_release
steps:
- name: Checkout Repository
uses: actions/checkout@v4
with:
persist-credentials: false
fetch-depth: 0
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: "20"
- name: Install Dependencies
run: npm install
- name: Build Package
run: npm run build
- name: Set up npm authentication
run: echo "//registry.npmjs.org/:_authToken=${{ secrets.NPM_AUTH_TOKEN }}" > ~/.npmrc
- name: Publish to NPM
run: npm publish
env:
NODE_AUTH_TOKEN: ${{secrets.NPM_AUTH_TOKEN}}
name: Publish to NPM
on:
pull_request:
types: [opened, reopened]
branches-ignore:
- main
jobs:
update_pkg_version:
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout@v4
with:
persist-credentials: false
fetch-depth: 0
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: "20"
- name: Configure Git credentials
run: |
git config --global user.email "github-actions[bot]@users.noreply.github.com"
git config --global user.name "github-actions[bot]"
- name: Extract package version
id: check_version
run: |
VERSION=$(node -e "console.log(require('./package.json').version)")
if [[ "${VERSION}" == *"beta"* ]]; then
echo "::set-output name=is_beta::true"
else
echo "::set-output name=is_beta::false"
fi
- name: Patch Version Package
run: |
if [ "${{ steps.check_version.outputs.is_beta }}" == "true" ]; then
npm version prerelease --preid=beta --no-git-tag-version
else
npm version prepatch --preid=beta --no-git-tag-version
fi
- name: Commit and Push changes
run: |
git add .
git commit -m "update beta-version package"
- name: Push changes
uses: ad-m/github-push-action@master
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
branch: ${{ github.head_ref }}
name: Publish to NPM
on:
pull_request:
types: [opened, reopened]
branches:
- main
jobs:
update_pkg_version:
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout@v4
with:
persist-credentials: false
fetch-depth: 0
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: "20"
- name: Configure Git credentials
run: |
git config --global user.email "github-actions[bot]@users.noreply.github.com"
git config --global user.name "github-actions[bot]"
- name: Patch Version Package
run: npm version patch --no-git-tag-version
- name: Commit and Push changes
run: |
git add .
git commit -m "update version package"
- name: Push changes
uses: ad-m/github-push-action@master
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
branch: ${{ github.head_ref }}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment