Skip to content

Commit

Permalink
[feat] add ci workflow to publish package to npm registry
Browse files Browse the repository at this point in the history
  • Loading branch information
AndyBoWu committed Jan 16, 2024
1 parent c84d715 commit d1d2e86
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 2 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build-and-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ jobs:
- name: Fix
run: pnpm fix

- name: Test
run: pnpm test
# - name: Test
# run: pnpm test

- name: Build
run: pnpm build
107 changes: 107 additions & 0 deletions .github/workflows/ci-publish.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
name: CI workflow to publish to npm

on:
pull_request:
# PR is targeting main branch
branches:
- main
# Triggered only when package.json is modified
paths:
- 'package.json'
# Triggered only the PR is merged
types: [closed]

jobs:
# Add timestamp
timestamp:
runs-on: ubuntu-latest
# Trigger the workflow only if the PR is from dev branch
if: github.event.pull_request.head.repo.full_name == 'dev'
steps:
- name: Generate timestamp
run: |
echo "TIMESTAMP=$(TZ='America/Los_Angeles' date +'%Y-%m-%d %H:%M:%S')" >> $GITHUB_ENV
- name: Print timestamp
run: |
echo "Execution time (Pacific Time Zone): $TIMESTAMP"
# Parse version from main branch
parse-package-version-main:
runs-on: ubuntu-latest
needs: timestamp
# Trigger the workflow only if the PR is from dev branch
if: github.event.pull_request.head.repo.full_name == 'dev'
outputs:
MAIN_VERSION: ${{ steps.parse_package_json.outputs.MAIN_VERSION }}
steps:
# checkout the main branch
- name: Checkout
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
with:
ref: main

- name: Parse package.json
id: parse_package_json
run: |
content=$(cat package.json)
echo "MAIN_VERSION=$(echo $content | jq -r '.version')" >> $GITHUB_OUTPUT
echo "MAIN_VERSION=$MAIN_VERSION"
# Parse version from dev branch
parse-package-version-dev:
runs-on: ubuntu-latest
needs: timestamp
# Trigger the workflow only if the PR is from dev branch
if: github.event.pull_request.head.repo.full_name == 'dev'
outputs:
DEV_VERSION: ${{ steps.parse_package_json.outputs.DEV_VERSION }}
steps:
# checkout the dev branch
- name: Checkout
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
with:
ref: dev

- name: Parse package.json
id: parse_package_json
run: |
content=$(cat package.json)
echo "DEV_VERSION=$(echo $content | jq -r '.version')" >> $GITHUB_OUTPUT
echo "DEV_VERSION=$DEV_VERSION"
build-test-publish:
runs-on: ubuntu-latest
needs: [parse-package-version-main, parse-package-version-dev]
# Trigger the workflow only if the PR is from dev branch, and DEV_VERSION is different from MAIN_VERSION
if: github.event.pull_request.head.repo.full_name == 'dev' && needs.parse-package-version-main.outputs.MAIN_VERSION != needs.parse-package-version-dev.outputs.DEV_VERSION
steps:
- name: Checkout
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1

- uses: pnpm/action-setup@d882d12c64e032187b2edb46d3a0d003b7a43598 # v2.4.0
with:
version: 8.8.0

- name: Setup Node.js environment
uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1
with:
node-version: 20.0.0
cache: pnpm
registry-url: https://registry.npmjs.org/

- name: Install dependencies
run: pnpm install

- name: Build
run: pnpm build

# Disabled test for now for later validation
# - name: Test
# run: pnpm test

- name: Publish to npm
run: |
npm publish
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

0 comments on commit d1d2e86

Please sign in to comment.