-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
[actions]: Add npm publish workflow #3460
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
name: Publish Package to npm | ||
on: | ||
workflow_dispatch: | ||
inputs: | ||
tag: | ||
description: "Tag to publish" | ||
required: true | ||
|
||
permissions: | ||
contents: read | ||
|
||
jobs: | ||
check-version: | ||
runs-on: ubuntu-latest | ||
outputs: | ||
is-new-version: ${{ steps.cpv.outputs.is-new-version }} | ||
steps: | ||
- uses: actions/checkout@v3 | ||
with: | ||
ref: ${{ github.event.inputs.tag }} | ||
|
||
- name: Validate semver pattern | ||
run: npx semver ${{ inputs.tag }} | ||
|
||
- name: Check package version | ||
id: cpv | ||
uses: PostHog/check-package-version@v2 | ||
|
||
- name: Validate package version | ||
uses: actions/github-script@v6 | ||
with: | ||
script: | | ||
const isNewVersion = `${{ steps.cpv.outputs.is-new-version }}`; | ||
if (isNewVersion === 'true') { | ||
console.log(`Version ${context.payload.inputs.tag} has not been published yet`); | ||
} else { | ||
core.setFailed(`Version ${context.payload.inputs.tag} is already published`); | ||
} | ||
|
||
check-status: | ||
needs: check-version | ||
if: needs.check-version.outputs.is-new-version == 'true' | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Verify checks passed | ||
uses: actions/github-script@v6 | ||
with: | ||
result-encoding: string | ||
retries: 3 | ||
script: | | ||
const ref = context.payload.inputs.tag; | ||
|
||
console.log(`Checking status checks for ${ref}`); | ||
|
||
const { owner, repo } = context.repo; | ||
const { default_branch: branch } = context.payload.repository; | ||
|
||
const branch = github.rest.repos.getBranch({ owner, repo, branch }); | ||
|
||
const checkSuites = await github.rest.checks.listSuitesForRef({ owner, repo, ref }); | ||
|
||
if (checkSuites.some(({ status }) => 'completed')) { | ||
core.setFailed(`Some workflows for ${context.payload.inputs.tag} are still in-progress`); | ||
} | ||
|
||
const { data: { check_runs: checkRuns } } = await Promise.all( | ||
(await branch).data.protection.required_status_checks.checks.map(({ context }) => ( | ||
github.rest.checks.listForRef({ | ||
owner, | ||
repo, | ||
ref, | ||
check_name: context | ||
}) | ||
) | ||
) | ||
|
||
checkRuns.forEach(({ name, status, conclusion }) => { | ||
if (status !== 'completed' || conclusion !== 'success') { | ||
console.log(`${name} check failed`); | ||
core.setFailed(`Required status check ${name} did not succeed`); | ||
} | ||
console.log(`${name} check passed`); | ||
}); | ||
|
||
publish: | ||
needs: [check-status] | ||
runs-on: ubuntu-latest | ||
permissions: | ||
contents: read | ||
id-token: write | ||
steps: | ||
- uses: step-security/harden-runner@v1 | ||
with: | ||
egress-policy: block | ||
allowed-endpoints: > | ||
github.com:443 | ||
nodejs.org:443 | ||
prod.api.stepsecurity.io:443 | ||
registry.npmjs.org:443 | ||
|
||
- uses: actions/checkout@v3 | ||
with: | ||
ref: ${{ github.event.inputs.tag }} | ||
|
||
- uses: ljharb/actions/node/install@main | ||
name: "nvm install lts/* && npm install" | ||
with: | ||
node-version: "lts/*" | ||
env: | ||
NPM_CONFIG_LEGACY_PEER_DEPS: true | ||
|
||
- run: echo "//registry.npmjs.org/:_authToken=${{ secrets.NODE_AUTH_TOKEN }}" >> .npmrc | ||
|
||
- run: npm publish --dry-run | ||
|
||
- uses: step-security/wait-for-secrets@v1 | ||
id: wait-for-secrets | ||
with: | ||
slack-webhook-url: ${{ secrets.SLACK_WEBHOOK_URL }} | ||
secrets: | | ||
OTP: | ||
name: 'OTP to publish package' | ||
description: 'OTP from authenticator app' | ||
|
||
- run: npm publish --access public --otp ${{ steps.wait-for-secrets.outputs.OTP }} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.