diff --git a/.github/workflows/test-monorepo.yml b/.github/workflows/test-monorepo.yml index 6e5884d..9799e9e 100644 --- a/.github/workflows/test-monorepo.yml +++ b/.github/workflows/test-monorepo.yml @@ -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 diff --git a/scripts/publish.sh b/scripts/publish.sh index 31aa957..58a76c3 100755 --- a/scripts/publish.sh +++ b/scripts/publish.sh @@ -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="" +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 + 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 @@ -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