|
| 1 | +name: "Publish a package to a specified npm registry" |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_call: |
| 5 | + inputs: |
| 6 | + npm-registry-url: |
| 7 | + description: "URL of the npm registry to publish to; e.g., https://npm.pkg.github.com for GitHub Packages" |
| 8 | + type: string |
| 9 | + required: true |
| 10 | + |
| 11 | + secrets: |
| 12 | + npm-token: |
| 13 | + description: "Token that is allowed to publish to the npm registry; e.g., secrets.GITHUB_TOKEN for GitHub Packages" |
| 14 | + required: true |
| 15 | + |
| 16 | +permissions: |
| 17 | + contents: read |
| 18 | + packages: write |
| 19 | + |
| 20 | +env: |
| 21 | + node-version: 22 |
| 22 | + pnpm-version: 10 |
| 23 | + |
| 24 | +jobs: |
| 25 | + build-and-publish: |
| 26 | + runs-on: ubuntu-latest |
| 27 | + |
| 28 | + steps: |
| 29 | + - name: Checkout |
| 30 | + uses: actions/checkout@v4 |
| 31 | + |
| 32 | + - name: Get short commit hash |
| 33 | + id: commit-hash |
| 34 | + run: echo "short-commit-hash=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT |
| 35 | + |
| 36 | + # appends the short commit hash to the version number |
| 37 | + # 1. reads the package.json file |
| 38 | + # 2. replaces the version and saves it in the package.json |
| 39 | + - name: Read package information |
| 40 | + id: package-info |
| 41 | + # uses the exact commit to prevent harmful updates |
| 42 | + uses: jaywcjlove/github-action-package@f6a7afaf74f96a166243f05560d5af4bd4eaa570 |
| 43 | + with: |
| 44 | + path: package.json |
| 45 | + - name: Append short commit hash to the version |
| 46 | + # uses the exact commit to prevent harmful updates |
| 47 | + uses: jaywcjlove/github-action-package@f6a7afaf74f96a166243f05560d5af4bd4eaa570 |
| 48 | + with: |
| 49 | + path: package.json |
| 50 | + version: ${{ steps.package-info.outputs.version }}-${{ steps.commit-hash.outputs.short-commit-hash }} |
| 51 | + |
| 52 | + - name: Install pnpm ${{ env.pnpm-version }} |
| 53 | + uses: pnpm/action-setup@v4 |
| 54 | + with: |
| 55 | + version: ${{ env.pnpm-version }} |
| 56 | + |
| 57 | + - name: Setup Node.js ${{ env.node-version }} |
| 58 | + uses: actions/setup-node@v4 |
| 59 | + with: |
| 60 | + node-version: ${{ env.node-version }} |
| 61 | + cache: pnpm |
| 62 | + registry-url: ${{ inputs.npm-registry-url }} |
| 63 | + scope: '@codemonger-io' |
| 64 | + |
| 65 | + - name: Installs dependencies |
| 66 | + run: pnpm install |
| 67 | + |
| 68 | + # the build script is executed by the prepare script |
| 69 | + - name: Build and publish |
| 70 | + env: |
| 71 | + NODE_AUTH_TOKEN: ${{ secrets.npm-token }} |
| 72 | + run: pnpm publish --no-git-checks |
0 commit comments