Skip to content

Commit

Permalink
Handle publishing of packages using Yarn Classic / NPM (#87)
Browse files Browse the repository at this point in the history
* feat: fall back to NPM_TOKEN if YARN_NPM_AUTH_TOKEN not set
* fix: use npm for publish when yarn berry not available
* ci: add missing PUBLISH_NPM_TAG env var

---------

Co-authored-by: Elliot Winkler <elliot.winkler@gmail.com>
  • Loading branch information
legobeat and mcmire authored Jun 12, 2024
1 parent 1ad8c25 commit 7cf06c2
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test-monorepo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ jobs:
cd skunkworks
yarn install --immutable
yarn plugin import workspace-tools
NPM_TOKEN="test" ../action-npm-publish/scripts/main.sh
PUBLISH_NPM_TAG="latest" NPM_TOKEN="test" ../action-npm-publish/scripts/main.sh
33 changes: 29 additions & 4 deletions scripts/publish.sh
Original file line number Diff line number Diff line change
@@ -1,15 +1,38 @@
#!/usr/bin/env bash

set -x
set -e
set -o pipefail

export YARN_NPM_AUTH_TOKEN="${YARN_NPM_AUTH_TOKEN:-$NPM_TOKEN}"

YARN_MAJOR="$(yarn --version | sed 's/\..*//' || true)"
if [[ "$YARN_MAJOR" -ge "3" ]]; then
PUBLISH_CMD="yarn npm publish --tag $PUBLISH_NPM_TAG"
PACK_CMD="yarn pack --out /tmp/%s-%v.tgz"
# install is handled by yarn berry pack/publish
INSTALL_CMD=""
LOGIN_CMD=""

Check warning on line 14 in scripts/publish.sh

View workflow job for this annotation

GitHub Actions / __fearphage_shellcheck-action

scripts/publish.sh#L14

LOGIN_CMD appears unused. Verify use (or export if used externally).
else
echo "Warning: Did not detect compatible yarn version. This action officially supports Yarn v3 and newer. Falling back to using npm." >&2
echo "//registry.npmjs.org/:_authToken=${YARN_NPM_AUTH_TOKEN}" >> $HOME/.npmrc

Check notice on line 17 in scripts/publish.sh

View workflow job for this annotation

GitHub Actions / __fearphage_shellcheck-action

scripts/publish.sh#L17

Double quote to prevent globbing and word splitting.
PUBLISH_CMD="npm publish --tag $PUBLISH_NPM_TAG"
PACK_CMD="npm pack --pack-destination=/tmp/"
if [[ -f 'yarn.lock' ]]; then
INSTALL_CMD="yarn install --frozen-lockfile"
else
INSTALL_CMD="npm ci"
fi
fi

if [[ -z $YARN_NPM_AUTH_TOKEN ]]; then
echo "Notice: 'npm-token' not set. Running 'yarn pack'."
yarn pack --out /tmp/%s-%v.tgz
echo "Notice: 'npm-token' not set. Running '$PACK_CMD'."
$INSTALL_CMD
$PACK_CMD
exit 0
fi

set -x

if [[ -z $PUBLISH_NPM_TAG ]]; then
echo "Notice: 'npm-tag' not set."
exit 1
Expand All @@ -34,4 +57,6 @@ if [[ -n "$1" ]]; then
fi
fi

yarn npm publish --tag "$PUBLISH_NPM_TAG"
$INSTALL_CMD
$PUBLISH_CMD
rm -f $HOME/.npmrc

Check notice on line 62 in scripts/publish.sh

View workflow job for this annotation

GitHub Actions / __fearphage_shellcheck-action

scripts/publish.sh#L62

Double quote to prevent globbing and word splitting.

0 comments on commit 7cf06c2

Please sign in to comment.