Skip to content

NPMJS publishing

NPMJS publishing #14

Workflow file for this run

name: NPMJS publishing
on:
workflow_dispatch:
inputs:
action:
description: action to perform 'publish|publish-preview|cleanup-previews'
required: true
type: string
build_artifact:
description: name of build artifact to download before publishing
default: ''
type: string
run-id:
description: 'run identifier of the calling job'
required: false
permissions:
id-token: write # Required for OIDC
contents: read
jobs:
# publish package preview
npmjs-publish-preview:
if: |
inputs.action == 'publish-preview'
name: Npmjs Publish Previews
runs-on: ubuntu-latest
timeout-minutes: 20
env:
GITHUB_TOKEN: ${{ secrets.SPEC_RENDERER_BOT_PAT }}
steps:
- name: Checkout
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
- name: Setup PNPM with Dependencies
uses: ./.github/actions/setup-pnpm-with-dependencies/
- name: Download Build Artifacts
uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4.3.0
with:
name: ${{inputs.build-artifact}}
path: ./dist
run-id: ${{inputs.run-id}}
- name: Publish Package Preview
id: package-preview
run: |
git config user.email "[email protected]"
git config user.name "Kong UI Bot"
preid="pr.${{ github.event.pull_request.number }}.$(git rev-parse --short ${{ github.event.pull_request.head.sha }})"
tag="pr-${{ github.event.pull_request.number }}"
echo "preid=${preid}"
# Use yarn to bump the version for the prerelease
pnpm version prerelease --preid ${preid} --no-git-tag-version --yes --amend
package_version=$(jq -r ".version" package.json)
package=@kong/spec-renderer@"${package_version}"
npm show "${package}" >/dev/null 2>&1 && npm_show_status=0 || npm_show_status=1
if [ $npm_show_status -eq 0 ]; then
echo "Package ${package} is already published. Skipping publishing."
exit 0
fi
npm_instructions=""
pkg=$(pnpm publish --no-git-checks --access public --report-summary --tag "${tag}" | grep "+ "| sed 's/+ //')
if [[ -z "${pkg}" ]]; then
echo "Error publishing package"
exit -1
fi
npm_instructions="@$(echo ${pkg}|cut -d'@' -f2)@${tag}"
echo "npm_instructions<<EOF" >> $GITHUB_OUTPUT
echo -e "$npm_instructions" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
- name: Provide preview link info
if: |
steps.package-preview.outputs.npm_instructions != ''
uses: marocchino/sticky-pull-request-comment@773744901bac0e8cbb5a0dc842800d45e9b2b405 # v2.9.4
with:
header: pr_preview_consumption
message: |
### Install the preview package from this PR
```sh
${{ steps.package-preview.outputs.npm_instructions }}
```
GITHUB_TOKEN: ${{env.GITHUB_TOKEN}}
# clean old preview versions
npmjs-cleanup-previews:
if: |
inputs.action == 'cleanup-previews'
name: Npmjs Cleanup Previews
runs-on: ubuntu-latest
timeout-minutes: 20
env:
GITHUB_TOKEN: ${{ secrets.SPEC_RENDERER_BOT_PAT }}
steps:
- name: Checkout
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
- name: Setup PNPM with Dependencies
uses: ./.github/actions/setup-pnpm-with-dependencies/
- name: Prepare cleanup
id: prepare-cleanup
run: |
echo "openPRs=$(gh pr list --state open --json number|jq -cM 'map(.number|tostring)')" >> $GITHUB_OUTPUT
- name:
uses: Kong/public-shared-actions/pr-previews/cleanup@main
with:
package: "@kong/spec-renderer"
openPRs: ${{ steps.prepare-cleanup.outputs.openPRs }}